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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • MDC Logging With MuleSoft Runtime 4.4
  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Managing Dynamic Application Properties in MuleSoft for CloudHub Applications
  • Enabling Business Transaction Monitoring for App Connect Enterprise

Trending

  • Web Crawling for RAG With Crawl4AI
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Infrastructure as Code (IaC) Beyond the Basics
  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.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Updated May. 15, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
13.6K 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.

Related

  • MDC Logging With MuleSoft Runtime 4.4
  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Managing Dynamic Application Properties in MuleSoft for CloudHub Applications
  • Enabling Business Transaction Monitoring for App Connect Enterprise

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!