Mule Idempotent Filter

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Idempotent Filter is an important message filter in Mule platform which ensures that only unique messages are received by a service by checking the unique ID of the incoming message.
We use Idempotent Filter to avoid any duplicate message in our Mule flow.

Idempotent Filter can be configured various ways. We can use Object store with Idempotent Filter.

Mule provides two types of object stores:-

  • In-memory store – Prior to Mule 3.5.0, in-memory store was the default. As of Mule 3.5.0, persistent store is the default.
  • Persistent store – Mule persists data when an object store is explicitly configured to be persistent. Mule creates a default persistent store in the file system.

Here we will see various configuration with Idempotent Filter.

Idempotent Filter with simple-text-file-store :-

We can configure it following way :-

<idempotent-message-filter idExpression="#[message:payload]" doc:name="Idempotent Message" throwOnUnaccepted="true" onUnaccepted="ValidationFailFlow">
   <simple-text-file-store directory="./idempotent" />
</idempotent-message-filter>

Here you can see that we have configured our Idempotent Filter with simple-text-file-store.

You will see that we will run our Mule application it will generate text file in our Mule application where it will store all the message with ID to avoid the duplicate :-

Untitled

Idempotent Filter with in-memory-store :-

We can configure it following way :-

<idempotent-message-filter idExpression="#[message:payload]" doc:name="Idempotent Message" throwOnUnaccepted="true" onUnaccepted="ValidationFailFlow">
    <in-memory-store name="myInMemoryObjectStore" entryTTL="120" expirationInterval="3600" maxEntries="60000" />
</idempotent-message-filter>

Here you can see that we have configured our Idempotent Filter with in-memory-store.

Idempotent Filter with managed-store:-

We can configure it following way :-

<idempotent-message-filter idExpression="#[message:payload]" doc:name="Idempotent Message"  throwOnUnaccepted="true"  onUnaccepted="ValidationFailFlow">

      <managed-store storeName="myNonPersistentManagedObjectStore" maxEntries="-1" entryTTL="20000" expirationInterval="5000"/>
</idempotent-message-filter>

Here you can see that we have configured our Idempotent Filter with managed-store.

Now, we can configure with any of the options shown above, we will get following Mule flow :-

Untitled

Our Mule Config will be :-

Untitled

Testing our application

So, we will test our application by hitting the following url http://localhost:8081/path/test for the first time we will get :-

Untitled

 

For the first time we will see browser display “passed” as the message payload is new and unique (path/test is the message payload here)

And then if we hit the same url http://localhost:8081/path/test again, will get the following :-

Untitled

For the second time we will see browser display “EXCEPTION” as the message payload is now same and duplicate (path/test is the message payload here)

Conclusion

So, we can see that, Idempotent Filter is a very important Component or you can say a Filter in our Mule flow, which is designed to prevent duplicate payload in our flow. There may be different ways available in Mule to configure this Idempotent Filter, but we can always choose the way we need based on our requirement, and that can be either object-store or in-memory or even simple-text-file-store.

Hope I am able to make it clear on how to implement Idempotent Filter with different ways and you can now try by your own and experiment with this powerful filter of Mule.
Hope you enjoyed this simpler version

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Anirban Sen Chowdhary

Anirban Sen Chowdhary is an information technology professional currently working on Java/J2ee, Esb and Integration platform. For more information, please visit http://anirbansenchowdhary.com and you can also follow https://twitter.com/Anir37

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>