The Scatter-Gather component is a routing event processor that processes a Mule event through different parallel processing routes that contain different event processors.
Each route receives a reference to the Mule event and executes a sequence of one or more event processors. Each of these routes uses a separate thread to execute the event processors, and the resulting Mule event can be either the same Mule event without modifications or a new Mule event with its own payload, attributes, and variables. The Scatter-Gather component then combines the Mule events returned by each processing route into a new Mule event that is passed to the next event processor only after every route completes successfully.
The routing message processor Scatter-Gather sends a request message to multiple targets concurrently. It collects the responses from all routes, and aggregates them into a single message.
Scatter Gather in MuleSoft
The following diagram details the behavior of the Scatter-Gather component:
Scatter Gather Component:
Example
Drag and drop HttpListener and Configure
Global configuration
General configuration
Step 2: Add a Set Variable and set a variable and name it.
Scatter-Gather router config:
Route-1:
Route-2:
The Scatter-Gather router should look like:
Step 4: Add Transform Message and simply transform the object to JSON model.
Step 5: At last add the logger and log the variable “name“.
That’s it. Now run the mule application and trigger a request to http://localhost:8089/scatterGather.
Scatter-Gather returns the payload as an Object. The sample output would look like:
{
“0”: {
“inboundAttachmentNames”: [],
“exceptionPayload”: null,
“inboundPropertyNames”: [],
“outboundAttachmentNames”: [],
“payload”: {
“userId”: 1,
“id”: 1,
“title”: “delectus aut autem”,
“completed”: false
},
“outboundPropertyNames”: [],
“attributes”: {
“headers”: {
“date”: “Sun, 21 Feb 2021 05:42:24 GMT”,
“content-type”: “application/json; charset=utf-8”,
“content-length”: “83”,
“connection”: “keep-alive”
},
“reasonPhrase”: “OK”,
“statusCode”: 200
}
},
“1”: {
“inboundAttachmentNames”: [],
“exceptionPayload”: null,
“inboundPropertyNames”: [],
“outboundAttachmentNames”: [],
“payload”: {
“userId”: 1,
“id”: 2,
“title”: “quis ut nam facilis et officia qui”,
“completed”: false
},
“outboundPropertyNames”: [],
“attributes”: {
“headers”: {
“date”: “Sun, 21 Feb 2021 05:42:24 GMT”,
“content-type”: “application/json; charset=utf-8”,
“content-length”: “99”
},
“reasonPhrase”: “OK”,
“statusCode”: 200
}
}
}
Note: We have removed few headers to keep the response simple.
The above response contains the attributes and the payload but most of the times the attributes are not required.
In such cases, to extract the only payload you can use flatten function to merge the both the responses.
To do that, replace the above dataweave script as shown below:
%dw 2.0
Output application/json
—
Flatten (payload..payload)
The response would look like after the above transformation:
Point to Rememeber:
Variable Propagation in Scatter-Gather
Now let’s see what happens to variables when we override the variables inside Scatter-Gather router.
Every route starts with the same initial variable values. Modifications to a variable within a specific route do not affect other routes. So, if a variable is added or modified in one route, then, after aggregation, the value is defined by that route. If a variable is added or modified by more than one route, the value is added to a list of all the values defined for that variable within all the routes.
So, if a variable is added or modified in one route, then, after aggregation, the value is defined by that route.
Concurrency Control
By default the Scatter-Gather process the mule event in parallel but sometimes we may need to process the mule event sequentially.
To achieve the sequential process, we can use two methods:
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…