Search code examples
spring-integrationspring-integration-dsl

Spring integration Java DSL: How to share header variable inside one integration flow run


The pseudocode of my flow is

    @Bean
    private IntegrationFlow myChannel() {
    return f -> f
            .enrichHeaders(h -> h.header("x", "y", true))
            .split(...)
            ...
            ..handle("myHandler", "doMyWork")
            ...
            .enrichHeaders(h -> h.header("x", "z", true))
}

First the header "x" is set to the value "y". Then messages are split and for the first message the header is set to the value "z". When the second message comes to the method doMyWork of the handler myHandler the header "x" has value "y". I want that value to be "z".

So how to share the header value inside one integration flow run? I want that value is shared only inside one specific integration flow, because there can be multiple flows running same time.


Solution

  • You are misunderstanding the concepts; EIP method's only operate on the current message; all split messages will inherit the value from the parent (pre-split) message, each message will then get z later in the flow.

    Each message gets its own set of headers, which are immutable.

    You could make the header a mutable value (e.g. AtomicReference<String>) - but I would not advise that, it would make things very difficult to debug.

    Perhaps you could store some state in a bean instead of trying to use message headers this way.