Choice Router in Mule 4 | Choice in Mule 4

Choice Router in Mule 4
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.

Below is Choice in mule 4:

 
In above example if input payload is equal to 1 then it moves to first branch otherwise print to default.

URL: http://localhost:8085/choice
Method: POST

Input:


Output:

 


XML project code:

<?xml version="1.0" encoding="UTF-8"?>

<mule 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f5c2981b-49f5-4fe1-a84a-4daf08245ebe" >
              <http:listener-connection host="0.0.0.0" port="8085" />
       </http:listener-config>
       <flow name="test_choiceFlow" doc:id="49a32c7c-5f0f-4e65-938d-4922614f7f09" >
              <http:listener doc:name="Listener" doc:id="78aa218d-be76-4611-b813-0335a8b24206" config-ref="HTTP_Listener_config" path="/choice"/>
              <choice doc:name="Choice" doc:id="1fc0ccf2-8e15-4cac-a1d1-fc094c608259" >
                     <when expression="#[payload.id==1]">
                           <set-payload value="#[payload]" doc:name="Set Payload" doc:id="37201df9-8a31-4a97-bef8-4b62a3ce9070" />
                     </when>
                     <otherwise >
                           <set-payload value="default" doc:name="Set Payload" doc:id="b4624434-427e-44fb-a167-e31d01a20d71" />
                     </otherwise>
              </choice>
       </flow>
</mule>