This week, we’re going to study Message Expiration pattern.
When do I use this pattern?
This pattern is applied when messages have to be processed within a given time. For example, a sender may not be interested in flight prices after a minute.
Message Expiration in CPI
In CPI, I will demonstrate the example using AMQP Receiver Adapter. In AMQP Receiver Adapter, Expiration Period can be set in the number of seconds. If nothing is specified, the default value of the specified queue or topic applies.
The example for Message Expiration pattern is similar to the examples for Return Address and Correlation Identifier patterns.
Step 1: Enqueueing Flight Price Request
This integration flow starts immediately and enqueues the flight price request in ‘flightpricerequests’ queue.
The only difference to the examples from the Return Address and Correlation Identifier patterns is the request body and the configuration in the Processing tab of AMQP Adapter.
The request body is:
And, the configuration in the Processing tab of the AMQP Adapter is:
Here, the expiration period is set to 60 seconds because the sender is not interested in flight prices after a minute.
Step 2: Processing Flight Price Requests
Again, this is similar to examples from Return Address and Correlation Identifier patterns. The difference is the response body and the Processing tab of AMQP Receiver Adapter.
The response body is:
And, the configuration in the Processing tab of the AMQP Receiver Adapter is:
Again, the expiration period is set to 60 seconds because the sender is not interested in flight prices after a minute.
Note that setting expiration period to 60 seconds means the message will stay alive in the queue for longer than the sender expects it. The extra time message will stay alive in the queue is the amount of time taken by the receiver to process the request and to put the response in the ‘flightprices’ queue.
Step 3: Processing Flight Price
For simplicity, as usual, the response is logged for monitoring.
Message Expiration pattern is used when processing the message after a certain time is not required.
Hope this helps,
About the Author
More Content by Bhalchandra Wadekar