Spring Integration,删除出站 channel 适配器中的文件

标签 spring integration delete-file channel outbound

我正在使用 Spring Integration 轮询目录中的文件,在服务类中处理该文件,将该文件写入输出目录,然后删除原始文件。

我有以下 XML 配置:

  <int-file:inbound-channel-adapter id="filesInChannel"
                                    directory="file:${java.io.tmpdir}/input" 
                                    auto-create-directory="true"  >
      <int:poller id="poller" fixed-delay="1000" />
  </int-file:inbound-channel-adapter>

  <int:service-activator id="servicActivator"
                         input-channel="filesInChannel"
                         output-channel="filesOut"
                         ref="my_file_processing_service">
  </int:service-activator>

  <int-file:outbound-channel-adapter  id="filesOut" auto-create-directory="true" delete-source-files="true" directory="file:${java.io.tmpdir}/output"/>

这会轮询文件,将其传递到我的processing_service 并将其复制到出站目录。但是原始文件并未被删除。有人知道为什么不吗?

最佳答案

我知道这个问题很久以前就被问过,但也许答案对其他人有用。

Spring Integration Reference中提供了输入文件未被删除的原因:

The delete-source-files attribute will only have an effect if the inbound Message has a File payload or if the FileHeaders.ORIGINAL_FILE header value contains either the source File instance or a String representing the original file path.

您的邮件不包含此特定 header 。如果您使用 standard file transformers 之一(FileToStringTransformerFileToByteArrayTransformer)它将自动设置。或者,您可以使用 header enricher 手动设置它.

Behind the scenes文件转换器中正在发生类似的事情:

...
Message<?> transformedMessage = MessageBuilder.withPayload(result)
        .copyHeaders(message.getHeaders())
        .setHeaderIfAbsent(FileHeaders.ORIGINAL_FILE, file)
        .setHeaderIfAbsent(FileHeaders.FILENAME, file.getName())
        .build();
...

关于Spring Integration,删除出站 channel 适配器中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12707149/

相关文章:

paypal - 测试 Paypal Express 集成/沙盒凭证/我不是程序员

Windows命令提示符删除文件夹中的文件

java - 将参数传递给 Spring MethodInvokingFactoryBean 参数列表

java - Spring Boot 的多重构造函数注入(inject)歧义

java - Spring propertyConfigurer 不工作

api - BambooHR API 未提供完整的员工详细信息

java - 尝试实现 AWS SDK 时出现异常

osgi - Liferay 7 - OSGi 组件 Web 控制台 (Apache Felix) 集成

c# - 如何从网络共享删除中回收文件/文件夹

c++ - C++中的文件删除