Splitter

Inbound messages that are being transmitted have the potential to contain multiple elements. For example, the message could be for an order containing information on multiple line items within the order and each line item has the potential to be handled differently than the others. Therefore, we need to have the ability to separate the content into these distinct categories.

 

We can utilize the Splitter integration pattern to separate the composite message into a set of smaller individualized messages containing information pertaining to a single specific item.

Splitter Types

The Apache Camel splitter actually supports two patterns, as follows:

  • Simple splitter — implements the splitter pattern on its own.

  • Splitter/aggregator — combines the splitter pattern with the aggregator pattern, such that the pieces of the message are recombined after they have been processed.

Splitter Shallow Copy

Before the Splitter begins the process of separating the original message into parts, it will make a shallow copy of the original message. In this shallow copy, the headers and payload of the original message are copied as references only. Although the splitter is not involved in routing the resulting message parts to different endpoints, parts of the split message might undergo secondary routing.

Splitter using XML DSL

This pattern can utilize a tokenizer as the driving force for separating content, in this example we use the new line character for this purpose:

<route> <from uri="direct:input"/> <split> <tokenize token="\n"/> <to uri="direct:output"/> </split> </route>

Split into Groups

To split a big file into chunks of 1000 lines, you can define a splitter route as follows

<route> <from uri="file:inbox"/> <split streaming="true"> <tokenize token="\n" group="1000"/> <to uri="activemq:queue:order"/> </split> </route>

Skip First Item

To skip the first item in the message you can use the skipFirst option.

<route> <from uri="file:inbox"/> <split streaming="true"> <tokenize token="\n" group="1000" skipFirst="true" /> <to uri="activemq:queue:order"/> </split> </route>