Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What Is Content-Based Routing With Mulesoft?

DZone's Guide to

What Is Content-Based Routing With Mulesoft?

Depending on a message's content, you can route it to any particular channel or destination that you'd like. Just start using content-based routing with Mulesoft.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Content-based routing is used to examine messages and route them to the correct channel or destination depending on a message's content. You should always use content-based routing when you want to route messages to the correct destination. Routing can be based on various criteria like the existence of fields, specific fields, etc.

For example, let's say you want to route the Purchase Order to different recipients on the basis of Customer ID. If Customer ID=1, then send the purchase order message to XYZ Limited via SFTP protocol. If Customer ID=2, then send the purchase order message to PQR Limited via HTTP. You can add more recipients depending on your requirements.

Anypoint Studio provided Choice Router to achieve content-based routing. Choice Router will always choose only one route. If no route matches, then the default route is used. 

Let's walk through how you can implement content-based routing with Anypoint Studio.

We will design solutions where we will get POST request over HTTP with query param CustID. Depending on the CustID value, the message will route to correct recipient.

Setting of Message Source 

Place the HTTP listener to the message source region in the Mule flow and configure the HTTP listener. You need to send CustID as a query param (i.e., http://localhost:8081/CBR?CustID=1).

Image titleStore Query Parameter to Flow Variable

You need to store the query parameter CustID in the flow variable so that you can use that flow variable for routing messages to the correct recipient.

Image title

Choice Router

Place the choice flow control after the variable transformer. Choice flow control will route the message to correct destination depending on the CustID value. The below table is shows how routes have been set up in choice flow control.

When Route Message to

#[flowVars.custID=='1']

File + Set Payload=Message sent to file location

#[flowVars.custID=='2']

JMS + Set Payload=Message sent to jms

Default

Set Payload = No destination found

Image title

Testing the Application

You can use Postman to test the application by sending requests to Mule flow. Make sure you are passing the CustID as a query param as it is an important parameter to decide the routing to correct destination.

Case 1

You can post HTTP messages to Mule flow and pass CustID=1 as the query param. This request will route to File and set the payload as "Message sent to file location."

Image title

Case 2

You can post the HTTP message to Mule flow and pass CustID=2 as a query param. This request will route to JMS Topic and set the payload as "Message sent to JMS."

Image titleCase 3

You can post the HTTP message to the Mule flow and pass CustID=3 (or any number other than 1 and 2) as the query param. This request will set the payload as "No destination found."

Image title

Code

<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
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"
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-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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<jms:activemq-connector name="Active_MQ" specification="1.1" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="contentbasedroutingFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/CBR" allowedMethods="POST" doc:name="HTTP"/>
<set-variable variableName="custID" value="#[message.inboundProperties.'http.query.params'.CustID]" doc:name="Variable"/>
<choice doc:name="Choice">
<when expression="#[flowVars.custID=='1']">
<file:outbound-endpoint responseTimeout="10000" doc:name="File" path="src/test/resources/PO"/>
<set-payload doc:name="Set Payload" value="Message sent to file location"/>
</when>
<when expression="#[flowVars.custID=='2']">
<jms:outbound-endpoint doc:name="JMS" connector-ref="Active_MQ" topic="apessentials"/>
<set-payload doc:name="Set Payload" value="Message sent to jms"/>
</when>
<otherwise>
<set-payload doc:name="Set Payload" value="No destination found"/>
</otherwise>
</choice>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="Error while processing the message" doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
</mule>

Now, you know how to use content-based routing with your Mule application!

Here is the video tutorial:


The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mulesoft ,tutorial ,integration ,message routing ,content-based routing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}