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

}

};

}

}

相关推荐
是小蟹呀^35 分钟前
Spring Security + JWT 面试题整理
java·jwt·springsecurity
FoldWinCard2 小时前
D5 Linux 网络及端口命令
linux·运维·服务器
spencer_tseng3 小时前
Redis + Nacos.bat
java·windows·dos
kirs_ur3 小时前
SSD 在 AI 训练中的角色
大数据·服务器·人工智能
troyzhxu3 小时前
列表查询的 GraphQL —— 一行代码终结你的 if-else 地狱!
java·springboot·graphql
tedcloud1233 小时前
OmniRoute怎么部署?开源AI模型路由平台Linux部署教程
linux·服务器·人工智能·开源·音视频
霸道流氓气质3 小时前
KMS 密钥管理服务(Key Management Service)原理与实践
linux·服务器·数据库
kirs_ur4 小时前
CXL(Compute Express Link)— 内存扩展的未来
服务器·数据库·性能优化
孫治AllenSun4 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm