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.
Similarly, create PriorityQueue.java and copy the following content to it:
[singlepic id=35 w=480 h=360 float=none]
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
- 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] - http://localhost:8001/priority would result in “Sent message (/priority) to priority Queue”.
[singlepic id=32 w=480 h=360 float=none] - 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]