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