...
Code Block | ||||
---|---|---|---|---|
| ||||
<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:
Wiki Markup |
---|
{snippet:id=e1|lang=java|title=Animal|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Animal.java} |
Then we can create a deep clone processor which clones the message body:
Wiki Markup |
---|
{snippet:id=e1|lang=java|title=AnimalDeepClonePrepare|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/AnimalDeepClonePrepare.java} |
Then we can use the AnimalDeepClonePrepare class in the Multicast route using the onPrepare
option as shown:
Wiki Markup |
---|
{snippet:id=e1|lang=java|title=Multicast using onPrepare|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastOnPrepareTest.java} |
Notice the onPrepare
option is also available on other EIPs such as Splitter and Recipient List.
Include Page | ||||
---|---|---|---|---|
|