JMS XA Receive Example

This example demonstrates receiving a message within the scope of an XA transaction. When using an XA transaction the message will only be acknowledged and removed from the queue when the transaction is committed. If the transaction is not committed the message maybe redelivered after rollback or during XA recovery.

ActiveMQ is JTA aware, meaning you can use ActiveMQ in an 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 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.

Example step-by-step

To run the example, simply type mvn verify 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 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 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 send two messages.
  30.           
             normalProducer.send(helloMessage);
             normalProducer.send(worldMessage);
              
           
  31. We receive the messages
  32.           
              TextMessage rm1 = (TextMessage)xaConsumer.receive();
              System.out.println("Message received: " + rm1.getText());
              TextMessage rm2 = (TextMessage)xaConsumer.receive();
              System.out.println("Message received: " + rm2.getText());
              
           
  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 create another transaction
  40.           Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
           
  41. We start the transaction
  42.           xaRes.start(xid2, XAResource.TMNOFLAGS);
           
  43. We receive those messages again
  44.            
               rm1 = (TextMessage)xaConsumer.receive();
               System.out.println("Message received again: " + rm1.getText());
               rm2 = (TextMessage)xaConsumer.receive();
               System.out.println("Message received again: " + rm2.getText());
                
           
  45. We stop the work
  46.           xaRes.end(xid2, XAResource.TMSUCCESS);
           
  47. We prepare
  48.           xaRes.prepare(xid2);
           
  49. We commit!
  50.           xaRes.commit(xid2, false);
           
  51. We check that no more messages are received.
  52.           
              TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
              if (rm3 == null)
              {
                 System.out.println("No message received after commit.");
              }
              else
              {
                 result = false;
              }
              
           
  53. 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
  54.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }