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




XML project code:

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

<mule xmlns:http="" xmlns=""
       xmlns:xsi="" xsi:schemaLocation="">
       <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f5c2981b-49f5-4fe1-a84a-4daf08245ebe" >
              <http:listener-connection host="" port="8085" />
       <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="#[]">
                           <set-payload value="#[payload]" doc:name="Set Payload" doc:id="37201df9-8a31-4a97-bef8-4b62a3ce9070" />
                     <otherwise >
                           <set-payload value="default" doc:name="Set Payload" doc:id="b4624434-427e-44fb-a167-e31d01a20d71" />