Scatter-Gather in MuleSoft

The Scatter-Gather component executes each route in parallel. 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.

A Scatter-Gather component must have at least two routes; otherwise, your Mule application throws an exception and does not start. Use Try scope in each route of a Scatter-Gather component to handle any errors that might be generated by a route’s event processors.

Here is a demo of Scatter-Gather using read file:

Two read operations will read the data from two different file like JSON and XML respectively.

In the next step, we will collect the data from both JSON and XML files and apply the transformation. Then, combine the two transformation (2 array) into a single transformation (single array) using the flatten function. The output should be in single JSON object.

Transformation :

Flatten function :

I have added a new transform component (flatten function) and wrote a DataWeave.

%dw 2.0

output application/json

— -

flatten(payload[“0”].payload ++ payload[“1”].payload)

Logger :

Output :

--

--