java - 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 中看到了一个“路由”功能,但不确定它是否适合使用。
先感谢您。
解决方案
对于这样的流程,您应该使用 aFiles.outboundGateway()
而不是 one-way Files.outboundAdapter()
。最后一个问题是它不会产生要发送到下一个频道的回复。
另一个问题是在网关FileWritingMessageHandler
模式下能够产生回复,因此它允许配置.setOutputChannel()
我想我们可以考虑拒绝这样的配置,如果它不是关于Files.outboundGateway()
.
请在参考手册中查看更多信息:https ://docs.spring.io/spring-integration/docs/current/reference/html/#file-writing-output-gateway
推荐阅读
- php - PHP将新行字符串存储到数组中
- azure-blob-storage - 使用 PowerShell 将 JSON 文件直接保存到 Blob 存储
- python - 如何创建一个包含两个子列表的字典?
- javascript - 用于字母数字字符和数字范围组合的正则表达式
- c++ - 如何正确实现 std::all_of 函数以验证字符串的一部分?
- mysql - 任何人都可以向我解释有什么问题吗?以及如何解决?
- regex - 组合多个正则表达式
- python - Keras CONV1D:检查目标时出错:预期解码输出的形状为 (50, 50) 但得到的数组形状为 (50, 1)
- latex - 如何在投影仪定理环境中更改文本格式?
- python - 尝试将数据从表单传递到 Django 中的数据库时出现 NoReverseMatch 错误