Mule School: Using Flow Controls – Choice Router

April 2 2012

2 comments 0
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”.

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.

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:


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.

Drag and drop Choice router next to the HTTP endpoint from the Flow Control palette, as shown below:

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.

Double-click on Java component to open the Property dialog. Specify “NormalQueue” as Display Name and “org.mulesoft.routing.NormalQueue” for Class Name.

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.

Double-click on Java component to open the Property dialog. Specify “PriorityQueue” as Display Name and “org.mulesoft.routing.PriorityQueue” for Class Name.

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

The resulting flow looks like this:

We now need to specify filter conditions using Groovy. Double-click on Choice router to open the Properties Dialog.

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.

Similarly, double-click on “PriorityQueue” and enter payload.contains(“priority”) in Expression.

Finally, double-click on the third branch “NormalQueue” and select the “Otherwise” checkbox.

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.
  2. http://localhost:8001/priority would result in “Sent message (/priority) to priority Queue”.
  3. http://localhost:8001/other would result in “Sent message (/normal) to normal Queue”. This is for the “otherwise” branch.

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.

    Agree(0)Disagree(0)Comment
  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.

    Agree(0)Disagree(0)Comment