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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • How to Migrate From LangChainGo to Genkit Go
  • Build a Voice Assistant With Gemini Live API
  • Solving Comprehension Debt in AI-Generated Codebases

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated May. 15, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook