Splitter and Collection Aggregator With Mulesoft
Splitter and Collection Aggregator are used in Mulesoft to split messages into separate fragments and then to reassemble parts of the original message.
Join the DZone community and get the full member experience.
Join For FreeSplitter is used to split Mule messages into separate fragments. Each fragment is then sent one at a time to the next processor in the flow. Segments are identified by expression parameters and are generally written in MEL (Mule Expression Language). You can then use a Collection Aggregator Flow Control to reassemble the parts of the original message.
Now, we will walk through how to use Splitter in your Mule flow.
Splitting the Message Payload
Place the HTTP listener into the canvas and click to open the Properties console.
Click the green + and configure as follows:
Host: localhost.
Port: 8081.
Method:
POST
.Path: slitter.
Place the Splitter component in Message processor region after HTTP Listener.
Field | Description | Default Value | Example |
Display Name | It is used to provide a unique name for Splitter in your application. | Splitter | doc:name="Splitter" |
Enable Correlation | Specifies whether Mule should give outgoing messages a correlation ID. Options are:
|
|
enableCorrelation="IF_NOT_SET" |
Message Info Mapping |
Optional. If this child element is not configured, |
"[java.util.UUID.randomUUID().toString()]" "[xpath('//Employee/FirstName')]"/> |
|
Expression |
It is used define how to split the message. This is a required field. No default value. |
#[xpath('//Employee')] |
Configure the Splitter component as shown below.
Parameter | Value |
Display Name |
Splitter |
Enable Correlation |
IF_NOT_SET |
Expression |
#[xpath('//Employee')] |
Place the DOM to XML transformation after Splitter, as it will convert the split message object into XML.
Finally, place the file connector at the end of message processor to save all split messages.
Testing Application
You can use Postman to post the message. The listening URL will be http://localhost:8081/splitter.
Sample input message:
<Employees>
<Employee>
<FirstName>Jitu</FirstName>
<LastName>Jain</LastName>
<Salary>100000</Salary>
</Employee>
<Employee>
<FirstName>Jospeh</FirstName>
<LastName>Adams</LastName>
<Salary>200000</Salary>
</Employee>
<Employee>
<FirstName>Steve</FirstName>
<LastName>Simon</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Brain</FirstName>
<LastName>Handscomb</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Rajeev</FirstName>
<LastName>Thandani</LastName>
<Salary>500000</Salary>
</Employee>
</Employees>
The above sample input message will be split into five messages and will be saved to file location as five different files.
Now, you can verify the file location and check if the message is properly split into five different files.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="splitterprojectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/splitter" allowedMethods="POST" doc:name="HTTP"/>
<splitter expression="#[xpath('//Employee')]" doc:name="Splitter"></splitter>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<file:outbound-endpoint path="src/test/resources/out" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Aggregating Message Payload
When Mule splits the message, it adds three new outbound variables into each of the output fragments. This you can use later when aggregating the message.
MULE_CORRELATION_GROUP_SIZE
: Number of fragments into which the original message was split.MULE_CORRELATION_SEQUENCE
: Position of a fragment within the group.MULE_CORRELATION_ID
: Single ID for the entire group (all output fragments of the same original message share the same value).
These three variables play an important role while aggregating the message, as they know what group to put it into and what the size of group is. Once all the split messages are received, it passes any split messages into one aggregated group.In continuation with above example, place Collection Aggregator after Splitter.
Field | Description | Default Value | Example |
Display Name |
It is used to provide unique name for Collection Aggregator in your application. |
Collection Aggregator | doc:name="Collection Aggregator" |
Timeout | Define a timeout in milliseconds to wait for events to be aggregated. | timeout="60000" | |
Fail On Timeout | If set, your app will fail if the aggregator times out | false | failOnTimeout="true" |
Message Info Mapping | Optional. If this childelement is not configured, MuleMessage.getCorrelationId() is used, which is optimal for most use cases. Defines where to obtain Correlation ID and Message ID in incoming messages. |
<expression-message-info-mapping messageIdExpression="#[java.util.UUID.randomUUID().toString()]" correlationIdExpression="#[xpath3('//order/@id')]"/> |
|
Store Prefix | Defines the prefix of the ObjectStore names |
storePrefix="split_" |
Collection Aggregator will aggregate all the split messages into a single payload.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="splitterprojectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/splitter" allowedMethods="POST" doc:name="HTTP"/>
<splitter expression="#[xpath('//Employee')]" doc:name="Splitter"></splitter>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<collection-aggregator failOnTimeout="true" doc:name="Collection Aggregator">
<expression-message-info-mapping messageIdExpression="#[message.id]" correlationIdExpression="#[message.correlationId]"/>
</collection-aggregator>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<file:outbound-endpoint path="src/test/resources/out" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Now, you know how to use Splitter and Collection Aggregator within a Mule flow.
Here is the video tutorial:
Opinions expressed by DZone contributors are their own.
Comments