我正在使用 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 theFileHeaders.ORIGINAL_FILE
header value contains either the source File instance or a String representing the original file path.
您的邮件不包含此特定 header 。如果您使用 standard file transformers 之一(FileToStringTransformer
和 FileToByteArrayTransformer
)它将自动设置。或者,您可以使用 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/