...
The Composed Message Processor from the EIP patterns allows you to process a composite message by splitting it up, routing the sub-messages to appropriate destinations and the re-aggregating the responses back into a single message.
Available in Camel 1.5.
In Camel we provide two solutions
- using both a Splitter and Aggregator EIPs
- using only a Splitter
The difference is when using only a Splitter it aggregates back all the splitted messages into the same aggregation group, eg like a fork/join pattern.
Whereas using the Aggregator allows you group into multiple groups, a pattern which provides more options.
Tip |
---|
Using the splitter alone is often easier and possibly a better solution. So take a look at this first, before involving the aggregator. |
Example using both Splitter and Aggregator
In this example we want to check that a multipart order can be filled. Each part of the order requires a check at a different inventory.
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ComposedMessageProcessorTest.java} |
Using the Spring XML Extensions
Code Block |
---|
<route>
<from uri="direct:start"/>
<split>
<simple>body</simple>
<choice>
<when>
<method bean="orderItemHelper" method="isWidget"/>
<to uri="bean:widgetInventory"/>
</when>
<otherwise>
<to uri="bean:gadgetInventory"/>
</otherwise>
</choice>
<to uri="seda:aggregate"/>
</split>
</route>
<route>
<from uri="seda:aggregate"/>
<aggregate strategyRef="myOrderAggregatorStrategy" completionTimeout="1000">
<correlationExpression>
<simple>header.orderId</simple>
</correlationExpression>
<to uri="mock:result"/>
</aggregate>
</route>
|
To do this we split up the order using a Splitter. The Splitter then sends individual OrderItems
to a Content Based Router which checks the item type. Widget items get sent for checking in the widgetInventory
bean and gadgets get sent to the gadgetInventory
bean. Once these OrderItems
have been validated by the appropriate bean, they are sent on to the Aggregator which collects and re-assembles the validated OrderItems
into an order again.
When an order is sent it contains a header with the order id. We use this fact when we aggregate, as we configure this .header("orderId")
on the aggregate
DSL to instruct Camel to use the header with the key orderId
as correlation expression.
For full details, check the example source here:
camel-core/src/test/java/org/apache/camel/processor/ComposedMessageProcessorTest.java
Example using only Splitter
In this example we want to split an incoming order using the Splitter eip, transform each order line, and then combine the order lines into a new order message.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java} |
Tip | ||
---|---|---|
| ||
If you use XML, then the <split> tag offers the strategyRef attribute to refer to your custom |
The bean with the methods to transform the order line and process the order as well:
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java} |
And the AggregationStrategy
we use with the Splitter eip to combine the orders back again (eg fork/join):
Wiki Markup |
---|
{snippet:id=e3|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java} |
Include Page | |||
---|---|---|---|
| |||
Include Page | |||
|