Search code examples
javaspringspring-integrationspring-integration-dsl

FileWritingMessageHandler - adding timestamp suffix to file


I have an issue on adding timestamp suffixes to files using spring integration.

Here is my FileWritingMessageHandler bean :

 public FileWritingMessageHandler getFileWritingMessageHandler(String directory) {
    FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(directory));
    handler.setFileExistsMode(FileExistsMode.REPLACE);
    handler.setExpectReply(false);
    handler.setDeleteSourceFiles(true);
    return handler;
}

I tried with handler.setTemporaryFileSuffix(getCurrentTimeStamp()); but it does nothing

I tried also with a DefaultFileNameGenerator :

    DefaultFileNameGenerator suffixFileNameGenerator = new DefaultFileNameGenerator();
    suffixFileNameGenerator.setHeaderName("id");
    suffixFileNameGenerator.setExpression("payload.name + '"+ getCurrentTimeStamp()+"'");
    handler.setFileNameGenerator(suffixFileNameGenerator);

A timestamp is added but it's the same for all files. They are processed at different times so I would like to append that time to the file name. How can I achieve that ?

Thanks in advance for your help


Solution

  • You are getting the time once and adding it as a literal to the expression.

    You need to get the timestamp at runtime instead of bean initialization time; use the T operator to invoke a static method:

    suffixFileNameGenerator.setExpression("payload.name + T(System).currentTimeMillis()");