Contact Us 1-800-596-4880

Choice (<choice/>)

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Route messages dynamically through a flow according to a set of DataWeave expressions that evaluate message content.

Each expression is associated with a different routing option. The effect is to add conditional processing to a flow, similar to an if/then/else code block in most programming languages.

Only one of the routes in the Choice router executes, meaning that the first expression that evaluates to true triggers that route’s execution and the others are not checked. If none of the expressions are true, then the default route executes.

To configure the Choice router, specify one or more conditions to evaluate, and the message processors that execute when any condition evaluates to true. Additionally, configure a default route that executes when none of the defined conditions are true.

Component XML

This component supports the following XML structure:

<choice doc:name="Choice" doc:id="qwolaa" tracking:enable-default-events="true" >
  <when expression=${expressionToEvaluate}> (1)
    <!-- Message processors --> (2)
  </when>
  <otherwise>
    <!-- Message processors --> (3)
  </otherwise>
</choice>
1 The expression attribute has the condition to evaluate.
2 Add any number of message processors to execute if expression evaluates to true.
3 Add any number of message processors to execute if none of the conditions evaluates to true.

Choice (<choice/>) attributes are configurable through the UI and XML:

Attribute Name Attribute XML Description

Choice (default)

doc:name

Editable name for the component to display in the canvas.

N/A

doc:id

Automatically generated identifier for the component.

Choice (<choice/>) has the following child elements:

Child Element Description

<when/>

Child element for specifying a condition to meet before running embedded components to process the Mule event. One or more <when/> elements are allowed.

<otherwise/>

Child element that runs if no conditions set within any <when/> element are met.

The child element <when/> has the following attribute:

Attribute Name Attribute XML Description

Expression

expression

DataWeave expression used to evaluate input. If the expression evaluates to true, this routing option executes.

Adding Routes to the Choice Router

By default, the Choice router has one route option that executes when the configured DataWeave expression evaluates to true, and a default route that executes when none of the expressions in the existing routes are true. Add more routes when you need to evaluate more than one condition and then execute different operations depending on which condition is met.

To add a new route:

  1. Add a <when/> element inside the <choice/> element.

  2. Configure the expression attribute to define the condition to execute this route.

Examples

The following example configures an HTTP listener that receives a language query parameter and a Choice router that evaluates the parameter and executes the correspoding route depending on the value.

<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:spring="http://www.springframework.org/schema/beans"
  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:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
    <http:listener-connection host="0.0.0.0" port="8081" />
  </http:listener-config>
  <flow name="content-based-routingFlow" >
    <http:listener config-ref="HTTP_Listener_config" path="/" doc:name="Listener" />
    <set-variable variableName="language" value="#[attributes.queryParams.language]" doc:name="Set Variable" />
    <!-- START OF CHOICE BLOCK-->
    <choice doc:name="Choice" >
      <when expression="#[vars.language == 'Spanish']" >
        <set-payload value="Hola!" doc:name="Reply in Spanish" />
      </when>
      <when expression="#[vars.language == 'French']" >
        <set-payload value="Bonjour!" doc:name="Reply in French" />
      </when>
      <otherwise>
        <flow-ref name="reply-in-default-languageSub_Flow" doc:name="reply-in-default-languageSub_Flow" />
      </otherwise>
    </choice>
    <!-- END OF CHOICE BLOCK-->
    <logger level="INFO" doc:name="Log the reply" message='#["The reply $(payload) means hello in $(vars.language)" ]'/>
  </flow>
  <sub-flow name="reply-in-default-languageSub_Flow" >
    <logger level="INFO" doc:name="Logger" message="No language specified. Using English as a default. "/>
    <set-variable variableName="language" value="English" doc:name="Set Language to English" />
    <set-payload value="Hello!" doc:name="Reply in English" />
  </sub-flow>
</mule>