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.


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=""
	xmlns:tracking="" xmlns=""
	xmlns:spring="" version="EE-3.5.1"
	<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"
			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" />
				expression="#[(flowVars.operation=='db' || flowVars.operation=='database')]">
				<component class="com.roytuts.mule.choice.routing.RouteToDatabase"
					doc:name="RouteToDatabase" />
			<when expression="#[flowVars.operation=='queue']">
				<component class="com.roytuts.mule.choice.routing.RouteToQueue"
					doc:name="RouteToQueue" />
				<set-payload value="#[message.payload] is an Invalid Operation"
					doc:name="Invalid Operation" />

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 *