Search code examples
spring-integration

Spring Integration and HeaderExistsRouter


Spring Integration 5 here. Looking to define a router in the Java DSL (not XML) that routes messages 2 ways based on the existence of a header:

  • if the header exists, go to channel chan-a
  • else if the header does not exist on the message, go to error-channel

At first glance HeaderValueRouter seems to be the right tool for the job, but that seems to be just for routing messages for a given header to various channels based on their values. I need something that's more like a...HeaderExistsRouter. What are my options here?


Solution

  • Something like "router lambda" and mapping based on its result:

    .route(Message.class, m -> m.getHeaders().containsKey("foo"),
                            r -> r.channelMapping(true, "chan-a")
                                    .channelMapping(false, "error-channel"))
    

    However your condition sounds more like a filter() which really does its stuff based on true-false evaluation. See its discardChannel() option for your error-channel use-case. The regular true result would just simply lead into that chan-a. And that's it: doesn't look like we need to bother more complicated router configuration.