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

}

};

}

}

相关推荐
小此方2 分钟前
Re:Linux系统篇(二十二)进程篇·七:环境变量的底层溯源、核心获取机制与安全应用
linux·运维·服务器
2601_957786775 分钟前
多平台矩阵运营的底层逻辑:当账号管理、内容生产与线索转化被一条链路串起来
java·数据库·矩阵·多平台管理
Cloud_Shy6188 分钟前
Linux 系统权限知识及应用实践
linux·运维·服务器·centos·云计算
代码中介商9 分钟前
排序算法完全指南(六):希尔排序深度详解
java·算法·排序算法
kjmkq12 分钟前
2026智算中心液冷服务器机房建设标准及常见方案对比
运维·服务器
腾讯蓝鲸智云15 分钟前
【运维自动化-节点管理】资源配额功能如何使用
运维·服务器·自动化·云计算
一氧化二氢.h19 分钟前
Linux手动脚本 vs Dockerfile部署:核心区别与适用场景
linux·运维·服务器
布吉岛的石头24 分钟前
Java 程序员第 22 阶段:Function Call 工具调用实战,Java 封装大模型外部能力
java·人工智能·python
阿维的博客日记32 分钟前
线程任务执行报错后,线程会不会挂掉,Java线程池
java·线程池
Hwang25234 分钟前
Spring 框架- 容器单例池的理解
java