Skip to content
Advertisement

How can I acknowledge a JMS message on another thread and request for redelivery of unacknowledged messages?

Step 1: I need to receive message by one thread.

Step 2: Process and sending ack and redelivery request (throwing exception) by another thread.

Sample code:

List<Message> list=new ArrayList();

@JmsListener(destination = "${jms.queue-name}", concurrency = "${jms.max-thread-count}")
public void receiveMessage(Message message) throws JMSException,UnsupportedEncodingException {
   list.add(message)
}
  
void run() {
   foreach(Message message:list) {
      //need to send ack or throw exception for redeliver if error
   }
}

Now another thread will start and process the list which contains data then how can I send an acknowledgement or throw an exception for redelivery?

Advertisement

Answer

Typically you’d let your framework (e.g. Spring) deal with concurrent message processing. This is, in fact, one of the benefits of such frameworks. I don’t see any explicit benefit to dumping all the messages into a List and then manually spawning a thread to process it. Spring is already doing this for you via the @JmsListener by invoking receiveMessage in a thread and providing configurable concurrency.

Furthermore, if you want to trigger redelivery then you’ll need to use a transacted JMS session and invoke rollback() but JMS sessions are not threadsafe so you’ll have to control access to it somehow. This will almost certainly make your code needlessly complex.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement