JMS XA Send Example

This example shows you how message sending behaves in an XA transaction in ActiveMQ. When a message is sent within the scope of an XA transaction, it will only reach the queue once the transaction is committed. If the transaction is rolled back the sent messages will be discarded by the server.

ActiveMQ is JTA aware, meaning you can use ActiveMQ in a XA transactional environment and participate in XA transactions. It provides the javax.transaction.xa.XAResource interface for that purpose. Users can get a XAConnectionFactory to create XAConnections and XASessions.

In this example we simulate a transaction manager to control the transactions. First we create an XASession and enlist it in a transaction through its XAResource. We then send two words, 'hello' and 'world', with the session, let the transaction roll back. The messages are discarded and never be received. Next we start a new transaction with the same XAResource, but this time we commit the transaction. Both messages are received.

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2.            InitialContext initialContext = getContext(0);
            
  3. We look-up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
            
  5. We perform a lookup on the XA Connection Factory
  6.            XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");
            
  7. We create a JMS XAConnection
  8.            connection = cf.createXAConnection();
            
  9. We Start the connection
  10.            connection.start();
            
  11. We create a JMS XASession
  12.           XASession xaSession = connection.createXASession();
           
  13. We create a normal session
  14.           Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
  15. We create a normal Message Consumer
  16.            
               MessageConsumer normalConsumer = normalSession.createConsumer(queue);
               normalConsumer.setMessageListener(new SimpleMessageListener());
               
           
  17. We get the JMS Session
  18.           Session session = xaSession.getSession();
           
  19. We create a message producer
  20.           MessageProducer producer = session.createProducer(queue);
           
  21. We create two Text Messages
  22.           
              TextMessage helloMessage = session.createTextMessage("hello");
              TextMessage worldMessage = session.createTextMessage("world");
              
           
  23. We create a transaction
  24.           Xid xid1 = new XidImpl("xa-example1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
           
  25. We get the JMS XAResource
  26.           XAResource xaRes = xaSession.getXAResource();
           
  27. We begin the Transaction work
  28.           xaRes.start(xid1, XAResource.TMNOFLAGS);
           
  29. We do work, sending two messages.
  30.           
              producer.send(helloMessage);
              producer.send(worldMessage);
              
           
  31. We check the result, it should receive none!
  32.           checkNoMessageReceived();
           
  33. We stop the work
  34.           xaRes.end(xid1, XAResource.TMSUCCESS);
           
  35. We prepare
  36.           xaRes.prepare(xid1);
           
  37. We roll back the transaction
  38.           xaRes.rollback(xid1);
           
  39. We check no messages should be received!
  40.           checkNoMessageReceived();
           
  41. We create another transaction
  42.           Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
           
  43. We start the transaction
  44.           xaRes.start(xid2, XAResource.TMNOFLAGS);
           
  45. We re-send those messages
  46.            
               producer.send(helloMessage);
               producer.send(worldMessage);
               
           
  47. We stop the work
  48.           xaRes.end(xid2, XAResource.TMSUCCESS);
           
  49. We prepare
  50.           xaRes.prepare(xid2);
           
  51. We check that no messages should be received at this moment
  52.           checkNoMessageReceived();
           
  53. We commit!
  54.           xaRes.commit(xid2, false);
           
  55. We check that all messages are received.
  56.           checkAllMessageReceived();
           
  57. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  58.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }