Contact Free trial Login

Choice Router

The Choice router dynamically routes messages 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.

Choice Router Schematic
Figure 1. Schematic diagram of a Choice router with three options, one being the default option

Example Project

You can use Anypoint Studio to download the example project Content-Based Routing from Anypoint Exchange. This project presents a working example of a flow that uses the Choice router.

choice-router-example
Figure 2. Flow with a Choice router with three options, one being the default option

To download and open this example project:

  1. Click the Exchange icon (3%) in Anypoint Studio.

  2. In the left panel, click All assets.

  3. In the window that opens, log in to Anypoint Exchange and search for the example project Content-Based Routing.

  4. Click the Open icon (6%).

When you run the project as a Mule application, the application performs the following actions:

  1. Listens for messages.

  2. Passes messages to a Set Variable component that sets the variable language to the language that is passed in the message by the parameter language.

  3. Uses a Choice router to find out whether each message contains a language attribute. The presence and value of this attribute determine how the Choice router routes the message:

    • If the value is French, the router routes the message to a Set Payload component that is named Reply in French. This latter component returns the message Bonjour! to the requester.

    • If the value is Spanish, the router routes the message to a Set Payload component that is named Reply in Spanish. This latter component returns the message Hola! to the requester.

    • If the message contains no language attribute, the router routes the message to the default path, which is a subflow that:

  4. Logs the message "No language specified. Using English as a default." to the console

  5. Sets the value of language to English.

  6. Returns the message Hello!.

The example project demonstrates that, when you are planning to route messages in a flow by using a Choice router, there are four aspects to planning that you need to consider:

  • The content that the Choice router evaluates to determine how it routes messages

  • The number of routes

  • The default routing option

  • The processing that the flow performs for each routing option

Example XML

Following is the XML code for the Choice router example:

<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>

Choice Router XML Reference

A Choice flow control component contains these elements:

  • A single root element <choice>

  • A <when> child element for each routing option, each with an expression to match

  • Optionally, an <otherwise> child element of Choice that handles all cases where none of the expressions on the <when> elements evaluate to true

  • Components that are conditionally executed under each of the choices are defined as child elements of the when and otherwise elements

Properties of <choice>

Property Description

Business Events

For activating the Business Events feature:

<choice doc:name="Choice" tracking:enable-default-events="true" >

Properties of <when>

Property Description

Expression (expression)

Expression in DataWeave language to evaluate input.
If the expression evaluates to true, this routing option is used:

<when expression="#[vars.language == 'Spanish']" >

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.