Splitter has been edited by Claus Ibsen (Feb 18, 2009).

Change summary:

CAMEL-1344

(View changes)

Content:

Splitter

The Splitter from the EIP patterns allows you split a message into a number of pieces and process them individually

As of Camel 2.0, you need to specify a Splitter as split(). In earlier versions of Camel, you need to use splitter().

Example

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

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").split(body(String.class).tokenize("\n")).to("seda:b");
    }
};

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.

from("activemq:my.queue").split(xpath("//foo/bar")).convertBodyTo(String.class).to("file://some/directory")

Using the Spring XML Extensions

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="seda:a"/>
    <split>
      <xpath>/invoice/lineItems</xpath>
      <to uri="seda:b"/>
    </split>
  </route>
</camelContext>

For further examples of this pattern in use you could look at one of the junit test case

Using Tokenizer from Spring XML Extensions
Avaiaible as of Camel 2.0

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.

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:start"/>
        <split>
            <tokenizer token="@"/>
            <to uri="mock:result"/>
        </split>
    </route>
</camelContext>

Message Headers

The following headers is set on each Exchange that are split:

header type description
org.apache.camel.splitCounter int A split counter that increases for each Exchange being split. The counter starts from 0.
org.apache.camel.splitSize int The total number of Exchanges that was splitted. This header is not applied for stream based

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");

In Camel 2.0 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

Available as of Camel 1.5

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");

Specifying a custom aggregation strategy

Available as of Camel 2.0

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"); 
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
from("activemq:my.queue").split(xPathBuilder, true, threadPoolExecutor).to("activemq:my.parts");

In the Spring DSL try this:

Available as of Camel 1.6.0

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:parallel-custom-pool"/>
    <split threadPoolExecutorRef="threadPoolExecutor">
      <xpath>/invoice/lineItems</xpath>
      <to uri="mock:result"/>
    </split>
  </route>
</camelContext>

<!-- There's an easier way of specifying constructor args, just can't remember it
     at the moment... old Spring syntax will do for now! -->
<bean id="threadPoolExecutor" class="java.util.concurrent.ThreadPoolExecutor">
  <constructor-arg index="0" value="8"/>
  <constructor-arg index="1" value="16"/>
  <constructor-arg index="2" value="0"/>
  <constructor-arg index="3" value="MILLISECONDS"/>
  <constructor-arg index="4"><bean class="java.util.concurrent.LinkedBlockingQueue"/></constructor-arg>
</bean>

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.

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.

from("direct:start")
        // here we use a POJO bean mySplitterBean to do the split of the payload
        .split().method("mySplitterBean")
        .to("mock:result");

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.

public class MySplitterBean {

    /**
     * The split method returns something that is iteratable such as a java.util.List.
     *
     * @param body the payload of the incoming message
     * @return a list containing each part splitted
     */
    public List split(String body) {
        // since this is based on an unit test you can of couse
        // use different logic for splitting as Camel have out
        // of the box support for splitting a String based on comma
        // but this is for show and tell, since this is java code
        // you have the full power how you like to split your messages
        List answer = new ArrayList();
        String[] parts = body.split(",");
        for (String part : parts) {
            answer.add(part);
        }
        return answer;
    }
}

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.

Reply via email to