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:
- Using flow reference, synchronously
- Using AVM Queue, asynchronously
There are four additional flow control components:
- Choice
- First Successful
- Round Robin
- 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
- Create a new project in AnypointStudio.
- Once the project is created, a new .xml file will appear on the canvas page.
- From the mule palette, drag the HTTP listener and drop it at the top of the canvas.
- In the Listener property view, set the display name to GET /testProject (endpoint)
- Confirm the connector configuration is connected to the existing HTTP_Listener_config
- Set the path to /testProject and the allowed method to GET.
- Drag any of the flow control elements from the core section of the Mule Palette.
Example 1 – Using Scatter-Gather
- Drag a Scatter-Gather flow control from Mule Palette and drop it in the process selection of Flow.
- Add three parallel set Payload components to the Scatter-Gather router.
- Set the name and display name of each payload.
- Add a logger after the Scatter-Gather.
Debug the Application
- Add a breakpoint to the Scatter-Gather.
- Save the file to redeploy the project in debug mode.
- By using Postman or Advanced REST Client, run the request. (http://localhost:8081/testProject)
- Below screenshot shows the flow implementation and console log
Example 2 – Using Choice
- Return to implementation .xml.
- Drag a flow scope from the Mule Palette and drop it at the top of the canvas above all other flows.
- Change the name of the flow to getCars.
- Move the GET /cars HTTP Listener.
- Drag a Choice flow control element from the Mule Palette and drop it in process section of getCars.
- Add one Set Variable before the choice flow control element, set the name and display the name to cartype.
- Convert the value field to expression mode; then, set its values to a query parameter called cartype. (message.attributes.queryParams.airline)
- Click the When scope in the choice router.
- 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.
- Set a similar expression for the united route if you want.
- Run the flow again in debug mode as we mentioned already in example 1.
- The output is shown in the below screenshot.