Ari Aaltonen Ari Aaltonen - 1 month ago 33
Java Question

How reliable is JMS rollback?

Code...

@Transactional
@JmsListener(destination = "QueueA")
public void process(String input) {
doSomethingWhichMayThrowException(input);
}


Consider following situation where...


  1. Transaction is started (using Spring @Transactional annotation)

  2. Persistent JMS message is read from QueueA (Queue use disk as message storage)

  3. Disk is full and do not accept any write operations

  4. Exception happens and transaction is rolled back






Is message lost?

If it's not then how message is read from queue under transaction (step 2)?

Is some kind of a queue browser used so message is read from queue but not consumed?

Answer

Is message lost?

No, the message is NOT lost as the transaction is rolledback.

If it's not then how message is read from queue under transaction (step 2) ?

Once after the message listener's process()/onMessage() method completes and returns with a success or exception, then internally message acknowledgment (default is AUTO_ACKNOWLEDGE) happens (which is the last thing implicitly happens) to the JMS provider(IBMMQ, ActiveMQ, SonicMQ, etc..) which tells that the transaction is successful or not.

If the Transaction is successful, JMS provider deletes the message from the queue/topic.

If the Transaction is NOT successful, JMS provider preserve the message as is (until message TimetoLive expires).

Is some kind of a queue browser used so message is read from queue but not consumed ?

You can think that it is like queue browser concept, but it is upto the implementation of the JMS provider how do they implement this internally. In order to achieve this, the message broker just reads the message content, but do not delete the actual message from the queue/topic until the acknowledgement is received from the message listener's process()/onMessage() method for the current transaction.