Choice Router

mani mulesoft
3 min readAug 2, 2021

--

Introduction:

The choice flow control dynamically routes message 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.

Use Case:

If you want to login into any web portal, the user needs to pass username and password that user name and password validate against the database and check whether the user exists or not in the web portal/database.

If customerName and customerPassword exists in the Database it will route the flow login success direction.

If is user not exists in the database our flow will going into the default direction. Get the error message “Please enter correct username and password”.

Anypoint Studio provided Choice Router to achieve condition-based routing.

Choice Router will always choose only one route. If no route matches, then the default route is used.

Let’s start the how you can implement content-based routing with Anypoint Studio.

We will design solutions where we will get POST request over HTTP with query customerName=mani and customerpassword=112233. Depending on the operation value, the message will route to correct recipient.

Choice Router flow:

Here, we will use one HTTP Listener and one Database Connector. the choice router from the Mule Palette and drag and drop it after the database component. The choice flow control dynamically routes messages based on message payload or properties.

Choice Router flow

Testing:

You can send HTTP request using Postman to test the application. Make sure you are passing the customerName and customerpassword as a query param as it is an important parameter to decide the routing to correct destination.

Case1: You can post HTTP messages pass the customerName=mani and customerpassword=112233 as the query param.

Testing Case 1

Output: Based on the input choice router route the flow into login success direction.

Status: 200,

message: “Login success”

Case2:

You can post HTTP messages pass the customerName= Nani and customerpassword=112233 as the query param.

Testing Case 2

Output: If choice router not found any match condition then it will route the flow the flow into default one.

Status: 1001,

message: “Please enter correct username and password”

Conclusion:

We can use choice router for routing message flow like if you want login in social media account it will ask credential if credential match it will show the home page otherwise it will land the flow into login page with error message.

Reference:

https://docs.mulesoft.com/mule-runtime/4.3/choice-router-concept

https://docs.mulesoft.com/mule-runtime/3.9/choice-flow-control-reference

--

--

mani mulesoft

Certified MuleSoft (Mule 4) Developer & Dell Boomi Associate Developer | Software Engineer