[CONF] Apache Camel: Splitter (page edited)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[CONF] Apache Camel: Splitter (page edited)

Dhiraj Bokde (Confluence)

Splitter has been edited by Jonathan Anstey (Nov 05, 2008).

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

Using the Spring XML Extensions

<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
  <route>
    <from uri="seda:a"/>
    <splitter>
      <xpath>/invoice/lineItems</xpath>
      <to uri="seda:b"/>
    </splitter>
  </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 boolean flag if processing should be parallel. e.g.

XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar"); 
from("activemq:my.queue").splitter(xPathBuilder, true).to("activemq:my.parts");
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").splitter(xPathBuilder, true, threadPoolExecutor).to("activemq:my.parts");

In the Spring DSL try this:

<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
  <route>
    <from uri="direct:parallel-custom-pool"/>
    <splitter threadPoolExecutorRef="threadPoolExecutor"> 
      <xpath>/invoice/lineItems</xpath>
      <to uri="mock:result"/>
    </splitter>
  </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</value></constructor-arg>
  <constructor-arg index="1"><value>16</value></constructor-arg>
  <constructor-arg index="2"><value>0</value></constructor-arg>
  <constructor-arg index="3"><value>MILLISECONDS</value></constructor-arg>
  <constructor-arg index="4"><bean class="java.util.concurrent.LinkedBlockingQueue"/></constructor-arg>
</bean>

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.