Content based router using Apache Camel – EIP Series


EIP Series 1.2: Content Based Router using Apache Camel

Let’s create routes in Apache Camel for EIP series by implementing a simple use case using ‘Content based router’

Its Apache Camel now. No confusions in creating the camel context, because its all routes here. Routes have a ‘from’ address and ‘to’ address. That’s the only simple logic for creating routes.

Use case:

Let’s take the same retail use case here. When a bunch of stocks arrive at the retail store, the retailer has to segregate based on the product categories. The store will have separate database for every category. Let’s take four categories: Books, Toys, Clothes & Mobiles. For every message coming in, the retailer has to forward the message based on its category. Each message will have a category description.

Let’s use the ‘Content based router (CBR)’ in place of the retailer.


  1. Create one ‘from’ endpoint and four ‘to’ endpoints for each category. Place the ‘Choice’ component between the components, it works very similar to a ‘If-else’ structure. (Here we will take JMS Queues as our from and in endpoints, Camel context XML is given below)
  2. Configure the Choice component using XPath for fixing up filtering values.
  3. The camel context flow will look similar to the code mentioned below. Even you can test flow by directly copying this code.
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""



<camelContext id="camel" xmlns="">


<from uri="activemq:queue:input"/>




<to uri="activemq:queue:Books"/>




<to uri="activemq:queue:Toys"/>




<to uri="activemq:queue:Clothes"/>




<to uri="activemq:queue:Mobiles"/>



<to uri="activemq:queue:Dump"/>





<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">

<property name="brokerURL" value="tcp://localhost:61616"/>





Send the input message provided below to the Input queue. (As per the given Camel configuration)

You can use the ActiveMQ web console to send and monitor messages here http://localhost:8161/admin

Figure 3: Queue monitor – Message being sent to ‘Books’ queue. Figure 3: Queue monitor – Message being sent to ‘Books’ queue


The input message contains book information; you can notice that the message is being sent to ‘Books’ queue. This is because the category is set to ‘Books’, try changing the category to Toys or Clothes or Mobiles. You should be able to notice that the messages are being sent to its respective queues.

Input XML:Add Media

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- BusHorn - CBR using Apache Camel - EIP Series 1.2 -->




<name>Life of PI</name>






Hope CBR routes your messages to the proper destination.

Happy Integration !


Gnanaguru Sattanathan

Gnanaguru Sattanathan

I am a Integration developer and a open source enthusiast. My areas of interest includes Enterprise messaging & SOA. Twitter: @tallguru

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>