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 provides a mechanism for you to set expiry on messages. Look at the below two references
Also mind that clocks between client - broker need to be in sync, for the expiry to work properly. If the clocks is not in sync, then the expiry set from the client, may already be expired when the message is received on the broker. Or the client time is ahead of the broker, so the expiry is longer than the 10 secs.
It kinda beats me why the expiry is client time based. So AMQ offers a plugin, to fix this by realigning the time, to be broker based only. See http://activemq.apache.org/timestampplugin.html
Well the setJMSExpiration(long expiration):
is something you MUST not call when you are the client. See my talk on this a bit on the ActiveMQ forum.