Thursday, March 24, 2016

MuleSoft Application Development: Using Idempotent Message Filter

Introduction

Here is the question:
How can we prevent a GUI user from submitting the same order multiple times with a short period of time?

Idempotent Message Filter is the answer. This filter will reject the submission of orders with same ID. This ID can be defined in MEL. This article explains how to setup idempotent message filter in a mule application flow.



Setup A Mule Flow

The diagram below shows the flow setup. Note that, we need to convert payload from stream to string in order to have the filter works properly.





    
        
    



    
        
        
        
            
        
        
        
        
            
        
    





Idempotent Message Filter Configuration

The follow two diagrams show how to configure the objectstore required for Idempotent Message Filter. The reason we persist the object store is that if for some reason, the mule runtime crushes, once the runtime starts again, the filter will still work.

The TTL should be less than 1 minutes. If it is too long, we may block the legitimate update cases. If it is too short, we may have the collisions.

Test Idempotent Message Filter

Here is the simple json payload for the testing:
{ 
    "name"   : "John Smith"
}

Use Postman send the first request, we should get message back with "YAY!". If you send again within few seconds, you will get the message of: "You cannot enter the same request with 60 seconds". You should also see the exception as the following:


ERROR 2016-03-24 19:04:51,112 [[poc-basic].HTTP_Listener_Configuration.worker.01] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : Message has been rejected by filter. Message payload is of type: String
Type                  : org.mule.api.routing.filter.FilterUnacceptedException
Code                  : MULE_ERROR--2
JavaDoc               : http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/routing/filter/FilterUnacceptedException.html
Payload               : { 
    "name"   : "John Smith"
}
********************************************************************************
Exception stack is:
1. Message has been rejected by filter. Message payload is of type: String (org.mule.api.routing.filter.FilterUnacceptedException)
  org.mule.processor.AbstractFilteringMessageProcessor:69 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/routing/filter/FilterUnacceptedException.html)
********************************************************************************
Root Exception stack trace:
org.mule.api.routing.filter.FilterUnacceptedException: Message has been rejected by filter. Message payload is of type: String
 at org.mule.processor.AbstractFilteringMessageProcessor.filterUnacceptedException(AbstractFilteringMessageProcessor.java:69)

This means the filter is working!


Conclusion

  • The purpose of Idempotent Message Filter is to prevent the same message passing through the flow. It is very import if you don't want the POST, UPDATA operation to submit multiple times within a short period of time
  • The message id cannot be stream. It must be serializable Java objects, such as String.
  • The TTL should be less than 1 minute

Reference

MuleSoft Document: https://docs.mulesoft.com/mule-user-guide/v/3.7/idempotent-filter

2 comments:

  1. What do Information Technology Consultants do?
    Statistics

    ReplyDelete
  2. Excellent website. Lots of useful information here, thanks in your effort! . For more information please visit

    Mulesoft online training

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...