You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Multicast

The 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.

Example

The 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

Error formatting macro: snippet: java.lang.NullPointerException

By default Multicast invokes each endpoint sequentially. If parallel processing is desired, simply use

from("direct:a").multicast().parallelProcessing().to("direct:x", "direct:y", "direct:z");

In case of using InOut MEP, an AggregationStrategy is used for aggregating all reply messages. The default is to only use the latest reply message and discard any earlier replies. The aggregation strategy is configurable:

from("direct:start")
  .multicast(new MyAggregationStrategy())
  .parallelProcessing().timeout(500).to("direct:a", "direct:b", "direct:c")
  .end()
  .to("mock:result");

Stop processing in case of exception

Available 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.
For example if you want to multicast to 3 destinations and the 2nd destination fails by an exception. What Camel does by default is to process the remainder destinations. You have the chance to remedy or handle this in the AggregationStrategy.

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 onPrepare to execute custom logic

Available as of Camel 2.8

The Multicast will copy the source Exchange and multicast each copy. However the copy is a shallow copy, so in case you have mutateable message bodies, then any changes will be visible by the other copied messages. If you want to use a deep clone copy then you need to use a custom onPrepare which allows you to do this using the Processor interface.

For example if you have a mutable message body as this Animal class:

Error formatting macro: snippet: java.lang.NullPointerException

Then we can create a deep clone processor which clones the message body:

Error formatting macro: snippet: java.lang.NullPointerException

Then we can use the AnimalDeepClonePrepare class in the Multicast route using the onPrepare option as shown:

Error formatting macro: snippet: java.lang.NullPointerException

Notice the onPrepare option is also available on other EIPs such as Splitter and Recipient List.

Using This Pattern

If 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.

  • No labels