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版连接汇川PLC,发送数据,读取数据,保持重新链接,适用安卓
android·java·python
无糖冰可乐213 小时前
IDEA多java版本切换
java·ide·intellij-idea
合作小小程序员小小店3 小时前
web开发,在线%超市销售%管理系统,基于idea,html,jsp,java,ssh,sql server数据库。
java·前端·sqlserver·ssh·intellij-idea
brucelee1863 小时前
IntelliJ IDEA 设置 Local History 永久保留
java·ide·intellij-idea
Pluto_CSND5 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
深耕AI6 小时前
【完整教程】宝塔面板FTP配置与FileZilla连接服务器
运维·服务器
百***46456 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
serve the people6 小时前
Prompts for Chat Models in LangChain
java·linux·langchain
一叶飘零_sweeeet6 小时前
不止于 API 调用:解锁 Java 工具类设计的三重境界 —— 可复用性、线程安全与性能优化
java·工具类
无聊的小坏坏7 小时前
从单 Reactor 线程池到 OneThreadOneLoop:高性能网络模型的演进
服务器·网络·一个线程一个事件循环