DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. What Is Content-Based Routing With Mulesoft?

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.

Jitendra Bafna user avatar by
Jitendra Bafna
CORE ·
May. 15, 17 · Tutorial
Like (3)
Save
Tweet
Share
12.59K Views

Join the DZone community and get the full member experience.

Join For Free

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:


Flow (web browser) Database MuleSoft

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Open Source Is Much More Than Just a Free Tier
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Top Authentication Trends to Watch Out for in 2023
  • The Data Leakage Nightmare in AI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: