DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Error Handling Inside Kumologica Subflow
  • The LLM Selection War Story: Part 3 - Decision Framework Through Failure Tolerance
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • Model Context Protocol Vs Agent2Agent: Practical Integration with Enterprise Data

Trending

  • Security Readiness Checklist: From AI Threats to Software Supply Chain Defense
  • How AI Is Rewriting the Rules of Software Security: Machine-Speed Delivery, Shifting Risk, and New Control Points
  • Spring CRUD Generator v1.1.0 Updates
  • The "Zombie API" Attack: Why Your Old Integrations Are Your Biggest Security Risk

Flow Control in Mule 4

In this article, we will look into four different Flow control components provided in Mule. We will also cover various use cases.

By 
Karan Gupta user avatar
Karan Gupta
·
Feb. 13, 22 · Review
Likes (3)
Comment
Save
Tweet
Share
15.0K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • Choice
  • Scatter-Gather
  • Round-Robin
  • First Successful

Let's understand each of these components and their use case.

Choice

Choice Router works on the evaluation of the expression i.e., True or False. The Route for which the expression evaluates to be True, that route is executed.

It executes routes sequentially i.e. 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.

Use Case

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, e.g., payload.Employee.Department == "IT", the Second condition will fetch the employees from the HR department, e.g., payload.Employee.Department == "HR". 

If a department other than this comes in the input then the Default route will be triggered.

Choice Component

Scatter-Gather

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.

Use Case

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.

Scatter-Gather Component

Aggregated Output

First Successful

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.

Use Case

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.

First Successful Component

Round Robin

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.

Use Case

This Router generally fits in scenarios where you want to balance the load i.e. 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


Flow control (data) Flow (web browser) Use case Round-robin (document)

Opinions expressed by DZone contributors are their own.

Related

  • Error Handling Inside Kumologica Subflow
  • The LLM Selection War Story: Part 3 - Decision Framework Through Failure Tolerance
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • Model Context Protocol Vs Agent2Agent: Practical Integration with Enterprise Data

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook