Nav

About Scatter-Gather

The Scatter-Gather component is a routing event processor. It copies (scatters) a Mule event to two or more parallel routes, each route being a sequence of one or more event processors that is similar to a subflow. Each route uses a separate thread and creates a Mule event, which has its own payload, attributes, and variables. The Scatter-Gather component then consolidates (gathers) the Mule events from each route together into a new Mule event and passes this consolidated Mule event to the next event processor. The Scatter-Gather component passes a consolidated Mule event to the next event processor only after every route completes successfully.

The Scatter-Gather component executes each route in parallel, not sequentially. Parallel execution of routes can greatly increase the efficiency of your Mule application and may provide more information than sequential processing.

scatter gather concept 56ccc
A Scatter-Gather component cannot be configured with only one route. A Mule application will throw an exception and not start if it contains a flow in which a Scatter-Gather component has not been configured with at least two routes.

How Errors Generated Inside Scatter-Gather Routes are Handled

You can use a Try scope in each route of a Scatter-Gather component to handle any errors that might be generated by a route’s event processors. Unhandled errors that are thrown because of a problem in a route cause the Scatter-Gather component to also throw an error of type MULE:COMPOSITE_ERROR. The component throws this error only after every route either completes or fails. Event processing does not proceed further than the Scatter-Gather component in the flow. Instead, the flow branches to your error-handling event processors. The MULE:COMPOSITE_ERROR error object gathers together not only the errors from the route that failed, but also the Mule events from completed routes, so that your application can still process Mule events from the routes that completed by using your error-handling event processors.

To illustrate how this works, let’s look at two cases. In the first case, the routes in a Scatter-Gather component each contain a Try scope. One of the routes generates an error that is successfully handled by that route’s Try scope, so the route is still able to generate a Mule event. The Scatter-Gather component consolidates the Mule events from all routes into a new Mule event, as usual, and passes the consolidated event to the next event processor.

In the second case, an error occurs in a route. This route either does not contain a Try scope or the error is of a type that the Try scope cannot handle. Although the other routes all complete, the one failed route causes the Scatter-Gather component to throw a MULE.COMPOSITE ERROR error, which the flow passes to the Error Handler component that you have configured for your flow. The error-handling event processors are able to process the Mule events from the completed routes.

How Scatter-Gather Timeout Errors are Handled

You can configure a timeout for a Scatter-Gather component. If a route does not complete processing before the period of time set in the timeout expires, the route throws a MULE:TIMEOUT error. This error is then handled the same way as any other error generated from a route: after each route completes (either by completing its last event processor or by throwing a MULE:TIMEOUT error), the successful results and errors are collected together and thrown by the Scatter-Gather component as a MULE:COMPOSITE error, which can then be processed in an error handler.

Example

In Anypoint Studio, you can download and open the example project Scatter-Gather Flow Control from Anypoint Exchange to learn more about how to use the Scatter-Gather component. This example shows the usage of the scatter-gather control flow to aggregate data in parallel and return the result in JSON.

The example uses prepared data as input for two resources that should be aggregated. The data represents information about two contacts and has the following structure:

Resource

firstname

surname

phone

email

contacts-1.csv

John

Doe

096548763

john.doe@texasComp.com

contacts-2.csv

Jane

Doe

091558780

jane.doe@texasComp.com

DataWeave is used to aggregate the data. The information about the contacts is aggregated to a JSON structure that represents data from both resources.

To download and open this example project while you are in Anypoint Studio, click the Exchange icon in the upper-left corner. Then, in the window that opens, log into Anypoint Exchange and search on the name of the project.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.

+