DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Splitter and Collection Aggregator With Mulesoft

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.

Jitendra Bafna user avatar by
Jitendra Bafna
CORE ·
May. 15, 17 · Integration Zone · Tutorial
Like (7)
Save
Tweet
38.67K Views

Join the DZone community and get the full member experience.

Join For Free

Splitter 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.

Image titlePlace 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:

  • IF_NOT_SET (existing correlation IDs are maintained)
  • Alwaysexisting correlation IDs are overridden)
  • Neverno action.  


IF_NOT_SET

enableCorrelation="IF_NOT_SET"

Message Info Mapping


Optional. If this child element is not configured, MuleMessage.getCorrelationId() is used, which is optimal for most use cases. Maps attribute from incoming data to construct Correlation ID and Message ID on outgoing messages.


<expression-message-info-mapping
messageIdExpression=

"[java.util.UUID.randomUUID().toString()]"
correlationIdExpression=

"[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.

Image titleFinally, 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.

Image title

Now, you can verify the file location and check if the message is properly split into five different files.

Image title

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).

Image titleThese 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.Image titleIn 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="&#x0023;[java.util.UUID.randomUUID().toString()]"

correlationIdExpression="&#x0023;[xpath3('//order/@id')]"/>

Store Prefix

Defines the prefix of the ObjectStore names

storePrefix="split_"

Image title

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:


Correlation (projective geometry) Fragment (logic) MuleSoft application Flow (web browser) Flow control (data) Timeout (computing) Payload (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Microservices: Code Examples, Tutorials, and More
  • Understand Source Code — Deep Into the Codebase, Locally and in Production
  • SQL Database Schema: Beginner’s Guide (With Examples)
  • Monitoring Spring Boot Application With Prometheus and Grafana

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo