JMS XA Heuristic Example

This example shows you how to make an XA heuristic decision through the ActiveMQ Management Interface.

A heuristic decision is a unilateral decision to commit or rollback an XA transaction branch after it has been prepared.

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 a text message, 'hello' and end/prepare the transaction on the XAResource, but neither commit nor roll back the transaction. Another transaction is created and associated with the same XAResource, and a second message, 'world' is sent on behalf of the second transaction. Again we leave the second transaction in prepare state. Then we get the MBeanServerConnection object to manipulate the prepared transactions. To illustrate, we roll back the first transaction but commit the second. This will result in that only the message 'world' is received.

This example uses JMX to manipulate transactions in a ActiveMQ Server. For details on JMX facilities with ActiveMQ, please look at the JMX Example.

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 hello message.
  30.           
              normalProducer.send(helloMessage);
              
           
  31. We stop the work for xid1
  32.           xaRes.end(xid1, XAResource.TMSUCCESS);
           
  33. We prepare xid1
  34.           xaRes.prepare(xid1);
           
  35. We check the none should be received.
  36.           checkNoMessageReceived();
           
  37. We create another transaction
  38.           Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
           
  39. We begin the transaction work
  40.           xaRes.start(xid2, XAResource.TMNOFLAGS);
           
  41. We send the second message
  42.            producer.send(worldMessage);
           
  43. We stop the work for xid2
  44.           xaRes.end(xid2, XAResource.TMSUCCESS);
           
  45. We prepare xid2
  46.           xaRes.prepare(xid2);
           
  47. Again, no messages should be received!
  48.           checkNoMessageReceived();
           
  49. We create JMX Connector to connect to the server's MBeanServer.
  50.           
              JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap());
              
           
  51. We retrieve the MBeanServerConnection
  52.           MBeanServerConnection mbsc = connector.getMBeanServerConnection();
           
  53. We list the prepared transactions
  54.           
              ObjectName serverObject = ObjectNameBuilder.DEFAULT.getMessagingServerObjectName();
              String[] infos = (String[])mbsc.invoke(serverObject, "listPreparedTransactions", null, null);
             
              System.out.println("Prepared transactions: ");
              for (String i : infos)
              {
                 System.out.println(i);
              }
              
           
  55. We roll back the first transaction
  56.           mbsc.invoke(serverObject, "rollbackPreparedTransaction", new String[] {XidImpl.toBase64String(xid1)}, new String[]{"java.lang.String"});
           
  57. We commit the second one
  58.           mbsc.invoke(serverObject, "commitPreparedTransaction", new String[] {XidImpl.toBase64String(xid2)}, new String[]{"java.lang.String"});
           
  59. We check the result, only the 'world' message received
  60.           checkMessageReceived("world");
           
  61. We check the prepared transaction again, should have none.
  62.           
              infos = (String[])mbsc.invoke(serverObject, "listPreparedTransactions", null, null);
              System.out.println("No. of prepared transactions now: " + infos.length);
              
           
  63. We close the JMX connector.
  64.           connector.close();
           
  65. 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
  66.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }