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

}

};

}

}

相关推荐
卓越小Y8 分钟前
配置jellyfin docker 硬件加速
java·spring cloud·docker
白萝卜弟弟10 分钟前
【JAVA】正则表达式中的捕获组和非捕获组
java·正则表达式
袁庭新31 分钟前
LuaRocks如何安装数据库驱动?
java·数据库·redis·lua·luarocks·袁庭新
hummhumm39 分钟前
第 10 章 - Go语言字符串操作
java·后端·python·sql·算法·golang·database
nukix1 小时前
Mac Java 使用 tesseract 进行 ORC 识别
java·开发语言·macos·orc
编程墨客1 小时前
第03章 文件编程
linux·运维·服务器
月光光心慌慌。1 小时前
新日撸java三百行` 新手小白java学习记录 `Day1
java
命里有定数1 小时前
windows工具 -- 使用rustdesk和云服务器自建远程桌面服务, 手机, PC, Mac, Linux远程桌面 (简洁明了)
linux·运维·服务器·windows·ubuntu·远程工作
蘑菇丁1 小时前
ranger-kms安装
java·ide·eclipse
XiaoLeisj1 小时前
【JavaEE初阶 — 多线程】内存可见性问题 & volatile
java·开发语言·java-ee