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


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.

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

To download and open this example project while you are in Anypoint Studio, click the Exchange icon in the upper-left corner. Then, in the window that opens, log into Anypoint Exchange and search on the name of the project.