splitapache-camelenterprise-integration

Split a Camel Route according to type


I have an Apache Camel exchange that, as result of some processing, contains a Pair<List<Message>, List<Error>>. I want to split my Camel route into two paths, one only processing the Messages, and one only processing the Errors. Using a pseudo-DSL, I am looking for something like this:

split(MyPairSplitter::Class)
  .to("direct:process-messages")
  .to("direct:process-errors")

Now, this is not how the splitter EIP works. To my understanding, the splitter creates a sequence of exchanges that all have the same data type as body and that are all fed into a single route; but I would like to have two routes that process different data types. What is the best way to do this?


Solution

  • The Splitter doesn't really care what the type of the thing being split is. I think what you're looking for is the Composed Message Processor pattern.

    split(body())
        .when(simple("${body.class.name == 'Message'"))
            .to("direct:process-messages")
        .otherwise()   
            .to("direct:process-errors")
        .end()
    .end()