Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Choice Flow Control Reference

Version 3.3.2 and 3.3.1 Only

This document describes the behavior of the Choice Flow Control for Mule Studio 3.3.1 and 3.3.2 only. Refer to the previous version of this document for details pertaining to earlier versions of Mule Studio.

The choice flow control dynamically routes messages 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 (see image below). It directs messages to the first routing option in the scope that matches the routing configurations (i.e. evaluates to true). If none of expressions evaluate to true, the choice flow control directs the message to the default (i.e. else) route.

Choice_schematic

Adding the Choice Flow Control

In Studio, drag the Choice icon from the Studio palette to the canvas, positioning it within the sequence of building blocks that form the flow (below). 

Studio_Choice_add

Add a choice element in your flow, with one attribute and, at minimum, two child elements as per the table below. Refer to the code sample below.

Attribute Value

doc:name

unique name for the choice element (not required for Standalone)

Child Element

Attribute

when

expression

otherwise

n/a


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<flow name="New_Studio_ProjectFlow1" doc:name="New_Studio_ProjectFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <choice doc:name="Choice">
            <when expression="">
                <processor-chain>
                </processor-chain>
            </when>
            <otherwise>
                <processor-chain>
                </processor-chain>
            </otherwise>
        </choice>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"/>
        </scripting:component>
</flow>

Configuring the Choice Flow Control

To configure the choice flow control, you need to determine the following message routing details:

  • the content the choice flow router should evaluate to determine routing

  • the number of routing options with which to supply the choice flow control

  • the processing Mule should perform for each routing option

  • the default routing option

Once you have determined your routing goals, follow the procedure below to define the routing options, then the routing instructions.

  1. Insert message processors within the dashed line area that signifies the choice flow control scope to define the routing options, making sure to place one of them within the "Default" box to define it as the default routing option. Note that you can place several message processors in a chain for each routing option, as needed. In our example, shown below, we have defined three routing options.

    Studio_Choice_routingoptions

    1. Drag, then drop a message processor into the blank area above the default box within the dashed line which signifies the scope of the choice flow control. This is the first message processor in the flow control’s first routing option. In the example (below), we use the Expression transformer as the first message processor in the first routing option.

      Studio_Choice_add1

    2. To add additional processing steps to a routing option, drag and drop another message processor into the space immediately after the message processor you just added, still within the scope of the choice flow control. In the example (below), we add the Session Variable transformer as the second message processor in the first routing option.

      Studio_Choice_Add2

    3. To add another routing option, drag and drop a message processor into the choice flow control scope. In the example below, we add an Attachment transformer as our second routing option.

      Studio_Choice_Add3

    4. Next, add a message processor within the "Default" box to set the default routing option. In our example below, we add an FTP endpoint.

      Studio_Choice_add4

  1. Double-click to open the Choice Flow Control’s Pattern Properties panel (below).

    Studio_Choice_PatternProperties

  2. Mule displays a table with two columns: When and Route Message to. Notice, in the image above, that Mule displays a line item for each routing option. Mule identifies each routing option by its first message processor. Double-click the first line item in the Route Message to column to open the Route Properties panel (below).

    Studio_Choice_RouteProperties_Expression

  3. In the Expression field, enter an expression for the choice router to use to evaluate the contents of a message. For example:

    #[payload['name'] == null]

  4. Repeat the two preceding steps until you have supplied the choice flow control with a When expression for each non-default routing option (see example below).

    Studio_Choice_PatternProperties_configured1

  5. Double-click the Default line item to open its Route Properties panel. 

    Notice that:

    1. you cannot edit the Expression field

    2. the Otherwise box is checked

      Studio_Choice_default_routeproperties

      The Otherwise box identifies this route as the Default for the choice flow control. If the flow control cannot route a message to any of the preceding routing options in its scope, it directs the message to the default route.

  6. Click OK to save the routing configurations.

  7. As it processes messages, Mule evaluates the expressions defined in your routing options in order, top down, until one of them evaluates to "true". If necessary, drag and drop building blocks within the choice flow control scope on the canvas to reorder routing options.

  1. To the first when element within your choice element, add message processors as child elements to form a routing option to which the choice element can direct messages. In the code sample below, we have added an expression-transformer and a session-variable-transformer.

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    <flow name="New_Studio_ProjectFlow1" doc:name="New_Studio_ProjectFlow1">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
            <choice doc:name="Choice">
                <when expression="">
                    <processor-chain>
                        <expression-transformer doc:name="Expression"/>
                        <session-variable-transformer doc:name="Session Variable"/>
                    </processor-chain>
                </when>
                <otherwise>
                </otherwise>
            </choice>
            <scripting:component doc:name="Groovy">
                <scripting:script engine="Groovy"/>
            </scripting:component>
    </flow>
  2. Configure the contents of one or more additional when elements to define multiple routing options for your choice element. Refer to code sample below.

  3. Configure the contents of the otherwise child element to define the default routing option to which your choice router can direct messages if all the previous when expressions evaluate to false. Refer to code sample below.

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    <flow name="New_Studio_ProjectFlow1" doc:name="New_Studio_ProjectFlow1">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
            <choice doc:name="Choice">
                <when expression="">
                    <processor-chain>
                        <expression-transformer doc:name="Expression"/>
                        <session-variable-transformer doc:name="Session Variable"/>
                    </processor-chain>
                </when>
                <when expression="">
                    <processor-chain>
                        <attachment-transformer doc:name="Attachment"/>
                    </processor-chain>
                 </when>
                <otherwise>
                    <processor-chain>
                        <ftp:outbound-endpoint host="localhost" port="21" responseTimeout="10000" doc:name="FTP"/>
                    <processor-chain>
                </otherwise>
            </choice>
            <scripting:component doc:name="Groovy">
                <scripting:script engine="Groovy"/>
            </scripting:component>
        </flow>
  4. For each when element, enter an expression for the choice router to use to evaluate the contents of a message. If, during processing, the expression associated with a routing option evaluates to true, Mule directs the message to that route. Refer to example expression below.

    
           
                   
                
    1
    
    <when expression="#[payload['name'] == null]">
  5. As it processes messages, Mule evaluates the expressions defined in your routing options in the order they appear in the config, top down, until one of them evaluates to "true". Adjust the order of the when elements in your flow with this in mind.

