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.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…