Choice Flow Control in Mule ESB

The choice flow control dynamically routes messages based on message payload or properties. It adds conditional programming to a flow, similar to an if/then/else code block.

A choice flow control uses expressions to evaluate the content of a message, then it routes the message to one of the routing options within its scope. It directs messages to the first routing option in the scope that matches the routing configurations (evaluates to true). If none of expressions evaluate to true, the choice flow control directs the message to the default (else) route.

Example

Step 1. Create below classes which will be used as Java Components.

package com.roytuts.mule.choice.routing;
public class RouteToFile {
	public String send(String payload) {
		return payload + " successfully sent to File";
	}
}
package com.roytuts.mule.choice.routing;
public class RouteToDatabase {
	public String send(String payload) {
		return payload + " successfully sent to Database";
	}
}
package com.roytuts.mule.choice.routing;
public class RouteToQueue {
	public String send(String payload) {
		return payload + " successfully sent to Queue";
	}
}

Step 2. Create the mule-choice-flow.xml file under src/main/app directory and put the below source code.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
	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" version="EE-3.5.1"
	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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
	<flow name="mule-choice-flow" doc:name="mule-choice-flow">
		<http:inbound-endpoint exchange-pattern="request-response"
			host="localhost" port="8081" doc:name="HTTP" />
		<set-variable variableName="operation"
			value="#[message.inboundProperties.'http.query.params'.operation]"
			doc:name="Set Operation Variable" />
		<choice doc:name="Choice">
			<when expression="#[flowVars.operation=='file']">
				<component class="com.roytuts.mule.choice.routing.RouteToFile"
					doc:name="RouteToFile" />
			</when>
			<when
				expression="#[(flowVars.operation=='db' || flowVars.operation=='database')]">
				<component class="com.roytuts.mule.choice.routing.RouteToDatabase"
					doc:name="RouteToDatabase" />
			</when>
			<when expression="#[flowVars.operation=='queue']">
				<component class="com.roytuts.mule.choice.routing.RouteToQueue"
					doc:name="RouteToQueue" />
			</when>
			<otherwise>
				<set-payload value="#[message.payload] is an Invalid Operation"
					doc:name="Invalid Operation" />
			</otherwise>
		</choice>
	</flow>
</mule>

It looks like the image shown below in Mule Studio

mule choice flow control

Step 3. Running the application

Now do a right-click on the mule-choice-flow.xml file or on the mule project and click on Run As -> Mule Application. Then you will see something like below in Console when the application is running

**********************************************************************
* Application: mule                                                  *
* OS encoding: Cp1252, Mule encoding: UTF-8                          *
*                                                                    *
* Agents Running:                                                    *
*   DevKit Extension Information                                     *
*   Batch module default engine                                      *
*   Clustering Agent                                                 *
*   JMX Agent                                                        *
**********************************************************************

Step 4. Now open the browser and hit the below URLs

mule choice flow control


mule choice flow control


mule choice flow control


mule choice flow control

Thanks for reading.

2 thoughts on “Choice Flow Control in Mule ESB

Leave a Reply

Your email address will not be published. Required fields are marked *