XML DSL (Splitter)

In some scenarios, the messages that we are processing may contain multiple elements and each of these may need to be handled in a completely different manner. In these situations, we use the Splitter pattern to allow us to separate a message into different parts and route each one individually.

In the following example, we see the implementation of a Splitter pattern to break a single composite message into separate pieces with each containing data related to a single item.

 

XML DSL Example

In the following route, we are consuming messages from an order folder. As it is read, we are employing XPATH to separate the XML payloads by line items within the Invoice.

<route> <from uri="jms:queue:order"/> <split> <xpath>/invoice/lineItems</xpath> <to uri="acct:processOrderLine"/> </split> </route>

 

Or, we can alternately use a regular expression to perform the message separation:

<route> <from uri="jms:queue:order"/> <split> <tokenizer token="([A-Z|0-9]*);" regex="true"/> <to uri="acct:processLineItems"/> </split> </route>

 

XML DSL using Bean Example

The use of Split will evaluate as an org.apache.camel.Expression to provide an iterable object that is able to produce each new individual message. Thus, it allows us to provide any kind of expression; such as a Bean invoked via a method call.

 

<split> <method bean="mySplitter" method="splitMessage"/> <to uri="acct:processLineItems"/> </split> <bean id="mySplitter" class="com.mycompany.MySplitter"/> public List splitMessage(String body) { // split using java code and return a List List items = ... return items; }