Claim Check in Mule: the order document stays in the Object Store and only the key travels
- 11 min read
On this page
Code
In the series’ order scenario, some orders arrive with the signed purchase order, a base64 PDF that only the document archive uses. Carrying that document through every step of the path makes each message heavy for nothing. The Claim Check stores the document in a persistent place and sends only a key along to fetch it later. In Mule 4, the place is an object store of the Object Store connector: os:store stores, os:retrieve returns, and the store’s TTL says how long the key is valid.
The problem
The order arrives like this (a summary of the fields):
{ orderId, document: { fileName, mediaType, contentBase64 } }
The contentBase64 is almost all of the message’s size; in the example’s test, it is 200,000 bytes of document for three order fields. The steps in the middle (validate, record, notify the archive) need the orderId and the file name, not the content. Only the document archive needs the bytes, and only at the end.
The pattern in the book
The book starts from two neighboring patterns. The Content Enricher deals with a message that lacks data; the Content Filter removes from the message the data that is of no interest. Sometimes, though, the removal should be only temporary: the message carries data that may be needed later in the flow, but not in all the intermediate steps, and carrying it through every step can hurt performance and make debugging harder (Claim Check).
The question is how to reduce the data volume of messages without losing information. The answer: store the message’s data in a persistent store and pass a Claim Check to the following components, which can use it to retrieve what was stored. The book describes the steps: the component that does the check-in generates a unique key, extracts the data from the message, stores it in a persistent store (a file or a database, for example) associated with the key, removes that data from the message and puts the key in its place. Another component can use a Content Enricher to retrieve the data by the key. The book’s comparison is checked luggage at the airport: the passenger hands over the bag, gets a number that identifies it and collects it at the destination (Claim Check).
Read together with the Content Filter, which the book lists among the related patterns, the check-in works as a Content Filter with a store beside it.
The document leaves the message and comes back at the end
The diagram, redrawn from the official sketch (CC BY 4.0), answers: where does the document go while the message moves on?
In Mule
The Mule 4 documentation has no component called Claim Check: the pattern is the sum of three pieces Mule already has. The store is an object store, which the Object Store connector operates. The Store operation stores a value under a key; Retrieve returns the stored value and, when there is no value for the key and no defaultValue was given, throws OS:KEY_NOT_FOUND (Object Store Connector Reference). Between the two, a Transform builds the message that moves on without the document, the same component as on the Content Filter page.
The key’s lifetime is a store setting. entryTtl is the lifetime of each entry, and expirationInterval says how often the expiration thread runs. In a Mule object store, it only takes effect when entryTtl or maxEntries is also set, and without them the expiration thread does not run; in Object Store v2, it requires entryTtl and, without it, is ignored (Object Store Connector Reference).
The check-in and check-out flow
The diagram answers: who stores, who fetches and what does the archive receive?
In the MuleSoft documentation
The documentation page Enterprise Integration Patterns Using Mule does not list the Claim Check, and no line of it points to the Object Store connector. This page’s pattern is built from the Store and Retrieve operations described above.
The example
The store has a lifetime. In the example, entryTtl is 3 seconds and expirationInterval is 1 second (app.yaml), short enough for the test to see the key expire:
<os:object-store name="eip-claim-check-documents-store" doc:name="Claim Check Documents Object Store" persistent="false"
entryTtl="${eip.claimCheck.ttlSeconds}" entryTtlUnit="SECONDS"
expirationInterval="${eip.claimCheck.expirationIntervalSeconds}" expirationIntervalUnit="SECONDS" />
In the repository: src/main/mule/eip/claim-check/claim-check.xml
The check-in generates the key, stores the document under it and builds the message that moves on with three fields:
<set-variable doc:name="SET vClaimKey" variableName="vClaimKey" value="#['CLM-' ++ uuid()]" />
<os:store doc:name="Check In Order Document" key="#[vars.vClaimKey]" objectStore="eip-claim-check-documents-store">
<os:value><![CDATA[#[output application/java --- {
orderId: payload.orderId,
fileName: payload.document.fileName,
mediaType: payload.document.mediaType default "application/octet-stream",
contentBase64: payload.document.contentBase64,
checkedInAt: now() as String
}]]]></os:value>
</os:store>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{ orderId: payload.orderId, claimKey: vars.vClaimKey, fileName: payload.document.fileName }]]></ee:set-payload>
The os:value uses output application/java: the store keeps a Java map. That is why the response of GET /eip/orders/documents/{claimKey} goes through a Transform that writes the map as JSON.
The document archive is simulated in the same application (claim-check-archive.xml). It receives the message with the key, fetches the document and answers with what it archived, including the fields it received, so the test can prove the content did not travel. The lookup sits in a sub-flow that turns the missing key into APP:NOT_FOUND, answered as 404:
<try doc:name="Try Retrieve Claimed Document">
<os:retrieve doc:name="Check Out Order Document" key="#[vars.vClaimKey]" objectStore="eip-claim-check-documents-store" />
<error-handler>
<on-error-continue doc:name="On Error Continue (claim not found)" type="OS:KEY_NOT_FOUND" logException="false">
<set-variable doc:name="SET vClaimMissing" variableName="vClaimMissing" value="#[true]" />
</on-error-continue>
</error-handler>
</try>
The check-out does not remove the document: the same key stays valid for other components until the TTL runs out. The test cc-check-out-does-not-remove-the-document fetches the document after the archive has already used it and gets 200 with the same content. When only one consumer should read the document, an os:remove (Object Store Connector Reference) after the lookup ends the key.
The main test calls the endpoint over HTTP with a 200,000-byte document and checks what reached the archive:
<munit-tools:assert-that doc:name="Archive received only the claim" expression="#[vars.vCheckIn.body.archive.receivedFields]" is="#[MunitTools::equalTo(['orderId', 'claimKey', 'fileName'])]" />
<munit-tools:assert-that doc:name="Archive got the whole document with the key" expression="#[vars.vCheckIn.body.archive.sizeBytes]" is="#[MunitTools::equalTo(200000)]" />
In the repository: src/test/munit/eip/claim-check-test-suite.xml
Caveats
The example’s store is not for production. The connector reference describes persistent as the choice between a persistent and a transient store, with true as the default (Object Store Connector Reference); the example uses persistent="false", a transient store. A real Claim Check needs a store that every consumer can reach and that lasts as long as the key’s lifetime. On CloudHub, that store is Object Store v2, and the documentation warns that, if the XML has persistent="false", the application does not use Object Store v2 (Object Store v2 Guide).
The TTL has to cover the slowest consumer, and it has a ceiling. Three seconds are enough for the test to see the key expire; the real archive may take hours. In Object Store v2, entryTtl accepts from 1 second to 30 days. With entryTtl set, the TTL is static: it starts at the write and reads do not extend it. Without entryTtl (Mule 4.2.1 and later), the TTL is rolling: each read or write extends the entry (Object Store Connector Reference). A consumer that arrives after the TTL gets OS:KEY_NOT_FOUND; the test cc-claim-expires-after-its-time-to-live waits 5 seconds and gets 404.
Each Object Store v2 value has a 10 MB limit. The store’s total size has no limit, but each value does (FAQ: Object Store v2); a MuleSoft knowledge base article repeats the 10 MB per value limit (The Different Types of MuleSoft Object Stores Explained). The book cites a file or a database as the store. The documentation does not say what to do above the limit; for larger documents, this page’s recommendation is to store the file in a file system or an object storage service and carry only the reference to it in the message.
Each check-in and each check-out is a call to Object Store v2. Every call to Object Store v2, through the connector or the API, counts toward the transactions-per-second limit: 10 TPS per application on the base subscription, 100 with the Premium add-on (FAQ: Object Store v2). In a 2020 Stack Overflow question, with Mule 4, an application hit the 10 TPS limit and got 429 rate limit exceeded; waiting 30 seconds before trying again did not fix it (Stack Overflow: How much time do I have to wait once exceeded the 10 TPS with the Mule 4 Object Store V2?). A Claim Check on every order makes at least two calls per order.
No distributed lock. The same knowledge base article says CloudHub has no distributed lock and does not recommend Object Store v2 for solutions with high concurrency or several workers (The Different Types of MuleSoft Object Stores Explained). In the example, each key is generated by a uuid(), written once and only read after that.
When not to use it
When the message is small, the Claim Check trades one extra field for two calls to the store and a TTL to get right. When no later step will need the data, there is nothing to fetch afterwards: the pattern is the Content Filter, which simply removes it. And when the document already lives in a system with its own identifier (a file storage service, an ECM), the message can carry that identifier, and the check-in to the object store is one copy too many.
Running the example and the tests
git clone https://github.com/brunosouzas/mulesoft-patterns-examples.git
cd mulesoft-patterns-examples
mvn clean test
The project needs Java 17, Maven 3.9+ and the Mule 4.9 runtime. MUnit runs on the Enterprise runtime, so Maven needs access to MuleSoft’s Enterprise repository (credentials in ~/.m2/settings.xml). The example runs with nothing external.
The claim-check-test-suite.xml suite pins the behavior down, observed on Mule 4.9.17 with MUnit 3.7.1:
| Test | What it proves |
|---|---|
cc-only-the-claim-travels-and-the-archive-checks-the-document-out |
Over HTTP, the check-in answers 202; the archive receives only orderId, claimKey and fileName and archives the 200,000 bytes fetched by the key |
cc-check-out-does-not-remove-the-document |
After the archive uses the key, the GET with the same key answers 200, in application/json, with the same document |
cc-claim-expires-after-its-time-to-live |
5 seconds after the check-in (TTL of 3 s, expiration every 1 s), the key gets 404 |
cc-unknown-claim-returns-404-at-the-archive |
A key that was never issued gets 404 from the archive, and nothing is archived |
cc-order-without-document-returns-400 |
An order without the document’s content gets 400, and nothing is stored |
The expiration test waits for the TTL to run out, so the suite takes about 6 seconds.
To call the endpoints, run the application in Anypoint Studio (port 8081) and use the Claim Check entries in requests.http:
### EIP claim check — check a document in; the archive gets only the claim key (the claim expires after 3 seconds)
POST {{base}}/eip/orders/documents
Content-Type: application/json
{ "orderId": "ORD-3701", "document": { "fileName": "po-3701.pdf", "mediaType": "application/pdf", "contentBase64": "JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+PgplbmRvYmoK" } }
### Check the document out: replace the key with the claimKey above (404 once it has expired)
GET {{base}}/eip/orders/documents/CLM-replace-with-the-claim-key
In the repository: requests.http
The GET with the key you received answers 200 in the first 3 seconds and 404 after that. The other patterns, with the Mule component that implements each one, are in the catalog of integration patterns in Mule; the reverse path, from component to patterns, is in the index by component.