mule custom aggregator
TRANSCRIPT
![Page 1: mule custom aggregator](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/1.jpg)
CUSTOM AGGREGATOR
![Page 2: mule custom aggregator](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/5.jpg)
5
Custom Aggregator
2. Override and create your own implementation of the method
MuleEvent aggregate(AggregationContext context) throws MuleException
![Page 6: mule custom aggregator](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081521/587380631a28ab272d8b4b19/html5/thumbnails/12.jpg)
12
Custom Aggregator (Example)