In Mule, Flow Controls helps you Route your input Mule Event into one or more separate flows based on the Flow control components you use.
In this article, we’ll discuss 4 of them that are listed below:
- First Successful
Let’s understand each of these components and their use case.
Choice Router works on the evaluation of the expression ie, True or False. The Route for which the expression evaluates to be True, that route is executed.
It executes routes sequentially ie if for both route-1 and route-2 condition expression evaluates to be true, it will execute only route-1 because of its sequential nature and will not execute route-2.
The expression cannot be kept as blank and there is a default block that executes if none of the expressions evaluates to True.
Consider you want to fetch the details of employees based on their department, you can make use of Choice Router. The First condition will fetch the employees from the IT department, eg,
payload.Employee.Department == "IT"the Second condition will fetch the employees from the HR department, eg,
payload.Employee.Department == "HR".
If a department other than this comes in the input then the Default route will be triggered.
Unlike Choice router which takes only one route, Scatter-Gather sends the requested data to multiple routes concurrently.
It waits for all the routes to complete the execution and then aggregates the response from each route and forms a Single output message. The Output of this is application/java format and consists of payload and attributes.
One more point to note here is, if any one of the routes fails, then the entire Scatter-Gather process fails. So while using Scatter-Gather, error handling becomes very crucial.
The maximum time taken by Scatter-Gather to complete the execution is the max time taken by any route. For instance, if route-1 takes 5 secs and route-2 takes 10 secs then Scatter-Gather will take 10 secs to complete the execution.
Suppose you need to club data for 2 different targets then instead of calling both of these targets sequentially, you can use Scatter-Gather. Below is a simple demo with the Scatter-Gather output.
First Successful is a router that iterates through each route in the router component until one of the routes executes successfully. If none of the routes in this router component give a success response then the router component throws an error response.
So basically, the First Successful router sends the incoming data to route-1 and if this route fails, it then sends the same data to route-2. If this route gives success response then the First successful router will not execute any further routes and will complete the execution.
You can use this router when you want to make a call to your application running on the secondary server if the application running on the primary server fails to respond. Below is a simple demo.
This router iterates through all the routes in the router component in a specific order but it only routes to one of the routes each time it is executed. For example, it keeps track of the last route it selected for execution and will not execute the same route again consecutively.
Basically, this router works in a circular manner and whichever route executes first will not be executed in the next execution till all the other routes are executed at least once.
This Router generally fits in scenarios where you want to balance the load ie distribute the load on each route. Below is the simple demo where the logger-1 route will execute first and in the immediate next iteration, the logger-2 will be executed and this sequence will continue.
Round Robin Component