概述
我们在启动JBoss后通常使用如下代码设定消息的TimeToLive
javax.jms.MessageProducer; MessageProducer msgProducer = null; session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); msgProducer = session.createProducer(responseQ); msgProducer.setTimeToLive(Long.valueOf(messageTtl));
但发现消息超过设定的 messageTtl 时间后仍然在消息队列中,所以有人误解为JBoss 中 对 JMS 消息设定 TimeToLive 没有实现。
原因
误解的本身是对JMS标准没有理解准确,TimeToLive意思是当消息队列上有消费者,且消息超过TimeToLive时间没有被消费者消费,则消息被移除,JBoss被异常到ExpirationQueue。如下摘自https://community.jboss.org/wiki/JBMMessageExpiration:
The setTimeToLive() can be used to expire a JMS message. However, the messages would remain on the given destination beyond the time set to expire, if there are no message listeners. The messages sent to any destination with having setTimeToLive() attribute may not expire unless there‘s a message listener attached to given destination. The messages marked to expire are validated and sent to the ExpiryQueue, at the time of processing by the attached message listener. IOW the messages which remain on the destination beyond the expiry limit, would expire (moved to the ExpiryQueue) by the time a listener is attached to the given destination.