Splitter has been edited by Torsten Mielke (Jun 02, 2008).

Change summary:

Wrong splitter example code.

(View changes)

Content:

Splitter

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

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").splitter(bodyAs(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").splitter(xpath("//foo/bar")).to("file://some/directory")

Using the Spring XML Extensions

<camelContext id="buildSplitter" xmlns="http://activemq.apache.org/camel/schema/spring">
    <route>
      <from uri="seda:a"/>
      <splitter>
        <recipients>
          <bodyAs class="java.lang.String"/>
          <tokenize token="
"/>
        </recipients>
      </splitter>
      <to uri="seda:b"/>
    </route>
  </camelContext>

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

Parallel execution of distinct 'parts'

If you want to execute all parts in parallel you can use special notation of splitter() with two arguments, where the second one is a flag if processing should be parallel. e.g.

XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar"); 
from("activemq:my.queue").splitter(xPathBuilder).to("activemq:my.parts");

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