JMS XA with JTA Example

This example shows you how to use JTA interfaces to control transactions with HornetQ. JTA provides facilities to start and stop a transaction and enlist XA resources into a transaction.

HornetQ is JTA aware, meaning you can use HornetQ 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 get a transaction manager from JBoss JTA to control the transactions. First we create an XASession for receiving and a normal session for sending. Then we start a new xa transaction and enlist the receiving XASession through its XAResource. We then send two words, 'hello' and 'world', receive them, and let the transaction roll back. The received messages are cancelled back to the queue. Next we start a new transaction with the same XAResource enlisted, but this time we commit the transaction after receiving the messages. Then we check that no more messages are to be received. In each transaction a dummy XAResource is also enlisted to show the transaction processing information.

Example step-by-step

To run the example, simply type mvn verify from this directory. It will download the JBoss JTA jars before it launches the example.

  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 Producer
  16.            
               MessageProducer normalProducer = normalSession.createProducer(queue);
               
           
  17. We get the JMS Session
  18.           Session session = xaSession.getSession();
           
  19. We create a message consumer
  20.           MessageConsumer xaConsumer = session.createConsumer(queue); 
           
  21. We create two Text Messages
  22.           
              TextMessage helloMessage = session.createTextMessage("hello");
              TextMessage worldMessage = session.createTextMessage("world");
              
           
  23. We get the Transaction Manager
  24.           javax.transaction.TransactionManager txMgr = TransactionManager.transactionManager();
           
  25. We start a transaction
  26.           txMgr.begin();
           
  27. We get the JMS XAResource
  28.           XAResource xaRes = xaSession.getXAResource();
           
  29. We enlist the resources in the Transaction work
  30.           
              Transaction transaction = txMgr.getTransaction();
              transaction.enlistResource(new DummyXAResource());
              transaction.enlistResource(xaRes);
              
           
  31. We send two messages.
  32.           
             normalProducer.send(helloMessage);
             normalProducer.send(worldMessage);
              
           
  33. We receive the messages
  34.           
              TextMessage rm1 = (TextMessage)xaConsumer.receive();
              System.out.println("Message received: " + rm1.getText());
              TextMessage rm2 = (TextMessage)xaConsumer.receive();
              System.out.println("Message received: " + rm2.getText());
              
           
  35. We roll back the transaction
  36.           txMgr.rollback();
           
  37. We create another transaction
  38.           
              txMgr.begin();
              transaction = txMgr.getTransaction();
              
           
  39. We enlist the resources to start the transaction work
  40.                    
              transaction.enlistResource(new DummyXAResource());
              transaction.enlistResource(xaRes);
              
           
  41. We receive those messages again
  42.            
               rm1 = (TextMessage)xaConsumer.receive();
               System.out.println("Message received again: " + rm1.getText());
               rm2 = (TextMessage)xaConsumer.receive();
               System.out.println("Message received again: " + rm2.getText());
                
           
  43. We commit
  44.           txMgr.commit();
           
  45. We check that no more messages are received.
  46.           
              TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
              if (rm3 == null)
              {
                 System.out.println("No message received after commit.");
              }
              else
              {
                 result = false;
              }
              
           
  47. 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
  48.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }