Controlling Event Flow in Mule

In this article, we will see what routing is and the controlling event process in the flow. 

ROUTING 

Routers send events to one or more groups of event processors. We can route messages to different destinations in a flow. Some routers use logic to analyze and transform messages before routing occurs. 

Routing an event between the flows in two ways: 

  1. Using flow reference, synchronously  
  1. Using AVM Queue, asynchronously 

There are four additional flow control components: 

  1. Choice 
  1. First Successful 
  1. Round Robin 
  1. Scatter-Gather 

Let us review each connector briefly: 

Choice Router 

The Choice flow control dynamically routes messages based on conditional logic. Like if/else code block. It operates on expressions to estimate the content of a message. If no expressions estimate as accurate, then it directs the transmission to the default route. 

First Successful 

The First Successful processor route consecutively until one is successfully executed. If the child message processor returns a message without exception, it succeeds. If the exception of a return occurs in the route, then it considers a failure. 

Round Robin 

The Round robin message processor iterates through a list maintained across the executions. Each message received is routed to each child. Once a message has been routed to each child, the next is routed to the first child again, restarting the iteration. 

Scatter-Gather 

The scatter-gather processors route multiple events in parallel. It collects the responses from all routes and aggregates them into a single message. It configures all routes by concurrent processing. It aggregates the results from each of the routes into a message collection. If any failure is detected during execution, it will not stop the process and sends messages to its other configured routes, so it is possible that many or all may fail concurrently. It is also called a Multicasting event. 

Real-Time Examples 

To Create a New Project in AnypointStudio 

  1. Create a new project in AnypointStudio. 
  1. Once the project is created, a new .xml file will appear on the canvas page. 
  1. From the mule palette, drag the HTTP listener and drop it at the top of the canvas. 
  1. In the Listener property view, set the display name to GET /testProject (endpoint) 
  1. Confirm the connector configuration is connected to the existing HTTP_Listener_config 
  1. Set the path to  /testProject and the allowed method to GET. 
  1. Drag any of the flow control elements from the core section of the Mule Palette. 

Example 1 – Using Scatter-Gather 

  1. Drag a Scatter-Gather flow control from Mule Palette and drop it in the process selection of Flow. 
  1. Add three parallel set Payload components to the Scatter-Gather router. 
  1. Set the name and display name of each payload. 
  1. Add a logger after the Scatter-Gather. 

Debug the Application 

  1. Add a breakpoint to the Scatter-Gather. 
  1. Save the file to redeploy the project in debug mode. 
  1. By using Postman or Advanced REST Client, run the request. (http://localhost:8081/testProject) 
  1. Below screenshot shows the flow implementation and console log 

Example 2 – Using Choice 

  1. Return to implementation .xml. 
  1. Drag a flow scope from the Mule Palette and drop it at the top of the canvas above all other flows. 
  1. Change the name of the flow to getCars. 
  1. Move the GET /cars HTTP Listener. 
  1. Drag a Choice flow control element from the Mule Palette and drop it in process section of getCars. 
  1. Add one Set Variable before the choice flow control element, set the name and display the name to cartype. 
  1. Convert the value field to expression mode; then, set its values to a query parameter called cartype. (message.attributes.queryParams.airline) 
  1. Click the When scope in the choice router. 
  1. In the “When” properties view, switch the value field to expression mode, and then add an expression to check if the cartype variable is equal to a specific type. 
  1. Set a similar expression for the united route if you want. 
  1. Run the flow again in debug mode as we mentioned already in example 1. 
  1. The output is shown in the below screenshot. 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Google BigQuery Connector

The Google BigQuery connector allows users to perform different operations on the Google BigQuery instance from Anypoint Platform/Anypoint Studio. BigQuery is Google’s fully managed, petabyte scale, low cost analytics data warehouse. The connector exposes Google BigQuery operations by executing their API calls as per configuration. It supports various operations related to Dataset, Table, Job, etc.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.