Scatter Gather in MuleSoft
- The ScatterGather 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
- Scatter-Gather component to have at least two routes; otherwise, your Mule application throws an exception and does not start.
- Use Try scope in each route of a ScatterGather component to handle any errors that might be generated by a route’s event processors
In this tutorial I will invoke 2 different Database Tables in parallel using scatter gather and it will give the new mule event with the result of 2 Database calls
Create a project in anypoint studio and configure the listener to trigger the flow
Drag and drop scatter gather and configure the 2 Select Operation
Two select operation will fetch the data from two different database table
Second select statement
In the next step transform the message to json
%dw 2.0 output application/json --- payload
Deploy and test the project from soapui, you can see a mule event create with the combination of the results from database calls. Each mule event contains attributes, payload and variables
Now in the next step we will collect the data from both table and combine as a single json object
I have added a new transform component and written a dataweave
%dw 2.0 output application/json --- (payload.payload ++ payload.payload) reduce ($ ++ $$)
Deploy and again test the same
Sample application: scattergather sample
Soapui Sample project: ScatterGather-soapui-project.xml