This example shows you how to use JTA interfaces to control transactions with ActiveMQ. JTA provides - facilities to start and stop a transaction and enlist XA resources into a transaction.
- -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 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.
- -To run the example, simply type mvn verify
from this directory. It will download the JBoss JTA jars before
- it launches the example.
client-jndi.properties
file in the directory ../common/config
- InitialContext initialContext = getContext(0);
-
-
-
- Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
-
-
-
- XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");
-
-
-
- connection = cf.createXAConnection();
-
-
-
- connection.start();
-
-
-
- XASession xaSession = connection.createXASession();
-
-
-
- Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
-
-
-
- MessageProducer normalProducer = normalSession.createProducer(queue);
-
-
-
-
- Session session = xaSession.getSession();
-
-
-
- MessageConsumer xaConsumer = session.createConsumer(queue);
-
-
-
-
- TextMessage helloMessage = session.createTextMessage("hello");
- TextMessage worldMessage = session.createTextMessage("world");
-
-
-
-
- javax.transaction.TransactionManager txMgr = TransactionManager.transactionManager();
-
-
-
- txMgr.begin();
-
-
-
- XAResource xaRes = xaSession.getXAResource();
-
-
-
-
- Transaction transaction = txMgr.getTransaction();
- transaction.enlistResource(new DummyXAResource());
- transaction.enlistResource(xaRes);
-
-
-
-
-
- normalProducer.send(helloMessage);
- normalProducer.send(worldMessage);
-
-
-
-
-
- TextMessage rm1 = (TextMessage)xaConsumer.receive();
- System.out.println("Message received: " + rm1.getText());
- TextMessage rm2 = (TextMessage)xaConsumer.receive();
- System.out.println("Message received: " + rm2.getText());
-
-
-
-
- txMgr.rollback();
-
-
-
-
- txMgr.begin();
- transaction = txMgr.getTransaction();
-
-
-
-
-
- transaction.enlistResource(new DummyXAResource());
- transaction.enlistResource(xaRes);
-
-
-
-
-
- rm1 = (TextMessage)xaConsumer.receive();
- System.out.println("Message received again: " + rm1.getText());
- rm2 = (TextMessage)xaConsumer.receive();
- System.out.println("Message received again: " + rm2.getText());
-
-
-
-
- txMgr.commit();
-
-
-
-
- TextMessage rm3 = (TextMessage)xaConsumer.receive(2000);
- if (rm3 == null)
- {
- System.out.println("No message received after commit.");
- }
- else
- {
- result = false;
- }
-
-
-
- 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();
- }
- }
-
-