Spring Integration, delete file in outbound channel adapter

帅比萌擦擦* 提交于 2019-12-24 05:48:32

问题


I am using Spring Integration to poll a directory for a File, process this file in a service class, write this file to an output directory and then delete the original file.

I have the following XML configuration:

  <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"/>

This polls the file, passes it to my processing_service and copies it to the outbound directory. However the original file is not being deleted. Does anyone have any idea as to why not?


回答1:


I know that the question was asked a long time ago but maybe the answer will be useful to someone else.

The reason why the input file is not deleted is provided in the 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.

Your message does not contain this particular header. If you use one of the standard file transformers (FileToStringTransformer and FileToByteArrayTransformer) it will be set automatically. Alternatively you can set it manually using a header enricher.

Behind the scenes something like this is happening in the file transformers:

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



回答2:


From the documentation http://static.springsource.org/spring-integration/reference/html/files.html

<int-file:outbound-gateway id="mover" request-channel="moveInput"
reply-channel="output"
directory="${output.directory}"
mode="REPLACE" delete-source-files="true"/>

I don't know how to do this on the inbound-channel-adapter(which I think makes sense)



来源:https://stackoverflow.com/questions/12707149/spring-integration-delete-file-in-outbound-channel-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!