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
else code block in most programming languages.
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:
Listens for messages.
Passes messages to a Set Variable component that sets the variable
languageto the language that is passed in the message by the parameter
Uses a Choice router to find out whether each message contains a
languageattribute. 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
languageattribute, the router routes the message to the default path, which is a subflow that:
Logs the message "No language specified. Using English as a default." to the console
Sets the value of
Returns the message
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.