0

I want to consume a message from the queue which is present more than 3 minutes in the queue.

Below is my class for consuming. How to define the condition in the selector.

@Async
@JmsListener(destination = "jms/xyz" ,containerFactory = "xyzfactory", selector = "JMSTimestamp= 'morethan 3 minuts'")
public void xyzRecive(Message message) throws JMSException {

}
Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
ak2020
  • 85
  • 2
  • 10
  • 1
    Do you want to consume these messages in order to process them now, or in order to delete them ? If you just want to delete them, consider setting timeToLive on the producer. – Axel Podehl Feb 21 '19 at 08:21

1 Answers1

1

The selector would need to be something like:

"JMSTimestamp > " + (System.currentTimeMillis() + 180000);

However, the selector is set on the underlying JMS consumer when it is created and is immutable so this selector would grow "stale" quickly since time is always moving forward. To change the selector would require closing the existing consumer and creating a new consumer with the new selector. Obviously in this case Spring is handling the creation of the consumer and setting the selector so you'll either need to drop Spring and use the JMS API yourself (which isn't hard).

Another potential solution would be to set the time-to-live on the message to 3 minutes and define an expiry queue on whatever broker you're using and the consume from that expiry queue instead of the main one since all the messages in the expiry queue would be guaranteed to have been on the main queue for at least 3 minutes.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
  • How can I put in selector key.@Async @JmsListener(destination = "jms/xyz" ,containerFactory = "xyzfactory", selector = "JMSTimestamp= 'morethan 3 minuts'") public void xyzRecive(Message message) throws JMSException { } – ak2020 Feb 20 '19 at 20:55
  • I'm not sure I understand your question. It appears to be the same as the original question which I've already addressed in my answer. – Justin Bertram Feb 20 '19 at 21:02