Content-based routing is a widely used architectural pattern that is particularly useful for handling incoming messages or requests that need to be distributed based on certain criteria or content. In this pattern, the content of a message is evaluated to determine the appropriate destination or processing logic for that message.
MuleSoft is an enterprise service bus (ESB) platform that provides a powerful set of tools for implementing content-based routing. In this blog, we will explore how to use MuleSoft to implement content-based routing. We will cover the following topics:
- Overview of Content-Based Routing
- Building a MuleSoft Application for Content-Based Routing
- Implementing Content-Based Routing with MuleSoft
- Example Use Cases for Content-Based Routing with MuleSoft
Let’s dive in!
Overview of Content-Based Routing
Content-based routing is a design pattern that allows messages to be distributed to different endpoints based on the content of the message. This is typically accomplished by evaluating a message’s content against a set of predefined rules or criteria, which are then used to determine the appropriate destination for that message.
For example, imagine you are building a web application that allows users to post comments on articles. When a user submits a comment, the application needs to decide whether to send that comment to a moderation queue for review or to display it immediately on the website. This decision can be based on the content of the comment itself, such as whether it contains inappropriate language or violates community guidelines.
Content-based routing can also be used to implement different processing logic based on the content of a message. For example, a message containing a customer order may need to be processed differently based on the customer’s shipping location or the type of products being ordered.
Building a MuleSoft Application for Content-Based Routing
MuleSoft provides a comprehensive set of tools for building integration applications. These applications typically consist of a set of flows that process incoming messages, transform data as necessary, and route messages to different endpoints based on their content.
To build a MuleSoft application for content-based routing, you will typically start by defining the different endpoints or processing logic that you want to use based on the content of incoming messages. You will then define a set of rules or criteria that can be used to evaluate the content of those messages and determine the appropriate destination.
Implementing Content-Based Routing with MuleSoft
MuleSoft provides several built-in components that can be used to implement content-based routing. The most commonly used component is the Choice Router, which allows you to define a set of rules that can be used to evaluate the content of incoming messages.
Here is an example of how to implement content-based routing using the Choice Router in a MuleSoft flow:

