JMS Timeout or TimeToLive

13,014

You have to make use of some implementation specific functionality to fulfill your requirements. The JMS specification does neither define which action is taken with a timed out message, nor does it offer you any reasonable criteria selection when polling messages from a queue.

Most (if not all) JMS implementations do however offer the concept of DLQs (dead letter queues). If a message cannot be delivered to a regular consumer or times out, the JMS implementation will most likely be able to move the message to a DLQ, which is basically also a regular queue with its own listener.

So, if you set up two queues, Q1 and Q2 and configure Q2 as a DLQ for Q1, you would do your normal request processing in a listener on Q1 and implement an additional listener for Q2 to do the error/timeout handling.

Share:
13,014
Paul
Author by

Paul

Updated on June 04, 2022

Comments

  • Paul
    Paul almost 2 years

    I am fairly new to Java EE and JMS and am looking at doing an implementation using JMS.

    Think of the following scenario:

    Scenario

    A user hits a servlet. A message is then put into a JMS server/Queue from this servlet. A response is then sent back to the user saying "Message Queued".

    Option 1

    The consumer/MDB receives the message from the JMS queue and processes it. This is normal operation and pretty standard.

    Option 2

    There is no consumer(for what ever reason) or the receiver is processing messages too slow. So what I would like is for the message in the queue to timeout. Once timed out, and email should be sent etc (email is just as an example).

    Reading the API spec/Java EE 6 tutorial I have found in the QueuSender class

    void send(Message message, int deliveryMode, int priority, long timeToLive) 
    

    So by settings the timeToLive the message will be evicted from the queue. The problem is that the is no "interface/call back" to know that the message was evicted. It just disappears. Or am I mistaken?

    Another approach I thought of was for a thread to monitor the queue and evict messages that are "expired" and pull them from the queue. But I don't think that is possible, is it?

    Any light shed on this matter would greatly be appreciated.