Content based router using Mule ESB – EIP Series

Facebooktwittergoogle_plusredditpinterestlinkedinmail

EIP Series 1.1: Content Based Router using Mule ESB

Let’s start the EIP series with Mule ESB. As mentioned we are going to, implement a simple use case using ‘Content based router’.

Creating mule flow is always interesting because of Mule Studio. Everything is configurable using UI :)

Use case:

Let’s take a 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 inbound endpoint and four outbound endpoints for each category. Grab the content based router from the flow control window located at the right side of the Mule flow and drop it between the inbound and outbound endpoints. Name and configure all the endpoints. (Here we will take JMS Queues as our inbound and outbound endpoints, MuleConfig XML is given below)
  2. Configure the Content based router using XPath for fixing up filtering values. By double clicking it you will be getting this configuration UI.

Content Based Router Figure 1: Content Based Router – Mule Studio configuration

 

3. The complete flow should look similar to this.

Mule flow - Content based router

Figure 2: Mule flow – Content based router

4. The mule flow editor has the source view option where you can see the XML configuration of the flow. Refer the following code after you create the flow, even you can test flow by directly copying this code.

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

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"

xmlns:jms="http://www.mulesoft.org/schema/mule/jms"xmlns="http://www.mulesoft.org/schema/mule/core"

xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

xmlns:spring="http://www.springframework.org/schema/beans"version="CE-3.3.1"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

 http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd

 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd

 http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

 http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd ">

<jms:activemq-connectorname="Active_MQ"

brokerURL="tcp://localhost:61616"validateConnections="true"doc:name="Active MQ"/>

<flowname="CBR_SampleFlow1"doc:name="CBR_SampleFlow1">

<jms:inbound-endpointqueue="Input"

connector-ref="Active_MQ"doc:name="JMS"/>

<choicedoc:name="Choice">

<whenexpression="/stock/items/category/text()='Books'"

evaluator="xpath">

<processor-chain>

<jms:outbound-endpointqueue="Books"

connector-ref="Active_MQ"doc:name="Books"/>

</processor-chain>

</when>

<whenexpression="/stock/items/category/text()='Mobiles'"

evaluator="xpath">

<processor-chain>

<jms:outbound-endpointqueue="Mobiles"

connector-ref="Active_MQ"doc:name="Mobiles"/>

</processor-chain>

</when>

<whenexpression="/stock/items/category/text()='Toys'"

evaluator="xpath">

<processor-chain>

<jms:outbound-endpointqueue="Toys"

connector-ref="Active_MQ"doc:name="Toys"/>

</processor-chain>

</when>

<whenexpression="/stock/items/category/text()='Clothing'"

evaluator="xpath">

<processor-chain>

<jms:outbound-endpointqueue="Clothing"

connector-ref="Active_MQ"doc:name="Clothing"/>

</processor-chain>

</when>

<otherwise>

<processor-chain>

<echo-componentdoc:name="Echo"/>

</processor-chain>

</otherwise>

</choice>

</flow>

</mule>

 

Testing:

Send the input message provided below to the Input queue. (As per the given Mule 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 its respective queues.

Input XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- BusHorn - CBR using Mule ESB - EIP Series 1.1 -->
<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>