springintegration sftp gateway move file and then process

@Configuration

@EnableIntegration

public class SftpIntegrationConfig {

@Bean

public SessionFactory<LsEntry> sftpSessionFactory() {

DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);

factory.setHost("sftp.server.com");

factory.setPort(22);

factory.setUser("username");

factory.setPassword("password");

factory.setAllowUnknownKeys(true);

return new CachingSessionFactory<>(factory);

}

@Bean

public IntegrationFlow sftpInboundFlow() {

return IntegrationFlows.from(

Sftp.inboundAdapter(sftpSessionFactory())

.preserveTimestamp(true)

.remoteDirectory("remote/dir")

.localDirectory(new File("local/dir"))

.autoCreateLocalDirectory(true)

.localFilter(new AcceptOnceFileListFilter<>())

.deleteRemoteFiles(false), // Set to false to keep the remote file after downloading

e -> e.poller(Pollers.fixedDelay(5000)))

.transform(Transformers.fileToString())

.channel("fileInputChannel") // Send the file content to the fileInputChannel

.get();

}

@Bean

public IntegrationFlow moveAndProcessFlow() {

return IntegrationFlows.from("fileInputChannel")

.handle(Sftp.outboundGateway(sftpSessionFactory(),

Command.MV, "headers'file_remoteDirectory' + '/' + headers'file_remoteFile'")

.renameExpression("headers'file_rename_to'")

.options(Option.RECURSIVE)

.temporaryFileSuffix(".writing"),

e -> e.advice(expressionAdvice()))

.handle("fileProcessor", "processFile")

.get();

}

@Bean

public ExpressionEvaluatingRequestHandlerAdvice expressionAdvice() {

ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();

advice.setSuccessChannelName("moveAndProcessChannel");

advice.setOnSuccessExpressionString("headers'file_originalFile' + ' was successfully moved and processed'");

return advice;

}

@Bean

public MessageChannel fileInputChannel() {

return new DirectChannel();

}

@Bean

public MessageChannel moveAndProcessChannel() {

return new DirectChannel();

}

@Bean

public MessageHandler fileProcessor() {

return new MessageHandler() {

@Override

public void handleMessage(Message<?> message) throws MessagingException {

String fileContent = (String) message.getPayload();

System.out.println("Processing file content: " + fileContent);

// Process the file content here

}

};

}

}

相关推荐
数智化管理手记1 小时前
海量数据如何沉淀有效数据资产?数据标准化治理方案如何搭建?
java·大数据·人工智能
隔窗听雨眠2 小时前
GBase 8s并发控制深度解析:封锁机制、隔离级别与死锁处理全攻略
服务器·数据库·oracle
cfm_29142 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型
鲸采云SRM采购管理系统3 小时前
采购管理系统哪家好?2026最新测评对比
java·服务器·数据库
那年窗外下的雪.3 小时前
AIDC 学习日志 第 04 天 BGP Underlay 基础与路由选择
服务器·学习·php
0x534 小时前
Java网络编程(二)
java·服务器·网络
Flittly4 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
Poo_Chai4 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
瑞码空间4 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
树码小子4 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea