Search code examples
springspring-bootspring-integrationspring-integration-sftp

Preappending line to new file in Spring Integration


Hi I've designed a integration flow in Spring Integration where I read from a file from SFTP and write to a file locally. I've set the fileExistsMode to APPEND-MODE but I'm wondering if it is possible to pre-append a string to the file before writing the contents of the remote file?

Example,

Remote file contains following content:

bla bla bla
bla bla

Local file will be written like this:

myLine <-- my pre-appended line
bla bla bla
bla bla

Solution

  • That fully looks like interceptor feature.

    So, you implement ChannelInterceptor.preSend() for the channel on the FileWritingMessageHandler. In the code you check for the file exists() (based on the message).

    If not, you create one and append that first like.

    Return message as is and go to the FileWritingMessageHandler.