Mule School: Using Flow Controls – Choice Router

motif

Flow Controls, as the name suggests, control how messages are sent and received within a Mule flow. In the following examples, we’ll learn about the various Flow Controls that Mule offers. First up: Choice Router.

Choice Router allows us to route a request to a specific path based on an expression. Mule provides implementations of all the common enterprise integration patterns from this recommended book. The Choice router enables content-based routing, which is a common way to introduce routing logic based on content of the current message.

 

In this example, we are going to use Choice Router to specify a route request to two different Java components based on a specific keyword in request parameters.

Under LearningMule project, create a new flow and name it “routing”.
[singlepic id=30 w=480 h=360 float=none]

Create Java files that will serve as endpoint components for Choice Router.

Click on File > New > Class. Enter “org.mulesoft.routing” for Package name and “NormalQueue” for Class name. Click on Finish.
[singlepic id=34 w=480 h=360 float=none]

Copy the following code to NormalQueue.java. This contains a single method “send” that returns a static string.

package org.mulesoft.routing;

public class NormalQueue {

	public String send(String message) {
		return "Sent message (" + message + ") to normal Queue";
	}

}

Similarly, create PriorityQueue.java and copy the following content to it:
[singlepic id=35 w=480 h=360 float=none]

package org.mulesoft.routing;

public class PriorityQueue {

	public String send(String message) {
		return "Sent message (" + message + ") to Prirority Queue";
	}

}

Drag and drop an HTTP endpoint on to the Message Flow canvas. By default, this will create an HTTP inbound endpoint listening on port 8081.
[singlepic id=36 w=480 h=360 float=none]

Drag and drop Choice router next to the HTTP endpoint from the Flow Control palette, as shown below:
[singlepic id=37 w=480 h=360 float=none]

Choice Router is similar to the Java statement, “if .. then .. else”. It allows us to specify multiple conditions followed by an otherwise. Let’s create our first condition to check for – a “normal” string in the payload – and then route the request to “NormalQueue” Java component as a result. We will use Groovy script to inspect the payload content. Groovy will be covered in detail in future lessons, but for now, we’ll use a very basic Groovy expression as shown in following steps.

Drag and drop a Java component next to Choice router as shown below. We will send “normal” requests to this Java component.
[singlepic id=38 w=480 h=360 float=none]

Double-click on Java component to open the Property dialog. Specify “NormalQueue” as Display Name and “org.mulesoft.routing.NormalQueue” for Class Name.
[singlepic id=21 w=480 h=360 float=none]

Drag and drop a second Java component next to Choice router as shown below. Please make sure you drop it exactly next to Choice router as opposed to the first Java component.
[singlepic id=39 w=480 h=360 float=none]

[singlepic id=40 w=480 h=360 float=none]

Double-click on Java component to open the Property dialog. Specify “PriorityQueue” as Display Name and “org.mulesoft.routing.PriorityQueue” for Class Name.
[singlepic id=22 w=480 h=360 float=none]

Repeat this step once more to create the third branch. Double-click on Java component to open the Property dialog. Specify “NormalQueue” as Display Name and “org.mulesoft.routing.PriorityQueue” for Class Name. We will use this branch for “otherwise”.
[singlepic id=21 w=480 h=360 float=none]

The resulting flow looks like this:
[singlepic id=41 w=480 h=360 float=none]

We now need to specify filter conditions using Groovy. Double-click on Choice router to open the Properties Dialog.
[singlepic id=24 w=480 h=360 float=none]

Double-click on “NormalQueue” and specify “groovy” for expression language and payload.contains(“normal”) for expression. “payload” is an implicit Java object in Mule to hold request payload. Groovy allows us to perform normal Java operations on this object.
[singlepic id=25 w=480 h=360 float=none]

Similarly, double-click on “PriorityQueue” and enter payload.contains(“priority”) in Expression.
[singlepic id=26 w=480 h=360 float=none]

Finally, double-click on the third branch “NormalQueue” and select the “Otherwise” checkbox.
[singlepic id=27 w=480 h=360 float=none]

This completes our example. Right-click on the project and Run as “Mule Flow”.

Testing

  1. http://localhost:8001/normal. We can see “Sent message (/normal) to normal Queue” as the output.
    [singlepic id=31 w=480 h=360 float=none]
  2. http://localhost:8001/priority would result in “Sent message (/priority) to priority Queue”.
    [singlepic id=32 w=480 h=360 float=none]
  3. http://localhost:8001/other would result in “Sent message (/normal) to normal Queue”. This is for the “otherwise” branch.
    [singlepic id=33 w=480 h=360 float=none]

We'd love to hear your opinion on this post


2 Responses to “Mule School: Using Flow Controls – Choice Router”

  1. Hi Amjad

    Can you access variables in the expressions? I have set a variable numUsers using a property transformer in studio and then want to evaluate numUsers < 100 in my choice router. I have an expression of #[variable:numUsers < 100] but it doesn't seem to like it.

  2. 1. Al realizar este caso, me mostraba error y era porque las condiciones las ponia asi con comilla doble:
    payload.contains(“normal”)
    Pero en el archivo *.xml donde muestra el codigo del flujo mostraba error y era porque era requerido con comilla simple:
    payload.contains(‘normal’)

    2. Creo que deben mencionar eso para que no se demoren en buscar el error.