Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS rollback

Tags:

rollback

jms

I have a process which involves sending a JMS message. The process is part of a transaction. If a later part of the transaction fails, a part that is after a previous part that sent the message, I need to cancel the message. One thought I had was to somehow set on the message that it is not to be picked up for a certain amount of time, and if I need to rollback, then I could go and cancel the message. Not knowing messaging, I do not know if the idea is possible. Or, is there a better idea? Thanks

like image 446
bmw0128 Avatar asked Feb 05 '09 18:02

bmw0128


People also ask

Can a JMS session be transactional?

JMS applications can run local transactions by first creating a transacted session. An application can commit or roll back a transaction.

What is session in JMS?

A session is a single-threaded context for producing and consuming messages. You use sessions to create the following: Message producers. Message consumers.

How do I rollback a JMS message?

Step 3: Commit or Roll Back the JMS Transacted SessionThe rollback() method cancels any messages sent during the current transaction and returns any messages received to the messaging system. If either the commit() or rollback() methods are issued outside of a JMS transacted session, a IllegalStateException is thrown.

What are JMS transactions?

In a JMS client, you can use local transactions to group message sends and receives. The JMS API Session interface provides commit and rollback methods that you can use in a JMS client. A transaction commit means that all produced messages are sent and all consumed messages are acknowledged.


2 Answers

You can use JMS and JTA (Java Transaction API) together. When doing that, the sending of a JMS message or the consumption of a received message actually happens atomically as part of the transaction commit.

What does this mean? If the transaction fails or is rolled back, the "sent" message doesn't go out and any "received" messages aren't really consumed. All handled for you by your JMS and JTA provider.

You need to be using a JMS implementation that supports JTA. Sounds like you're already using transactions, so it might be a matter of doing some configuration to make it happen (waving hand vigorously...).

I've had experience using this (BEA WebLogic 7 w/ BEA WebLogic Integration). Worked as advertised -- "the outside world" saw no impact of JMS stuff I tried unless the transaction committed successfully.

Earlier versions of this linked to a Java page describing JMS/JTA integration generally. The page went stale and I don't see an equivalent replacement. This javadoc is for a JMS interface related to this capability.

like image 112
John M Avatar answered Oct 16 '22 04:10

John M


What you have described is an XA transaction. This allows a transaction to scope across multiple layers i.e. JMS provider, DB or any other EIS. Most containers can be configured to use both non XA and none XA transaction so check your container settings!

For example if you are using JMS with XA transactions the following is possible.

Start Transaction
      |
   DB Insert
      |
   Send JMS Msg
      |
   More DB Inserts
      | 
   Commit Transaction  <- Only at this point will the database records be inserted and the JMS message sent.

XA Tranactions are only available in full Java EE containers so XA transactions are not available in Tomcat.

Good luck!

Karl

like image 31
Karl Avatar answered Oct 16 '22 06:10

Karl