Configuration Summary

Element Description

choice

Dynamically routes messages based on message payload or properties, adding conditional programming to a flow, similar to an if/then/else code block.

Element Attribute Description

doc:name

Customize to display a unique name for the flow control in your application.

Note: Attribute not required in Mule Standalone configuration.

Child Element Description

when

Use to define all non-default routing options within the choice flow control.

Child Element Attribute Value Description

expression

Mule expression

Use MEL to define an expression that the choice router will use to evaluate the contents of a message. If the expression evaluates to "true", Mule directs the message to this routing option.

Child Element Description

otherwise

Use to define the default routing option for the message, should none of the preceding when expressions evaluate to "true"

Changing the Default Route

You can change the choice flow control configuration to identify a different default routing option.

  1. Double-click to open the choice flow control icon, then, in the table, double-click the line item of whichever routing option that you would like to specify as the new default route.

    Studio_Choice_newdefault1

  2. Check the Otherwise box (see below), then click OK.

    choice4

  3. Mule applies the Default label to the new default routing option in the table on the pattern properties panel (below). (Note that the FTP routing option now needs a "when" expression defined.) Click OK.

    Studio_Choice_newdefault3

  4. Mule applies the new routing order to the building blocks on the canvas. The new default routing option appears at the bottom of the scope.

    Studio_Choice_NewOrder

  5. Define a when expression for the routing option previously identified as the default. (In the example, the FTP routing option.)

Adjust your XML configuration to swap the contents of a when element and the otherwise element.

The code sample below has been adjusted to make the Attachment transformer the default routing option and change the FTP outbound endpoint to a when element. Note that the otherwise element requires no further configuration, but we defined a new expression for the new when element.


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<flow name="ChoiceFlowFlow1" doc:name="ChoiceFlowFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <choice doc:name="Choice">
            <when expression="#[payload['name'] == null]">
                <processor-chain>
                    <expression-transformer doc:name="Expression"/>
                    <session-variable-transformer doc:name="Session Variable"/>
                </processor-chain>
            </when>
            <when expression="#[payload['amount'] > 30000]">
                <processor-chain>                
                    <ftp:outbound-endpoint host="localhost" port="21" responseTimeout="10000" doc:name="FTP"/>
                </processor-chain>
            </when>
            <otherwise>
                <processor-chain>
                    <attachment-transformer doc:name="Attachment"/>
                </processor-chain>
            </otherwise>
        </choice>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"/>
        </scripting:component>
    </flow>

Complete Example Code


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
 
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:ftp="http://www.mulesoft.org/schema/mule/ee/ftp" 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" version="EE-3.3.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
 
http://www.mulesoft.org/schema/mule/ee/ftp http://www.mulesoft.org/schema/mule/ee/ftp/current/mule-ftp-ee.xsd
 
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 ">
 
    <flow name="ChoiceFlowFlow1" doc:name="ChoiceFlowFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <choice doc:name="Choice">
            <when expression="#[payload['name'] == null]">
                <processor-chain>
                    <expression-transformer doc:name="Expression"/>
                    <session-variable-transformer doc:name="Session Variable"/>
                </processor-chain>
            </when>
            <when expression="#[payload['amount']] &gt; 30000]">
                <processor-chain>
                    <ftp:outbound-endpoint host="localhost" port="21" responseTimeout="10000" doc:name="FTP"/>
                </processor-chain>
            </when>
            <otherwise>
                <processor-chain>
                    <attachment-transformer doc:name="Attachment"/>
                </processor-chain>
            </otherwise>
        </choice>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"/>
        </scripting:component>     </flow>
</mule>

See Also