Camel中的消息自动过期
我有一个实现 Camel 和 ActiveMQ 的系统,用于在一些服务器之间进行通信.我想知道是否有办法在 X 段时间后自动过期并清除发送到队列的消息.由于原始服务器(填充队列)不知道是否有人在接收消息,所以我不希望我的队列增长到太大以至于崩溃.Bonus karma 指向可以提供帮助并提供 java dsl 方式来实现此功能的人.
I have a system implementing Camel and ActiveMQ for communication between some servers. I would like to know if there is a way to automatically expire and clear-out messages sent to a queue after X period of time. Since the originating server (filling the queue) wont know if anyone is picking up the messages, I don't want my queue to grow until its so large that something crashes. Bonus karma points to someone who can help and provide the java dsl way to implement this feature.
解决方案
// expire message after 2 minutes
long ttl = System.currentTimeMillis() + 120000;
// send our info one-way to the group topic
camelTemplate.sendBodyAndHeader("jms:queue:stats", ExchangePattern.InOnly, stats, "JMSExpiration", ttl);
推荐答案
JMS 提供了一种机制让您设置消息的过期时间.看下面两个参考
JMS provides a mechanism for you to set expiry on messages. Look at the below two references
- setJMSExpiration(长期过期):每条消息
- ActiveMQ:如何设置消息过期:每个目标/每个消息
- setJMSExpiration(long expiration): per message
- ActiveMQ: How do I set the message expiration: per destionation/per message
相关文章