2

My application looks like this: Read in files, route them around for a bit, transform them to a POJO and back, then write them to disk again.

I'm using a FileWritingMessageHandler to write out my files and set

handler.setDeleteSourceFiles(true);

Now this works fine for the files i do not transform to a POJO, but i'm having some JSON files that i transform to POJO and later on back to a JSON file that i also want to write back and delete the old ones. I'm setting the OriginalFile Header and checking it before and after transforming it POJO back to JSON and it is the correct path, but it doesn't get deleted.

Does FileWritingMessageHandler ignore the header if it sees a message containing a file as payload? If so, how do i get it to delete the sourcefile without manually deleting it?

Edit

I looked a bit deeper at the implementation of FileWritingMessageHandler and it does indeed ignore the header if it's a File object. So i just have to manually delete it.

Blue
  • 601
  • 2
  • 8
  • 19

1 Answers1

1

Yes, you investigation is correct. See the code:

if (payload instanceof File) {
    resultFile = this.handleFileMessage((File) payload, tempFile, resultFile);
}

So, FileWritingMessageHandler originalFileFromHeader in this case because it does not make sense, because we already have an original File as payload.

After it does this:

if (this.deleteSourceFiles) {
    if (sourceFile.renameTo(resultFile)) {
        return resultFile;
    }

So, you file in payload will be removed anyway.

But since it isn't the same file as in your headers, it really isn't removed automatically. For this purpose you can use something like this:

<file:outbound-channel-adapter>
    <file:request-handler-advice-chain>
        <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
             <property name="onSuccessExpressionString" value="headers[file_originalFile].delete()"/>
        </bean>
    </file:request-handler-advice-chain>
</file:outbound-channel-adapter>
cactuschibre
  • 1,908
  • 2
  • 18
  • 36
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118