Composed Message Processor

Camel supports the Composed Message Processor from the EIP patterns book.

How can you maintain the overall message flow when processing a message consisting of multiple elements, each of which may require different processing?

image

Use Composed Message Processor to process a composite message. The Composed Message Processor splits the message up, routes the sub-messages to the appropriate destinations and re-aggregates the responses back into a single message.

With Camel this pattern is implemented by the Splitter which has built-in aggregation to re-aggregate the responses back into a single message.

Sample

This sample uses the Splitter as composed message processor to process each splitted message, aggregate and return a combined single response.

The route and the code comments below explains how you can use the Splitter to split each message to sub-message which are processed individuallay and then combined back into a single response message via the custom `aggregationStrategy (MyOrderStategy), which then are the output of the splitter, that are then further processed at the end of the route.

// this routes starts from the direct:start endpoint
// the body is then splitted based on @ separator
// the splitter in Camel supports InOut as well and for that we need
// to be able to aggregate what response we need to send back, so we provide our
// own strategy with the class MyOrderStrategy.
from("direct:start")
    .split(body().tokenize("@"), new MyOrderStrategy())
        // each splitted message is then send to this bean where we can process it
        .to("bean:MyOrderService?method=handleOrder")
        // this is important to end the splitter route as we do not want to do more routing
        // on each splitted message
    .end()
    // after we have splitted and handled each message we want to send a single combined
    // response back to the original caller, so we let this bean build it for us
    // this bean will receive the result of the aggregate strategy: MyOrderStrategy
    .to("bean:MyOrderService?method=buildCombinedResponse")

More details

See the Splitter EIP.