0

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

amarzeet
  • 63
  • 2
  • 11

1 Answers1

1

Use a SpEL expression

new FileWritingMessageHandler(new SpelExpressionParser().parseExpression(
    "headers['someHeaderWithTheDestinationPath']"));
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I tried it and got the following exception: `org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'headers' cannot be found on null`. – Marc Tarin Jun 27 '22 at 15:18
  • That doesn't make any sense, it implies the message is `null` which should not be possible; I suggest you ask a new question showing your code and configuration. – Gary Russell Jun 27 '22 at 16:45
  • Of course, as soon as I read your comment, the error disappeared... I'll post a new question if ever I figure out what I did wrong the first time. Thanks. – Marc Tarin Jun 27 '22 at 22:38
  • Done: https://stackoverflow.com/q/72816425/5873923 – Marc Tarin Jun 30 '22 at 13:11