Search code examples
javajmsmq

MQ Message rollback


I'm trying to read messages from an MQ queue in Java and I'm a little confused about the flow when comes time to roll back when handling exceptions.

I have a reader class that runs in a loop and look for message. When I initialize the reader, it creates a connection and a session. Then when comes time to read a message in the loop, it creates an MQQueue and MQQueueReceiver. This works fine and I get a JMSMessage out of the queue, which is then handed to another thread for work. If the worker thread fails, I want to put that message back onto the queue (so it gets retried by other nodes in my cluster).

My question is, how do I roll back? I noticed there's a rollback() method in the MQQueueSession class. But I'm reusing the same session for all my messages. Am I to understand that a new session should be created for each message instead of reusing the same one over and over?

This is counter intuitive to me.

Also the session is created with connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);. Should I use another flag, other than CLIENT_ACKNOWLEDGE?


Solution

  • You should use Session.SESSION_TRANSACTED flag and Session.commit/Sessiuon.rollback to commit/rollback all messages done in the current transaction