Content based router using Apache Camel – EIP Series

Facebooktwittergoogle_plusredditpinterestlinkedinmail

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.

Idea:

  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="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 id="camel" xmlns="http://camel.apache.org/schema/spring">

<route>

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

<choice>

<when>

<xpath>/stock/items/category/text()='Books'</xpath>

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

</when>

<when>

<xpath>/stock/items/category/text()='Toys'</xpath>

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

</when>

<when>

<xpath>/stock/items/category/text()='Clothes'</xpath>

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

</when>

<when>

<xpath>/stock/items/category/text()='Mobiles'</xpath>

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

</when>

<otherwise>

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

</otherwise>

</choice>

</route>

</camelContext>

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

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

</bean>


</beans>

 

Testing:

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

<stock>

<items>

<category>Books</category>

<name>Life of PI</name>

<rate>300</rate>

</items>

</stock>

 

 

Hope CBR routes your messages to the proper destination.

Happy Integration !

 

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