MulticastThe Multicast allows to route the same message to a number of endpoints and process them in a different way. The main difference between the Multicast and Splitter is that Splitter will split the message into several pieces but the Multicast will not modify the request message. ExampleThe following example shows how to take a request from the direct:a endpoint , then multicast these request to direct:x, direct:y, direct:z. Using the Fluent Builders from("direct:a").multicast().to("direct:x", "direct:y", "direct:z"); If you want to aggregate the whatever the incoming messages or outgoing messages within the multicast, here is an example Using the Fluent Builders // The message will be sent parallelly to the endpoints from("direct:parallel") .multicast(new BodyOutAggregatingStrategy(), true).executorService(tpExecutor) .to("direct:x", "direct:y", "direct:z"); // Multicast the message in a sequential way from("direct:sequential").multicast(new BodyOutAggregatingStrategy()).to("direct:x", "direct:y", "direct:z"); from("direct:x").process(new AppendingProcessor("x")).to("direct:aggregator"); from("direct:y").process(new AppendingProcessor("y")).to("direct:aggregator"); from("direct:z").process(new AppendingProcessor("z")).to("direct:aggregator"); from("direct:aggregator").aggregate(header("cheese"), new BodyInAggregatingStrategy()). completionPredicate(property(Exchange.AGGREGATED_SIZE).isEqualTo(3)).to("mock:result"); For further examples of this pattern in use you could look at one of the junit test case Stop processing in case of exceptionAvailable as of Camel 2.1 The Multicast will by default continue to process the entire Exchange even in case one of the multicasted messages will thrown an exception during routing. But sometimes you just want Camel to stop and let the exception be propagated back, and let the Camel error handler handle it. You can do this in Camel 2.1 by specifying that it should stop in case of an exception occurred. This is done by the stopOnException option as shown below:
from("direct:start")
.multicast()
.stopOnException().to("direct:foo", "direct:bar", "direct:baz")
.end()
.to("mock:result");
from("direct:foo").to("mock:foo");
from("direct:bar").process(new MyProcessor()).to("mock:bar");
from("direct:baz").to("mock:baz");
And using XML DSL you specify it as follows:
<route>
<from uri="direct:start"/>
<multicast stopOnException="true">
<to uri="direct:foo"/>
<to uri="direct:bar"/>
<to uri="direct:baz"/>
</multicast>
<to uri="mock:result"/>
</route>
<route>
<from uri="direct:foo"/>
<to uri="mock:foo"/>
</route>
<route>
<from uri="direct:bar"/>
<process ref="myProcessor"/>
<to uri="mock:bar"/>
</route>
<route>
<from uri="direct:baz"/>
<to uri="mock:baz"/>
</route>
Using This PatternIf you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out. |