首页 > 解决方案 > Spring Integration 将文件移动到另一个文件夹,然后发送到 sftp 服务器

问题描述

我想创建一个应用程序,我将在其中收听名为“输入”的文件夹以获取文本文件,然后将文本文件移动到“输出”,然后将其发送到 sftp 服务器。这是我在 Spring Integration 中的代码。

@Bean
public IntegrationFlow textFileIntegration(@Value("${input.dir}") File in,
                                   @Value("${output.dir}") File out,
                                   MessageChannel sftpChannel) {

    return IntegrationFlows
            .from(Files.inboundAdapter(in)
                            .autoCreateDirectory(true)
                            .patternFilter("*.txt"),
                    sourcePollingChannelAdapterSpec ->
                            sourcePollingChannelAdapterSpec.poller(pollerFactory -> pollerFactory.fixedRate(1000)))
            //.transform(File.class, file -> service.process(file)) commented on purpose
            .handle(Files.outboundAdapter(out))
            .channel(sftpChannel)
            .get();
}

现在发生的事情是当我将文本文件放在“输入”目录中时,该文件然后成功移动到“输出”目录但发送到 sftp 通道不起作用。我尝试评论句柄方法和 sftp 通道将工作。我只想先将文件放到输出目录,然后再将其发送到 sftp。我在 Spring Integration DSL 中看到了一个“路由”功能,但不确定它是否适合使用。

先感谢您。

标签: javaspringspring-integration

解决方案


对于这样的流程,您应该使用 aFiles.outboundGateway()而不是 one-way Files.outboundAdapter()。最后一个问题是它不会产生要发送到下一个频道的回复。

另一个问题是在网关FileWritingMessageHandler模式下能够产生回复,因此它允许配置.setOutputChannel()

我想我们可以考虑拒绝这样的配置,如果它不是关于Files.outboundGateway().

请在参考手册中查看更多信息:https ://docs.spring.io/spring-integration/docs/current/reference/html/#file-writing-output-gateway


推荐阅读