Search code examples
sessionjmsactivemq-classicrollback

ActiveMQ message ordering - Session rollback for JMSException?


I have a Spring JMS Consumer class that reads messages off a queue (implements SessionAwareMessageListener) and processes them for sending off to a web service. We need to preserve the order in which the messages arrive and are processed, as they contain incremental updates to the same data.

To ensure this, we roll back the Session in the listener in case of any recoverable failure, such as a service timeout, so the same message can be retried again. However, if the message has an invalid format or contains bad data, it is discarded (session is not rolled back).

In case of a JMSException, which is thrown by the message.getText() method, I am not clear if I should roll back the session or not. Can this be considered a recoverable error or should the message be discarded in case this error occurs?

The code looks something like this:

public void onMessage(Message message, Session session) throws JMSException {
  try {
    String msgText = ((TextMessage) message).getText();

    // Processing occurs
    // Web service is called

  } catch (JMSException jmse) {
    // log error
    session.rollback(); // Question about this line
  } catch (InvalidMessageException ime) {
    // log error
    // session is NOT rolled back, proceed to next message
  } catch (SocketTimeoutException | AnyOtherRecoverableException excp) {
    // log error
    session.rollback();
  }
}

Solution

  • In order to preserve ordering (sequencing), you have to roll back the message, if there are any exceptions because of the JMS provider (MQ server) failures.

    Also please find the below text from oracle doc on getText() method:

    TextMessage getText() method Throws:

    JMSException - if the JMS provider fails to get the text due to some internal error