Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a message in a jms queue

I am using activemq to pass requests between different processes. In some cases, I have multiple, duplicate message (which are requests) in the queue. I would like to have only one. Is there a way to send a message in a way that it will replace an older message with similar attributes? If there isn't, is there a way to inspect the queue and check for a message with specific attributes (in this case I will not send the new message if an older one exists).

Clarrification (based on Dave's answer): I am actually trying to make sure that there aren't any duplicate messages on the queue to reduce the amount of processing that is happening whenever the consumer gets the message. Hence I would like either to replace a message or not even put it on the queue.

Thanks.

like image 658
Udi Avatar asked Oct 24 '08 18:10

Udi


People also ask

Can a JMS queue have multiple consumers?

You can certainly have multiple consumers. As many as you want/need actually. A queue makes sure that every message is processed once and only once by a consumer. A topic is used that one message needs to be processed to multiple subscribers.


2 Answers

This sounds like an ideal use case for the Idempotent Consumer which removes duplicates from a queue or topic.

The following example shows how to do this with Apache Camel which is the easiest way to implement any of the Enterprise Integration Patterns, particularly if you are using ActiveMQ which comes with Camel integrated out of the box

from("activemq:queueA").
  idempotentConsumer(memoryMessageIdRepository(200)).
  header("myHeader").
  to("activemq:queueB");

The only trick to this is making sure there's an easy way to calculate a unique ID expression on each message - such as pulling out an XPath from the document or using as in the above example some unique message header

like image 113
James Strachan Avatar answered Sep 21 '22 20:09

James Strachan


You could browse the queue and use selectors to identify the message. However, unless you have a small amount of messages this won't scale very well. Instead, you message should just be a pointer to a database-record (or set of records). That way you can update the record and whoever gets the message will then access the latest version of the record.

like image 44
Dave Avatar answered Sep 17 '22 20:09

Dave