java - Spring Integration FTP 使用后删除本地文件(Spring Boot)
问题描述
我正在尝试编写一个程序,该程序可以通过 ftp 从一台服务器获取文件并通过 ftp 将其放置在另一台服务器上。但是,我在写入后删除本地文件时遇到问题。只要它是暂时的,就可以在本地保存它不是问题。我曾尝试使用带有 OnSuccessExpression 的 ExpressionEvaluatingRequestHandlerAdvice,但我无法让它实际使用该表达式。代码在这里:
@Configuration
@EnableConfigurationProperties(FTPConnectionProperties.class)
public class FTPConfiguration {
private FTPConnectionProperties ftpConnectionProperties;
public FTPConfiguration(FTPConnectionProperties ftpConnectionProperties) {
this.ftpConnectionProperties = ftpConnectionProperties;
}
@Bean
public SessionFactory<FTPFile> ftpInputSessionFactory() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost(ftpConnectionProperties.getInputServer());
sf.setUsername(ftpConnectionProperties.getInputFtpUser());
sf.setPassword(ftpConnectionProperties.getInputFtpPassword());
return new CachingSessionFactory<>(sf);
}
@Bean
public SessionFactory<FTPFile> ftpOutputSessionFactory() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost(ftpConnectionProperties.getOutputServer());
sf.setUsername(ftpConnectionProperties.getOutputFtpUser());
sf.setPassword(ftpConnectionProperties.getOutputFtpPassword());
return new CachingSessionFactory<>(sf);
}
@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpInputSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(true);
fileSynchronizer.setRemoteDirectory(ftpConnectionProperties.getInputDirectory());
fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.TIF"));
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "input", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> ftpMessageSource() {
FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
source.setLocalDirectory(new File("ftp-inbound"));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), ""));
return source;
}
@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler handler() {
FtpMessageHandler handler = new FtpMessageHandler(ftpOutputSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(ftpConnectionProperties.getOutputDirectory()));
handler.setFileNameGenerator(message -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
});
return handler;
}
}
它完全按照预期处理远程文件,从源中删除远程文件并放入输出,但在使用后不删除本地文件。
解决方案
我建议您将该input
频道设为 aPublishSubscribeChannel
并添加一个更简单的订阅者:
@Bean
public PublishSubscribeChannel input() {
return new PublishSubscribeChannel();
}
@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler handler() {
...
}
@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler deleteLocalFileService() {
return m -> ((File) message.getPayload()).delete();
}
这样,带有File
有效负载的相同消息将首先发送给您FtpMessageHandler
,然后才发送给这个新消息,以便deleteLocalFileService
根据有效负载删除本地文件。
推荐阅读
- global-variables - AUTOSAR SWC 全局变量
- wcf - 如何使用 wcf 服务创建多个数据合同类
- c# - 循环遍历类中的字符串
- javascript - 将图像内的坐标转换为图形
- python - 从视频流中执行接近实时的人员检测的问题
- javascript - Mobx 可观察映射到 js 数组
- ssis - SSIS foeach 容器正在跳过子文件夹
- hyperledger-fabric - 是否可以在 Hyperledger 中定义对查询的访问控制?
- apache - htaccess阿帕奇。如何允许通过 ip 访问管理员的 url?
- r - `_bookdown.yml`、`_output.yml` 和第一个文档的 yaml 标头有什么区别?