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.

Example

This example uses the Split EIP as composed message processor to process each split message, aggregate and return a single combined response.

The route and the code comments below explain how you can use the Split EIP to split each message to sub-message which are processed individually and then combined back into a single response message via the custom AggregationStrategy (myStrategy), as the output from the Split EIP.

  • Java

  • XML

  • YAML

// this routes starts from the direct:start endpoint
// the body is then split 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 which has been registered as a bean
// with id myStrategy.
from("direct:start")
    .split(simple("${split(@)}")).aggregationStrategy("myStrategy")
        // each split 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 split message
    .end()
    // after we have split 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
    .to("bean:MyOrderService?method=buildCombinedResponse")
<route>
    <from uri="direct:start"/>
    <split aggregationStrategy="myStrategy">
        <simple>${split(@)}</simple>
        <to uri="bean:MyOrderService?method=handleOrder"/>
    </split>
    <to uri="bean:MyOrderService?method=buildCombinedResponse"/>
</route>
- route:
    from:
      uri: direct
      parameters:
        name: start
      steps:
        - split:
            aggregationStrategy: myStrategy
            expression:
              simple:
                expression: "${split(@)}"
            steps:
              - to:
                  uri: bean
                  parameters:
                    beanName: MyOrderService
                    method: handleOrder
        - to:
            uri: bean
            parameters:
              beanName: MyOrderService
              method: buildCombinedResponse

More details

See the Splitter EIP.