Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JMS listener acknowledging even when exception

I am using JMS to send/receive messages to my SQS queue, however i am unable to redeliver the message when there is an exception even while using client_acknowledge. How to achieve this? I tried a simple test,

@JmsListener(destination = "test-normalqueue")
public void receiveNormalQueue(String message)
{

    try {
        logger.info("message received in normal queue: " + message);
        throw new NullPointerException();

    } catch (Exception e) {

        logger.error(LoggingUtil.getStackTrace(e));;
    }

}

Even after exception message doesnt come back to queue.

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(getSQSConnectionFactory());
    factory.setConcurrency("1-2");
    factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    return factory;
}
like image 977
Bhargav Avatar asked May 06 '17 06:05

Bhargav


People also ask

How do you handle exceptions in JMS?

An exception that is thrown by a JMS 1.1 API call is often the result of a lower level problem which is reported by another exception that is linked to this exception. Your application can obtain a linked exception by calling either the JMSException. getLinkedException() method or the Throwable. getCause() method.

How do I stop JMS message listener?

First, on your main thread you need to lock the message listener after starting your JMS connection and invoke the message listener “wait” method. On your message listener, you need to lock again the message listener and then invoke the “notify all” method. Save this answer. Show activity on this post.

How do you check if JMS listener is running?

The status of the listeners is always available by running the list listeners command from the user interface or the Transaction Server console. If a listener becomes disconnected, the Transaction Server can attempt to re-connect to the queue multiple times before it fails with a connection error.

How does Spring JMS listener work?

What is a Spring JMS Listener? In order to asynchronously receive JMS messages, Spring offers a solution to create message-driven POJOs (MDP). A message listener container is used to receive messages from a JMS broker. The container is a wrapper of sorts that calls a simple POJO listener class when a message arrives.


1 Answers

You have to use transactions with the DMLC.

Use Session.AUTO_ACKNOWLEDGE and setSessionTransacted(true).

If the listener exits normally, the message is removed. If the listener throws an exception, the message will be rolled-back onto the queue.

You can also use client mode with transactions, but you have to acknowledge successful messages yourself.

You don't have to use transactions with a SimpleMessageListenerContainer but you still have to throw an exception to get the message requeued.

like image 170
Gary Russell Avatar answered Oct 21 '22 13:10

Gary Russell