Using the Spring XML Extensions
For further examples of this pattern in use you could look at one of the junit test case
You can use the tokenizer _expression_ in the Spring DSL to split bodies or headers using a token. This is a common use-case, so we provided a special tokenizer tag for this.
In the sample below we split the body using a @ as separator. You can of course use comma or space or even a regex pattern, also set regex=true.
Splitting the body in Spring XML is a bit harder as you need to use the Simple language to dictate this
<split>
<simple>${body}</simple>
<to uri="mock:result"/>
</split>
What the Splitter returns
Camel 2.2 or older:
The Splitter will by default return the last splitted message.
Camel 2.3 and newer
The Splitter will by default return the original input message.
For all versions
You can override this by suppling your own strategy as an AggregationStrategy. There is a sample on this page (Split aggregate request/reply sample). Notice its the same strategy as the Aggregator supports. This Splitter can be viewed as having a build in light weight Aggregator.
Parallel execution of distinct 'parts'
If you want to execute all parts in parallel you can use special notation of split() with two arguments, where the second one is a boolean flag if processing should be parallel. e.g.
XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar");
from("activemq:my.queue").split(xPathBuilder, true).to("activemq:my.parts");
The boolean option has been refactored into a builder method parallelProcessing so its easier to understand what the route does when we use a method instead of true|false.
XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar");
from("activemq:my.queue").split(xPathBuilder).parallelProcessing().to("activemq:my.parts");
Stream based
![]() | Splitting big XML payloads The XPath engine in Java and saxon will load the entire XML content into memory. And thus they are not well suited for very big XML payloads.
Instead you can use a custom _expression_ which will iterate the XML payload in a streamed fashion. From Camel 2.9 onwards you can use the Tokenizer language
which supports this when you supply the start and end tokens. |
You can split streams by enabling the streaming mode using the streaming builder method.
from("direct:streaming").split(body().tokenize(",")).streaming().to("activemq:my.parts");
You can also supply your custom splitter to use with streaming like this:
import static org.apache.camel.builder.ExpressionBuilder.beanExpression;
from("direct:streaming")
.split(beanExpression(new MyCustomIteratorFactory(), "iterator"))
.streaming().to("activemq:my.parts")
Streaming big XML payloads using Tokenizer language
Available as of Camel 2.9
If you have a big XML payload, from a file source, and want to split it in streaming mode, then you can use the Tokenizer language with start/end tokens to do this with low memory footprint.
![]() | StAX component The Camel StAX component can also be used to split big XML files in a streaming mode. See more details at StAX. |
For example you may have a XML payload structured as follows
<orders>
<order>
<!-- order stuff here -->
</order>
<order>
<!-- order stuff here -->
</order>
...
<order>
<!-- order stuff here -->
</order>
</orders>
Now to split this big file using XPath would cause the entire content to be loaded into memory. So instead we can use the Tokenizer language to do this as follows:
from("file:inbox")
.split().tokenizeXML("order").streaming()
.to("activemq:queue:order");
In XML DSL the route would be as follows:
<route>
<from uri="file:inbox"/>
<split streaming="true">
<tokenize token="order" xml="true"/>
<to uri="activemq:queue:order"/>
</split>
</route>
Notice the tokenizeXML method which will split the file using the tag name of the child node, which mean it will grab the content between the <order> and </order> tags (incl. the tokens). So for example a splitted message would be as follows:
<order>
<!-- order stuff here -->
</order>
If you want to inherit namespaces from a root/parent tag, then you can do this as well by providing the name of the root/parent tag:
<route>
<from uri="file:inbox"/>
<split streaming="true">
<tokenize token="order" inheritNamespaceTagName="orders" xml="true"/>
<to uri="activemq:queue:order"/>
</split>
</route>
And in Java DSL its as follows:
from("file:inbox")
.split().tokenizeXML("order", "orders").streaming()
.to("activemq:queue:order");
Splitting files by grouping N lines together
Available as of Camel 2.10
The Tokenizer language has a new option group that allows you to group N parts together, for example to split big files into chunks of 1000 lines.
from("file:inbox")
.split().tokenize("\n", 1000).streaming()
.to("activemq:queue:order");
And in XML DSL
<route>
<from uri="file:inbox"/>
<split streaming="true">
<tokenize token="\n" group="1000"/>
<to uri="activemq:queue:order"/>
</split>
</route>
The group option is a number that must be a positive number that dictates how many groups to combine together. Each part will be combined using the token.
So in the example above the message being sent to the activemq order queue, will contain 1000 lines, and each line separated by the token (which is a new line token).
The output when using the group option is always a java.lang.String type.
Specifying a custom aggregation strategy
This is specified similar to the Aggregator.
Specifying a custom ThreadPoolExecutor
You can customize the underlying ThreadPoolExecutor used in the parallel splitter. In the Java DSL try something like this:
XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar");
ExecutorService pool = ...
from("activemq:my.queue")
.split(xPathBuilder).parallelProcessing().executorService(pool)
.to("activemq:my.parts");
Using a Pojo to do the splitting
As the Splitter can use any _expression_ to do the actual splitting we leverage this fact and use a method _expression_ to invoke a Bean to get the splitted parts.
The Bean should return a value that is iterable such as: java.util.Collection, java.util.Iterator or an array.
So the returned value, will then be used by Camel at runtime, to split the message.
![]() | Streaming mode and using pojo When you have enabled the streaming mode, then you should return a Iterator to ensure streamish fashion. For example if the message is a big file, then by using an iterator, that returns a piece of the file in chunks, in the next method of the Iterator ensures low memory footprint. This avoids the need for reading the entire content into memory. For an example see the source code for the TokenizePair implementation. |
In the route we define the _expression_ as a method call to invoke our Bean that we have registered with the id mySplitterBean in the Registry.
And the logic for our Bean is as simple as. Notice we use Camel Bean Binding to pass in the message body as a String object.
Split aggregate request/reply sample
This sample shows how you can split an Exchange, process each splitted message, aggregate and return a combined response to the original caller using request/reply.
The route below illustrates this and how the split supports a aggregationStrategy to hold the in progress processed messages:
And the OrderService bean is as follows:
And our custom aggregationStrategy that is responsible for holding the in progress aggregated message that after the splitter is ended will be sent to the buildCombinedResponse method for final processing before the combined response can be returned to the waiting caller.
So lets run the sample and see how it works.
We send an Exchange to the direct:start endpoint containing a IN body with the String value: A@B@C. The flow is:
HandleOrder: A
HandleOrder: B
Aggregate old orders: (id=1,item=A)
Aggregate new order: (id=2,item=B)
HandleOrder: C
Aggregate old orders: (id=1,item=A);(id=2,item=B)
Aggregate new order: (id=3,item=C)
BuildCombinedResponse: (id=1,item=A);(id=2,item=B);(id=3,item=C)
Response to caller: Response[(id=1,item=A);(id=2,item=B);(id=3,item=C)]
Stop processing in case of exception
Available as of Camel 2.1
The Splitter will by default continue to process the entire Exchange even in case of one of the splitted message will thrown an exception during routing.
For example if you have an Exchange with 1000 rows that you split and route each sub message. During processing of these sub messages an exception is thrown at the 17th. What Camel does by default is to process the remainder 983 messages. 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")
.split(body().tokenize(",")).stopOnException()
.process(new MyProcessor())
.to("mock:split");
And using XML DSL you specify it as follows:
<route>
<from uri="direct:start"/>
<split stopOnException="true">
<tokenize token=","/>
<process ref="myProcessor"/>
<to uri="mock:split"/>
</split>
</route>
Using onPrepare to execute custom logic when preparing messages
Available as of Camel 2.8
See details at Multicast
Sharing unit of work
Available as of Camel 2.8
The Splitter will by default not share unit of work between the parent exchange and each splitted exchange. This means each sub exchange has its own individual unit of work.
For example you may have an use case, where you want to split a big message. And you want to regard that process as an atomic isolated operation that either is a success or failure. In case of a failure you want that big message to be moved into a dead letter queue. To support this use case, you would have to share the unit of work on the Splitter.
Here is an example in Java DSL
Now in this example what would happen is that in case there is a problem processing each sub message, the error handler will kick in (yes error handling still applies for the sub messages). But what doesn't happen is that if a sub message fails all redelivery attempts (its exhausted), then its not moved into that dead letter queue. The reason is that we have shared the unit of work, so the sub message will report the error on the shared unit of work. When the Splitter is done, it checks the state of the shared unit of work and checks if any errors occurred. And if an error occurred it will set the exception on the Exchange and mark it for rollback. The error handler will yet again kick in, as the Exchange has been marked as rollback and it had an exception as well. No redelivery attempts is performed (as it was marked for rollback) and the Exchange will be moved into the dead letter queue.
Using this from XML DSL is just as easy as you just have to set the shareUnitOfWork attribute to true:
![]() | Implementation of shared unit of work So in reality the unit of work is not shared as a single object instance. Instead SubUnitOfWork is attached to their parent, and issues callback to the parent about their status (commit or rollback). This may be refactored in Camel 3.0 where larger API changes can be done. |
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.