Collection Splitter and Aggregator in Mule


In this walk through, we will build a mule application that query the database to fetch major cities of the country provided as a input payload. This uses, collection splitter and collection aggregator along with message enricher.

Lets bring in a http connector and json to object transformer to mule canvas and configure it as shown below. JSON to Object Transformer is mapped to ArrayList class. This converts the json input to collection object.
Drag and drop the collection splitter and keep the default configurations then bring in a message enricher into the canvas and inside enricher processor chain and database connector are placed as shown. Processor Chain components allows us to keep more activities or component inside the message enricher. Configuration of each components are shown.
Output of database is mapped to Object to JSON transformer. And result-set of processor chain is stored to flow variable.

After message enricher, place a set payload component and assign payload with flow variable then bring in collector aggregator have default configurations and place message transform components as shown. 
Below is complete mule flow, when a message is received it converted to arraylist and collection splitter splits payload and processes the 1st payload collection further down the flow; in order to retain the original correlation id and other message properties, message enricher is used and result set from DB operation is stored in flow variable and this updated to payload and pass to collection aggregator. Thus the same process continuous for other collection objects and the final result set it collected in aggregator using the correlation ID. 

The message ["India", "France"], yields two objects India and France, thus 1st payload object lookups the data and gets collected in collection aggregator. Then 2nd payload completes the same process and gets collected and process continuous till no objects are left, finally aggregation process is kick-off and results is echoed to client. 
Lets customize the correlation properties in collection splitter and aggregator as shown below.

Collection Splitter Configuration:-
Collection Aggregator Configuration:-
Run and test project.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.