mule custom aggregator

12
CUSTOM AGGREGATOR

Transcript of mule custom aggregator

Page 1: mule   custom aggregator

CUSTOM AGGREGATOR

Page 2: mule   custom aggregator

2

Scatter-Gather

• Scatter Gather is a mule routing strategy that may sends the same payload and execute different process asynchronously.

• By default, all results are gathered into a list of objects that can be used when needed.

Page 3: mule   custom aggregator

3

Aggregation• If one did not specify a custom strategy, mule

provide a default one that populates all results into one single collection

• But sometimes, a need for custom aggregation is needed.

Page 4: mule   custom aggregator

4

Custom AggregatorCreating custom aggregator is pretty simple.

1. Create a class that implements the following interface

org.mule.api.routing.AggregationContext

Page 5: mule   custom aggregator

5

Custom Aggregator

2. Override and create your own implementation of the method

MuleEvent aggregate(AggregationContext context) throws MuleException

Page 6: mule   custom aggregator

6

Custom Aggregator

One must understand the method and parameters that got overridden in order to create proper implementation.

aggregate(AggregationContext context)

Page 7: mule   custom aggregator

7

Custom Aggregator

One must understand the method and parameters that got overridden in order to create proper implementation.

aggregate(AggregationContext context)

Page 8: mule   custom aggregator

8

Custom AggregatorThere are 2 key factor that needs to understand under the parameter context.

1. The first one is the ‘events’ and2. The second one is ‘original event’.

Page 9: mule   custom aggregator

9

Custom AggregatorContext ‘events’ defines a collection of events that gathered from one or multiple routes through scatter gather. These events holds its own message (MuleMessage) which may be valuable because of the payload (result). Iterating to the events allows the access to each payload for each routes taken inside scatter-gather.

Page 10: mule   custom aggregator

10

Custom AggregatorContext ‘Original event’ is a single event that defines the actual event running before/during/after the call for scatter-gather. You may think of the Original Event as the message being processed originally.

Page 11: mule   custom aggregator

11

Custom Aggregator

3. Iterate over the collection of events and get every data needed. If you are composing a new payload, you need to set it as payload to the OriginalEvent, and then return Original Event. If adding message properties, this should also be done in the message of the original event.

Page 12: mule   custom aggregator

12

Custom Aggregator (Example)