# 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 `./build.sh` (or `build.bat` on windows) 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`

               InitialContext initialContext = getContext(0);

2. We look-up the JMS queue object from JNDI

               Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");

3. We perform a lookup on the XA Connection Factory

               XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");

4. We create a JMS XAConnection

               connection = cf.createXAConnection();

5. We Start the connection

               connection.start();

6. We create a JMS XASession

              XASession xaSession = connection.createXASession();

7. We create a normal session

              Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

8. We create a normal Message Producer

               MessageProducer normalProducer = normalSession.createProducer(queue);

9. We get the JMS Session

              Session session = xaSession.getSession();

10. We create a message consumer

              MessageConsumer xaConsumer = session.createConsumer(queue);

11. We create two Text Messages

              TextMessage helloMessage = session.createTextMessage("hello");
              TextMessage worldMessage = session.createTextMessage("world");

12. We get the Transaction Manager

              javax.transaction.TransactionManager txMgr = TransactionManager.transactionManager();

13. We start a transaction

              txMgr.begin();

14. We get the JMS XAResource

              XAResource xaRes = xaSession.getXAResource();

15. We enlist the resources in the Transaction work

              Transaction transaction = txMgr.getTransaction();
              transaction.enlistResource(new DummyXAResource());
              transaction.enlistResource(xaRes);

16. We send two messages.

             normalProducer.send(helloMessage);
             normalProducer.send(worldMessage);

17. We receive the messages

              TextMessage rm1 = (TextMessage)xaConsumer.receive();
              System.out.println("Message received: " + rm1.getText());
              TextMessage rm2 = (TextMessage)xaConsumer.receive();
              System.out.println("Message received: " + rm2.getText());

18. We roll back the transaction

              txMgr.rollback();

19. We create another transaction

              txMgr.begin();
              transaction = txMgr.getTransaction();

20. We enlist the resources to start the transaction work

              transaction.enlistResource(new DummyXAResource());
              transaction.enlistResource(xaRes);

21. We receive those messages again

               rm1 = (TextMessage)xaConsumer.receive();
               System.out.println("Message received again: " + rm1.getText());
               rm2 = (TextMessage)xaConsumer.receive();
               System.out.println("Message received again: " + rm2.getText());

22. We commit

              txMgr.commit();

23. We check that no more messages are received.

              TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
              if (rm3 == null)
              {
                 System.out.println("No message received after commit.");
              }
              else
              {
                 result = false;
              }

24. 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

               finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }