Apache Camel

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:

  1. Maven 4.0 – Build and dependency tool. You can visit here for more details
  2. Apache Camel 2.15.2 – Open-source integration framework based on known Enterprise Integration Patterns.
  3. Spring 4.1.5.RELEASE – Comprehensive programming and configuration model for modern Java-based enterprise applications
  4. 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.



4.2 Input Request

The below sample order XML request is used to send as an input message to the Apache Camel route.


<?xml version="1.0" encoding="UTF-8"?>
	<order product="electronics">
	<order product="books">
			<item>Design Patterns</item>

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.


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        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" >
			<from uri="direct:start" />
			<log message="Split by article Element"/>
				<log message="${body}"/>
				<to uri="file:src/main/resources/orderxmlroute/"  />


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.


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(
    CamelContext camelContext = SpringCamelContext.springCamelContext(
            appContext, false);
    try {            
        ProducerTemplate template = camelContext.createProducerTemplate();
        InputStream orderxml = new FileInputStream("src/main/resources/order.xml");
        template.sendBody("direct:start", orderxml);
    } finally {



4.5 Output Request

The below XML request is the split output from the Apache Camel route.


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
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

Simranjit Singh

Simranjit Singh has graduated from Computer Science Department of the Guru Nanak Dev University of Amritsar, Punjab, India. He also holds a Master degree in Software Engineering from the Birla Institute of Technology & Science of Pilani, Rajasthan, India. He works as a Senior Consultant in the e-commerce sector where he is mainly involved with projects based on Java and Big Data technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button