How to write files dynamically into subdirectory by FileWritingMessageHandler

430 Views Asked by At

I have to write files into multiple subdirectories based on a header attribute. Not getting a way to configure it in Spring Integration.

@Bean
@ServiceActivator(inputChannel = "processingChannel")
public MessageHandler processingDirectory() {
    FileWritingMessageHandler handler = new FileWritingMessageHandler(new File("some-path"));
    handler.setFileExistsMode(FileExistsMode.REPLACE);
    handler.setExpectReply(false);
    handler.setPreserveTimestamp(true);
    handler.setTemporaryFileSuffix(".writing");
    handler.setAutoCreateDirectory(true);
    return handler;
}

This bean receives a file along with some headers attributes i.e. type="abc" from "processingChannel" . Files are written successfully into some-path. But my requirement is to write into somepath/abc or somepath/xyz location based on "type" value

1

There are 1 best solutions below

4
On

Use a SpEL expression

new FileWritingMessageHandler(new SpelExpressionParser().parseExpression(
    "headers['someHeaderWithTheDestinationPath']"));