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

}

};

}

}

相关推荐
一直跑16 小时前
查看显卡驱动版本,查看哪个用户使用显卡(GPU)进程
linux·服务器
先做个垃圾出来………16 小时前
Python位运算及操作
java·前端·python
你怎么知道我是队长17 小时前
C语言---字符串
java·c语言·算法
rannn_11117 小时前
【Java项目】中北大学Java大作业|电商平台
java·git·后端·课程设计·中北大学
ICT系统集成阿祥17 小时前
Linux运维最万能的三条指令
linux·运维·服务器
资生算法程序员_畅想家_剑魔17 小时前
Java常见技术分享-26-事务安全-锁机制-常见的锁实现
java·开发语言
座山雕~17 小时前
spring
java·后端·spring
草原印象17 小时前
Spring、SpringMVC、Mybatis框架整合实战视频课程
java·spring·mybatis
QT 小鲜肉18 小时前
【Linux命令大全】002.文件传输之uupick命令(实操篇)
linux·运维·服务器·chrome·笔记
乌日尼乐18 小时前
【Java】IO流完全指南
java·后端