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 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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Flow Control in Mule 4
  • Explore Salesforce OAuth Authorization Flows and Its Use Cases
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • Build a Flow Collectibles Portal Using Cadence (Part 2)

Trending

  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Why Documentation Matters More Than You Think

Content-Based Routing Using Mule Choice Flow Control

Learn what content-based routing is, when it's used in Mule flows, and how to use the choice flow control for routing messages.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Sep. 16, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
26.8K 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. We use content-based routing when we want to route messages to the right destination.

Choice Flow Control

In Mule, the choice flow control is used to achieve content-based routing where routing will be done based on the message content, like inbound properties, flow variables, and payload. A choice router will always choose only one route. If no route matches, then the default route is used. In a choice router, we use the MEL expression to specify the path for the message.

The choice flow control adds conditional programming to a flow; it is similar to an if/else code block in Java.

Use Case

Let’s take an application use case here. When we are expecting a message or payload to be received from the many inbound sources like file channel, FTP channel, and a messaging queue, and based on the type of the source, we want to process the message accordingly, we can apply the choice flow control in the Mule flow.

Let’s walk through how to use the choice flow control in an application.

Search for the Composite Source component in the Mule Palette and drag and drop it to the source region of the flow. Now we can drag and drop multiple connectors under the composite source (i.e. wrap multiple connectors with the composite source). Here, we will use one HTTP Listener and one File Connector as inbound endpoints and one JMS (ActiveMQ) endpoint to receive messages from different sources. Now, grab the choice flow control from the Mule Palette and drag and drop it after the composite source component. The choice flow control dynamically routes messages based on message payload or properties. Use the attribute "expression" of the choice flow control to filter the values based on the content of the message.

Flow

Image title


Code

<mule xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
 xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http"
 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.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
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">
<file:connector name="File" autoDelete="true" doc:name="File" />
 <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" basePath="/composite" doc:name="HTTP Listener Configuration" />
 <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ">
  <receiver-threading-profile maxThreadsActive="200" maxBufferSize="100" />
 </jms:activemq-connector>
 <flow name="compositeSourceFlow">
  <composite-source doc:name="Composite Source">
   <file:inbound-endpoint connector-ref="File" path="/file/in/processing/" moveToDirectory="/file/data/processed" doc:name="File">
    <file:filename-wildcard-filter pattern="*.xml" caseSensitive="false" />
   </file:inbound-endpoint>
   <http:listener config-ref="HTTP_Listener_Configuration" path="/composite" doc:name="HTTP"> </http:listener>
   <jms:inbound-endpoint queue="myqueue1" connector-ref="Active_MQ" doc:name="JMS">
    <jms:selector expression="JMSPriority = 4" />
   </jms:inbound-endpoint>
  </composite-source>
  <logger level="INFO" message="#[message]" doc:name="Logger"></logger>
  <choice doc:name="Choice">
   <when expression="#[message.inboundProperties['http.query.params']]">
    <logger level="INFO" message="Message is triggered from the Http Source"
     doc:name="Logger"></logger>
    <byte-array-to-string-transformer
     doc:name="Byte Array to String" />
   </when>
   <when expression="#[message.inboundProperties['originalDirectory']]">
    <logger level="INFO" message="Message is triggered from the File Channel" doc:name="Logger"></logger>
    <file:file-to-string-transformer doc:name="File to String" />
   </when>
   <when expression="#[message.inboundProperties['JMSPriority']]">
    <logger level="INFO" message="Message is triggered from the JMS call" doc:name="Logger"></logger>
    <byte-array-to-string-transformer doc:name="Byte Array to String" />
   </when>
   <otherwise>
    <logger level="INFO" message="Message is triggered from the Unknown Source" doc:name="Logger"></logger>
   </otherwise>
  </choice>
 </flow>
</mule>

Hope this helps.

Thanks.

Keep learning.

Flow control (data) Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Flow Control in Mule 4
  • Explore Salesforce OAuth Authorization Flows and Its Use Cases
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • Build a Flow Collectibles Portal Using Cadence (Part 2)

Partner Resources

×

Comments

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: