Skip to content

Mule Chunking Connector ​

The Chunking Connector lets a flow process any stream in chunks. Content of any size, from any source, arrives as a sequence of bounded pieces, cut by size or by record, so the flow never needs the whole of it in memory.

Mule 4 made repeatable streams its default so that a stream can be read more than once and by more than one component at the same time. The connector is built to honor that principle for chunks: its streaming strategies give up as little of it as the memory you allow.

Uses include:

  • sending content onward in parts, as storage and transfer services that accept multipart uploads expect;
  • handing records to another system in batches, a set number of lines or records at a time;
  • working through content larger than the memory available.

The connector has one operation, read-chunked, and needs no global configuration element.

Features ​

  • by-size cuts chunks of a fixed number of bytes, 1 MB by default; the last chunk holds what is left. See Chunking Strategies.
  • by-delimiter cuts chunks of a fixed number of records, each record ending with a delimiter you choose, a newline by default. A maximum chunk size bounds the memory a chunk needs, and it never cuts a record. See Chunking Strategies.
  • Four streaming strategies decide how the chunks are held. The chunks can be read once, in order (the default); by several readers at the same time, through a sliding window; or any number of times, from memory or from a temporary file. Every limit is counted in chunks. See Streaming Strategies.
  • Every chunk is an exact slice of the content. The chunks' data, taken in order, reproduce the content byte for byte.
  • Invalid settings raise CHUNKING errors before any chunk is read. A failure while the chunks are read is always raised, and the content is never reported as having ended early. See Errors.

Quick example ​

With no settings, read-chunked cuts the message payload into chunks of 1 MB, and foreach processes them one at a time:

xml
<chunking:read-chunked/>
<foreach>
    <logger level="INFO" message="#['Chunk $(payload.index): $(payload.length) bytes, last: $(payload.last)']"/>
</foreach>

To cut content into chunks of 1000 lines each, regardless of their size in bytes:

xml
<chunking:read-chunked>
    <chunking:chunking-strategy>
        <chunking:by-delimiter recordsPerChunk="1000"/>
    </chunking:chunking-strategy>
</chunking:read-chunked>
<foreach>
    <logger level="INFO" message="#['Chunk $(payload.index) holds $(payload.recordCount) lines']"/>
</foreach>

Chunking and streaming strategies ​

read-chunked has two optional child elements. Leaving one unset means the default.

Child elementWhat it decidesOptionsLeft unset
chunking-strategyHow the content is cut into chunksby-size, by-delimiterBy size, in chunks of 1 MB
streaming-strategyHow the chunks are heldnon-repeatable, sliding-window, in-memory, file-storenon-repeatable: the chunks can be read once, in order

In Anypoint Studio a strategy left as None, and in Anypoint Code Builder one whose box is left unticked, means the default.

Chunk properties ​

Each chunk has these properties, read in a flow as shown:

PropertyTypeDescription
payload.databinary (byte[])Exactly the chunk's bytes
payload.indexnumberThe chunk's position in the sequence, counting from 0
payload.offsetnumberThe position of the chunk's first byte in the content, counting from 0
payload.lengthnumberThe number of bytes in the chunk
payload.firstbooleantrue for the first chunk
payload.lastbooleantrue for the last chunk, including when the content ends exactly at the end of this chunk
payload.recordCountnumberWhen chunking by delimiter, the number of records the chunk holds; empty (null) when chunking by size

Empty content produces no chunks.

Requirements ​

Mule Runtime 4.9.6 or later, and Java 17. No global configuration element is needed.

Upgrading from 0.2.0 ​

1.0.0 changes the operation's settings and the errors a flow can receive. The chunk's flags are read as payload.first and payload.last; 0.2.0's documentation showed payload.isFirst and payload.isLast instead. A 0.2.0 application needs changes before it runs on 1.0.0, and Upgrade from 0.2.0 lists every one.

Source code ​

The source code and the issue tracker are in the connector's GitHub repository.

Site content released under the Apache License 2.0. Each asset carries its own license. RSS · llms.txt