java - Spring Integration FTP 使用后删除本地文件(Spring Boot)

标签 java spring spring-boot spring-integration

我正在尝试编写一个程序,该程序可以通过 ftp 从一台服务器获取文件并通过 ftp 将其放置在另一台服务器上。但是,我在写入本地文件后删除本地文件时遇到问题。只要它是临时的,能够在本地保存它不是问题。我尝试将 ExpressionEvaluatingRequestHandlerAdvice 与 OnSuccessExpression 一起使用,但我无法让它实际使用该表达式。代码在这里:

@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 channel 设为 PublishSubscribeChannel 并添加一个简单的订阅者:

@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用于根据负载删除本地文件。

关于java - Spring Integration FTP 使用后删除本地文件(Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780158/

相关文章:

java - 初始化具有内部类类型字段的bean

java - 带有 @Transactional 注释的 Spring OpenSessionInViewFilter

spring-boot - 带有嵌入式 Tomcat 的 Spring Boot 中的 Log4J2

java - 使用 @InjectMocks 将 @Autowired 字段替换为模拟实现

java - 每个上下文的 Log4J

java - 在 Tomcat7 服务器上用 Java 创建新实例时出现 XmlPullParser 异常

java - 如何验证 Spring Data 填充的 Controller 输入中的域对象?

java - 如何使用 Cucumber runner 加载 Spring 应用程序上下文

java - Spring Boot 中内容类型为 application/x-www-form-urlencoded 的请求的自定义反序列化器

java - 与具有路径变量的单个端点相比,拥有单独的端点是否有优势?