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.
Join the DZone community and get the full member experience.
Join For FreeContent-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
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.
Opinions expressed by DZone contributors are their own.
Comments