Apache Camel Splitter Example
1. Introduction
In this article, we will see an example of Splitter using Apache Camel. We will see an example of how we can use Apache Camel‘s Splitter to split an order into its items and process each item individually.
2. What is Splitter?
The Splitter from the EIP patterns allows you to split a message into a number of pieces and process them individually.
3. Technology Stack
In this example we will be using following technology stack:
- Maven 4.0 – Build and dependency tool. You can visit here for more details
- Apache Camel 2.15.2 – Open-source integration framework based on known Enterprise Integration Patterns.
- Spring 4.1.5.RELEASE – Comprehensive programming and configuration model for modern Java-based enterprise applications
- Spring Tool Suite (STS) – An Eclipse-based development environment that is customized for developing Spring applications.
4. Apache Camel Splitter Example
4.1 Dependencies
To continue using our example, we need to add the dependent jar files to the classpath. This can be achieved either by deploying directly the jar file or using the Maven. Since we are using Maven for our example we will be using the pom.xml for the dependency of the following jars:
- camel-core
- spring-context
- camel-spring
- spring-core
- camel-stream
Copy the below code and paste it onto the pom.xml file.
pom.xml
<dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.15.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>2.15.2</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-stream</artifactId> <version>2.15.2</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-camel</artifactId> <version>5.14.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> </dependencies>
4.2 Input Request
The below sample order XML request is used to send as an input message to the Apache Camel route.
order.xml
<?xml version="1.0" encoding="UTF-8"?> <orders> <order product="electronics"> <items> <item>Laptop</item> <item>Mobile</item> </items> </order> <order product="books"> <items> <item>Design Patterns</item> <item>XML</item> </items> </order> </orders>
4.3 Route XML File
In this sample, we are using the Spring XML Extensions to write the route logic for CamelContext. 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.
In our example, we are using one route. The route is accepting the input of order. XML file then separates the Items data from it based on the content, then it checks and sends the split message to the external file system.
orderxmlrouteContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <camelContext xmlns="http://camel.apache.org/schema/spring" > <route> <from uri="direct:start" /> <log message="Split by article Element"/> <split> <xpath>//order[@product='electronics']/items</xpath> <log message="${body}"/> <to uri="file:src/main/resources/orderxmlroute/" /> </split> </route> </camelContext> </beans>
4.4 Main Java Class
In this Java class, firstly, we create the instance of the ApplicationContext based for orderxmlrouteContext.xml file. Then we start the Camel context so that we can use the route java class. For our example, we have used the createProducerTemplate method of the created camel context’s instance, so that we can send the data to the route for processing. At last, we stop the instance of the Camel context.
orderxmlroute.java
package com.order; import java.io.FileInputStream; import java.io.InputStream; import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; import org.apache.camel.spring.SpringCamelContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class orderxmlroute { public static void main(String[] args) throws Exception { ApplicationContext appContext = new ClassPathXmlApplicationContext( "orderxmlrouteContext.xml"); CamelContext camelContext = SpringCamelContext.springCamelContext( appContext, false); try { camelContext.start(); ProducerTemplate template = camelContext.createProducerTemplate(); InputStream orderxml = new FileInputStream("src/main/resources/order.xml"); template.sendBody("direct:start", orderxml); } finally { camelContext.stop(); } } }
4.5 Output Request
The below XML request is the split output from the Apache Camel route.
<items> <item>Laptop</item> <item>Mobile</item> </items>
4.6 Console Output
Route: route1 started and consuming from: Endpoint[direct://start] Total 1 routes, of which 1 is started. Apache Camel 2.15.2 (CamelContext: camel-1) started in 0.901 seconds Apache Camel 2.15.2 (CamelContext: camel-1) is starting Total 1 routes, of which 1 is started. Apache Camel 2.15.2 (CamelContext: camel-1) started in 0.001 seconds Split by article Element Created default XPathFactory com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl@5e81e5ac <items> <item>Laptop</item> <item>Mobile</item> </items> Apache Camel 2.15.2 (CamelContext: camel-1) is shutting down Starting to graceful shutdown 1 routes (timeout 300 seconds) Route: route1 shutdown complete, was consuming from: Endpoint[direct://start] Graceful shutdown of 1 routes completed in 0 seconds Apache Camel 2.15.2 (CamelContext: camel-1) uptime 0.695 seconds Apache Camel 2.15.2 (CamelContext: camel-1) is shutdown in 0.002 seconds
5. Conclusion
Here in Apache Camel Splitter Example, we have learned how to split the message based on content route and then process it. So, now you are ready to implement the Splitter in Apache Camel applications.
6. Download the Spring Tool Suite Project
This was an example of Splitter using Apache Camel.
You can download the full source code of this example here: camelSplitXml.zip