JMS processing same message in onMessage()

338 Views Asked by At

I have a JMS 2.0 MessageListener which seems to be sporadically reprocessing messages even when they have been successfully processed (confirmed via a log). I am suspecting that a session.commit() needs to be done but I am unsure because in a vast majority of cases, the messages are NOT retried. From what I understand, AUTO_ACKNOWLEDGE is the default but again, I am not so sure how it works for SessionAwareMessageListener.

The relevant spring.xml section looks something like this

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        :
        :
        <property name="messageListener" ref="myMessageListener" />
        <property name="maxConcurrentConsumers" value="1" />
        <property name="receiveTimeout" value="5000" />
        <property name="sessionTransacted" value="true" />
    </bean>

MessageListener implementation is as follows

public class MyMessageListener implements SessionAwareMessageListener {

   // All spring autowired objects
   :
   :
   @Override
   public void onMessage(Message message, Session session)
   {
     logger.debug("JMSMessage ID : " + message.JMSMessageId, "Entering onMessage() ...");
     logger.debug("JMSMessage ID : " + message.JMSMessageId, "Retry Count : " + message.getIntProperty("JMSXDeliveryCount"));
     try
     {
     
     }
     catch(JMSException e)
     {
        // Log something 
        throw new RuntimeException(e);
     }
     catch(Exception ex)
     {
        if(certain types of exceptions)
        {
          session.rollback();
          System.Exit(1);
        }
        
        throw new RuntimeException(ex);

     }
     // THE FOLLOWING IS THE LAST LINE IN onMessage()
     logger.debug("JMSMessage ID : " + message.JMSMessageId,"Completed successfully !");

   }


}

So, almost all the messages that I see now have this in the logs

:
JMSMessage Id : 1234, Entering onMessage()
JMSMessage Id : 1234, Retry count : 1
:
JmsMessage Id : 1234, Completed successfully!
JmsMessage Id : 3344, Entering onMessage() // New message taken up for processing.
JMSMessage Id : 3344, Retry count : 1

The problem is that once in a while (after thousands of messages), I see this in the logs

:
JMSMessage Id : 5566, Entering onMessage()
JMSMessage Id : 5566, Retry count : 1
:
JmsMessage Id : 5566, Completed successfully!
JMSMessage Id : 5566, Entering onMessage() // WHY IS JMS PROCESSING THE SAME MESSAGE (MESSAGEID : 5566) AGAIN ?
JMSMessage Id : 5566, Retry count : 2 
:
:
1

There are 1 best solutions below

0
On

When you have sessionTransacted set to true acknowledge mode is ignored, there is even a special value that can be set to denote that it is not being used, from other examples I see this:

<property name="sessionAcknowledgeModeName" value="SESSION_TRANSACTED"/>

According to Gary Russell's answer to the stackoverflow question Spring DMLC message consumption: auto_ack vs Transacted Session, if you have sessionTransacted set to true with a DMLC, the session is committed by the DMLC after the listener is called, if the listener throws an exception the transaction is rolled back.