
examples.jms.xa-receive.readme.html Maven / Gradle / Ivy
HornetQ JMS XA Receive Example
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.
HornetQ is JTA aware, meaning you can use HornetQ 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 ./build.sh
(or build.bat
on windows) from this directory
- 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);
- We look-up the JMS queue object from JNDI
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
- We perform a lookup on the XA Connection Factory
XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");
- We create a JMS XAConnection
connection = cf.createXAConnection();
- We Start the connection
connection.start();
- We create a JMS XASession
XASession xaSession = connection.createXASession();
- We create a normal session
Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- We create a normal Message Producer
MessageProducer normalProducer = normalSession.createProducer(queue);
- We get the JMS Session
Session session = xaSession.getSession();
- We create a message consumer
MessageConsumer xaConsumer = session.createConsumer(queue);
- We create two Text Messages
TextMessage helloMessage = session.createTextMessage("hello");
TextMessage worldMessage = session.createTextMessage("world");
- We create a transaction
Xid xid1 = new XidImpl("xa-example1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
- We get the JMS XAResource
XAResource xaRes = xaSession.getXAResource();
- We begin the Transaction work
xaRes.start(xid1, XAResource.TMNOFLAGS);
- We send two messages.
normalProducer.send(helloMessage);
normalProducer.send(worldMessage);
- 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());
- We stop the work
xaRes.end(xid1, XAResource.TMSUCCESS);
- We prepare
xaRes.prepare(xid1);
- We roll back the transaction
xaRes.rollback(xid1);
- We create another transaction
Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
- We start the transaction
xaRes.start(xid2, XAResource.TMNOFLAGS);
- 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());
- We stop the work
xaRes.end(xid2, XAResource.TMSUCCESS);
- We prepare
xaRes.prepare(xid2);
- We commit!
xaRes.commit(xid2, false);
- 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;
}
- 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();
}
}