One of the more commonly used EIP’s in Camel is the Splitter, you can find the documentation here http://camel.apache.org/splitter.html
Usually the splitter is used for tokenizing some message or splitting collections into single messages. But what if you need something more specific? It is good to know that the splitter can either split on Java collection types as well as Iterators. In this blog we are going to create a custom Iterator helper class and method. The business use of this example will be rather questionable, but since we have the opportunity to fully integrate a custom Java method the potential is limitless.
The first thing to do is create a Class and method which return an Iterator.
import javax.inject.Named; import java.util.Iterator; import java.util.NoSuchElementException; /** * Created by pim on 6/30/17. */ @Named("myCustomIterator") public class MyCustomIterator { public Iterator getIterator(Exchange exchange) { final String exampleMsg = exchange.getIn().getBody(String.class); if (exampleMsg == null) throw new NullPointerException(); return new Iterator<Character>() { private int index = 0; public boolean hasNext() { return index < exampleMsg.length(); } public Character next() { if (!hasNext()) throw new NoSuchElementException(); return exampleMsg.charAt(index++); } public void remove() { throw new UnsupportedOperationException(); } }; } }
Once we have our custom Iterator we can incorporate it in our Camel route and more specifically use it in our splitter.
package nl.rubix.eos.camel; import javax.inject.Inject; import javax.inject.Named; import org.apache.camel.Endpoint; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.cdi.ContextName; import org.apache.camel.cdi.Uri; /** * Configures all our Camel routes, components, endpoints and beans */ @ContextName("myIteratorSplitter") public class MyIteratorSplitterRoute extends RouteBuilder { @Inject @Named("myCustomIterator") MyCustomIterator myCustomIterator; @Override public void configure() throws Exception { // you can configure the route rule with Java DSL here from("direct:start") .log("${body}") .split().method("myCustomIterator", "getIterator") .log("${body}") .end(); } }
Now when we run this example with the string “camelisawesome” we see the following log entries:
2017-06-30 14:18:35,830 [main ] INFO DefaultCamelContext - Route: route1 started and consuming from: Endpoint[direct://start] 2017-06-30 14:18:35,830 [main ] INFO DefaultCamelContext - Total 1 routes, of which 1 are started. 2017-06-30 14:18:35,835 [main ] INFO DefaultCamelContext - Apache Camel 2.17.0.redhat-630187 (CamelContext: myIteratorSplitter) started in 0.329 seconds 2017-06-30 14:18:35,849 [main ] INFO Bootstrap - WELD-ENV-002003: Weld SE container camel-context-cdi initialized 2017-06-30 14:18:35,877 [main ] INFO route1 - camelisawesome 2017-06-30 14:18:35,887 [main ] INFO route1 - c 2017-06-30 14:18:35,887 [main ] INFO route1 - a 2017-06-30 14:18:35,888 [main ] INFO route1 - m 2017-06-30 14:18:35,888 [main ] INFO route1 - e 2017-06-30 14:18:35,889 [main ] INFO route1 - l 2017-06-30 14:18:35,889 [main ] INFO route1 - i 2017-06-30 14:18:35,889 [main ] INFO route1 - s 2017-06-30 14:18:35,890 [main ] INFO route1 - a 2017-06-30 14:18:35,890 [main ] INFO route1 - w 2017-06-30 14:18:35,891 [main ] INFO route1 - e 2017-06-30 14:18:35,891 [main ] INFO route1 - s 2017-06-30 14:18:35,891 [main ] INFO route1 - o 2017-06-30 14:18:35,892 [main ] INFO route1 - m 2017-06-30 14:18:35,893 [main ] INFO route1 - e 2017-06-30 14:18:35,898 [main ] INFO CamelContextProducer - Camel CDI is stopping Camel context [myIteratorSplitter]
Using the custom Iterator enables you to create a custom splitter.