简体   繁体   中英

How to filter JMS message using selector in receiver class

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 {

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM