The Splitter from the EIP patterns allows you split a message into a number of pieces and process them individually
You need to specify a Splitter as
split(). In earlier versions of Camel, you need to use
Refers to an AggregationStrategy to be used to assemble the replies from the sub-messages, into a single outgoing message from the Splitter. See the defaults described below in What the Splitter returns. From Camel 2.12 onwards you can also use a POJO as the
AggregationStrategy, see the Aggregate page for more details.
Camel 2.12: This option can be used to explicit declare the method name to use, when using POJOs as the
AggregationStrategy. See the Aggregate page for more details.
Camel 2.12: If this option is
false then the aggregate method is not used for the very first splitted message. If this option is
null values is used as the
oldExchange (for the very first message splitted), when using POJOs as the
AggregationStrategy. See the Aggregate page for more details.
If enabled then processing the sub-messages occurs concurrently. Note the caller thread will still wait until all sub-messages has been fully processed, before it continues.
Refers to a custom Thread Pool to be used for parallel processing. Notice if you set this option, then parallel processing is automatically implied, and you do not have to enable that option as well.
Camel 2.2: Whether or not to stop continue processing immediately when an exception occurred. If disable, then Camel continue splitting and process the sub-messages regardless if one of them failed. You can deal with exceptions in the AggregationStrategy class where you have full control how to handle that.
If enabled then Camel will split in a streaming fashion, which means it will split the input message in chunks. This reduces the memory overhead. For example if you split big messages its recommended to enable streaming. If streaming is enabled then the sub-message replies will be aggregated out-of-order, eg in the order they come back. If disabled, Camel will process sub-message replies in the same order as they where splitted.
Camel 2.5: Sets a total timeout specified in millis. If the Recipient List hasn't been able to split and process all replies within the given timeframe, then the timeout triggers and the Splitter breaks out and continues. Notice if you provide a TimeoutAwareAggregationStrategy then the
timeout method is invoked before breaking out. If the timeout is reached with running tasks still remaining, certain tasks for which it is difficult for Camel to shut down in a graceful manner may continue to run. So use this option with a bit of care. We may be able to improve this functionality in future Camel releases.
Camel 2.8: Refers to a custom Processor to prepare the sub-message of the Exchange, before its processed. This allows you to do any custom logic, such as deep-cloning the message payload if that's needed etc.
Camel 2.8: Whether the unit of work should be shared. See further below for more details.
The following properties are set on each Exchange that are split:
A split counter that increases for each Exchange being split. The counter starts from 0.
The total number of Exchanges that was splitted. This header is not applied for stream based splitting. From Camel 2.9 onwards this header is also set in stream based splitting, but only on the completed Exchange.
Camel 2.4: Whether or not this Exchange is the last.
The following example shows how to take a request from the queue:a endpoint the split it into pieces using an Expression, then forward each piece to queue:b
Using the Fluent Builders
The splitter can use any Expression language so you could use any of the Languages Supported such as XPath, XQuery, SQL or one of the Scripting Languages to perform the split. e.g.
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
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.
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.
You can split streams by enabling the streaming mode using the
streaming builder method.
You can also supply your custom splitter to use with streaming like this:
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.
For example you may have a XML payload structured as follows
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:
In XML DSL the route would be as follows:
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> tags (incl. the tokens). So for example a splitted message would be as follows:
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:
And in Java DSL its as follows:
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.
And in XML DSL
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
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:
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.
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:
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
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:
And using XML DSL you specify it as follows:
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:
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.