<flow name="contentBasedRoutingFlow"> <http:listener path="/comments" method="POST" config-ref="HTTP_Listener_config" doc:name="/comments"/> <set-variable variableName="comment" value="#[payload]" doc:name="comment"/> <choice> <when expression="#[vars.comment contains 'badword']"> <logger message="Comment contains bad word: #[vars.comment]" level="INFO" doc:name="Comment contains bad word"/> <jms:publish doc:name="PublishMessage to Queue" doc:id="ca2f8a47-a5f2-46a0-91c1-b5a3dbaf5bd3" config-ref="JMS_Config" destination="testQueue"/> </when> <otherwise> <logger message="Comment approved: #[vars.comment]" level="INFO" doc:name="Comment approved"/> <http:request config-ref="HTTP_Request_configuration" path="/api/comments" method="POST" doc:name="HTTP API Call"/> </otherwise> </choice> </flow>
In this example, we are using an HTTP listener to receive incoming requests to the /comments
endpoint. We then set a variable called comment
to the contents of the incoming request payload.
Next, we use the Choice Router to evaluate the content of the comment
variable. We use an expression to check if the variable comment contains the word “badword”. If it does, we log a message and send the comment to a JMS queue for moderation. If the comment does not contain the word “badword”, we log a message and send the comment to an API endpoint for display on the website.
Another example for content-based routing
Routing Incoming Emails to the Appropriate Team
Imagine you are building an email integration application that receives emails from customers and routes them to the appropriate team for processing. You want to evaluate the contents of each email and route it to the appropriate team based on the nature of the request.
To implement this with MuleSoft, you can set up an email inbound endpoint that receives incoming emails. You can then use content-based routing to evaluate the contents of each email and route it to the appropriate team.
For example, you can use a choice router to evaluate the subject line of each email. If the subject line contains the word “billing,” you can route the email to the billing team for processing. If the subject line contains the word “technical,” you can route the email to the technical support team. If the subject line contains the word “sales,” you can route the email to the sales team.
You can also use content-based routing to evaluate the body of each email and route it to the appropriate team based on the contents of the email. For example, you can evaluate the body of each email for keywords such as “refund,” “installation,” or “upgrade” and route the email to the appropriate team based on the contents.
By using content-based routing to route incoming emails to the appropriate team, you can ensure that each email is processed by the team best equipped to handle it, improving the efficiency and effectiveness of your customer support operations.
Code Example
Here’s an example MuleSoft flow that implements content-based routing to route incoming emails to the appropriate team based on the contents of the email:
<flow name="EmailRouter"> <email:listener-imap doc:name="On New Email - IMAP" doc:id="6d82fc81-d761-4a07-be13-2119aedab750" config-ref="Email_IMAP"> <scheduling-strategy> <cron expression="*"/> </scheduling-strategy> </email:listener-imap> <choice doc:name="Check Email Subject"> <when expression="#[payload.subject contains 'sales']"> <jms:publish doc:name="salesQueue" doc:id="0c42ea9a-bfb9-4e8c-9fe7-5dc057e2a7f9" config-ref="JMS_Config" destination="salesQueue"/> </when> <when expression="#[payload.subject contains 'technical']"> <jms:publish doc:name="technicalQueue" doc:id="cc130ead-f16f-4c29-830a-0f8a7605b668" config-ref="JMS_Config" destination="technicalQueue"/> </when> <when expression="#[payload.subject contains 'billing']"> <jms:publish doc:name="billingQueue" doc:id="213117e0-1786-49ea-a0ed-98157e4dff3b" config-ref="JMS_Config" destination="billingQueue"/> </when> <otherwise> <logger message="Email does not match any criteria" doc:name="Email does not match any criteria"/> </otherwise> </choice> </flow>
In this example, we use an
to receive incoming emails. We then use a email:listener-imap
choice
router to evaluate the subject line of each email and route it to the appropriate JMS queue based on the contents of the subject line.
If the subject line contains the word “billing,” we route the email to the billingQueue
. If the subject line contains the word “technical,” we route the email to the technicalQueue
. If the subject line contains the word “sales,” we route the email to the salesQueue
. If the email does not match any of the criteria, we log a message to indicate that the email does not match any criteria.
These are just simple examples, but the Choice Router can be used to implement much more complex content-based routing logic. You can use any MuleSoft component within a choice route to define your processing logic, including calling other flows, invoking APIs, or sending messages to different queues.
Example Use Cases for Content-Based Routing with MuleSoft
Content-based routing can be used in a wide range of use cases. Here are a few examples of how content-based routing can be used with MuleSoft:
Routing Messages Based on Geographic Location
Imagine you are building an e-commerce application that sells products to customers in different regions around the world. You want to route customer orders to different processing logic based on the customer’s geographic location. You can use content-based routing to evaluate the shipping address in each customer order and route the order to the appropriate processing logic based on the customer’s location.
Moderating User-Generated Content
As we saw in the earlier example, content-based routing can be used to moderate user-generated content such as comments or posts. You can evaluate the content of each message or post and route it to a moderation queue if it violates certain guidelines or contains inappropriate language.
Processing Customer Support Requests
Content-based routing can also be used to route customer support requests to different support teams based on the nature of the request. For example, you can evaluate the contents of an incoming support request and route it to the appropriate team based on the customer’s product, support tier, or issue type.
Conclusion
Content-based routing is a powerful architectural pattern that can be used to implement complex processing logic based on the content of incoming messages. MuleSoft provides a powerful set of tools for implementing content-based routing, including the Choice Router and a wide range of other components for integrating with different endpoints and services. By using MuleSoft to implement content-based routing, you can build flexible and scalable integration applications that can handle a wide range of use cases.