Thursday, November 9, 2023

Idempotent Message Validator in Mule 4

Here, we will see how to use Idempotent Message Validator in Mule 4

Idempotent Filter is a powerful tool, can be used to prevent duplicate messages. 

Typically, MuleSoft will receive duplicate data..when processing orders, financial transactions, end user may click the submit button twice etc. :-) 

We can use any incoming attribute of the Mule message as an ID, or you can compute the ID by configuring a DataWeave expression.

The Idempotent filter depends on an Object Store, so we need to setup an Object Store connector (No persistent) and configure TTL and Expiration parameters.

In this example, Entry TTL : 10 seconds. Which means, the ID will be stored in Object Store for 10 seconds. During this time if any request comes with same id, that particular request will be considered as "DUPLICATE" and flow will be routed to exception flow. Else, request will be considered as new and flow continues as a happy path.


HTTP - Request Properties:


Set Variable Properties:



Idempotent Properties:

%dw 2.0
import dw::Crypto
output application/octet-stream
---
Crypto::hashWith(vars.tid as String,'MD5')


Transform Message Properties:




Transform Message (On Propagate) Properties:


Object Store Properties:



Testing:
http://localhost:8081/test?transactionId=101
First time, we can get positive response


Next time (within 10 seconds), we will get negative response as "Duplicate Message found"


Source code: 


Ref: MuleSoft documentation

No comments:

Post a Comment