InitialContext context = getInitialContext();
XATopicConnectionFactory factory = (XATopicConnectionFactory) context.lookup(XA_TOPIC_FACTORY);
Topic topic = (Topic) context.lookup(TEST_TOPIC);
XATopicConnection connection = factory.createXATopicConnection();
try
{
// Set up
XATopicSession xaSession = connection.createXATopicSession();
// Add the xa resource to xid1
MyXid xid1 = new MyXid();
XAResource resource = xaSession.getXAResource();
resource.start(xid1, XAResource.TMNOFLAGS);
TopicSession session = xaSession.getTopicSession();
TopicSubscriber subscriber = session.createSubscriber(topic);
connection.start();
TopicPublisher publisher = session.createPublisher(topic);
Message message = session.createTextMessage();
// Publish a message using "AutoAcknowledge"
publisher.publish(message);
resource.end(xid1, XAResource.TMSUCCESS);
resource.prepare(xid1);
// JBossMessaging only sends the message when a commit is done, while JBossMQ would send messages to consumers on the same session,
// doing something differently on the transaction isolation.
// Because of that this commit is necessary to complete this testcase.
resource.commit(xid1, false);
xid1 = new MyXid();
resource.start(xid1, XAResource.TMNOFLAGS);
// Receive the message
message = subscriber.receive(1000);
if (message == null)
fail("No message?");
// Prepare the transaction
resource.end(xid1, XAResource.TMSUCCESS);
resource.prepare(xid1);
// Rollback
resource.rollback(xid1);
xid1 = new MyXid();
resource.start(xid1, XAResource.TMNOFLAGS);
// Receive the message using "AutoAcknowledge"
message = subscriber.receiveNoWait();
if (message == null)
fail("No message after rollback?");
resource.end(xid1, XAResource.TMSUCCESS);
resource.commit(xid1, true);
}
finally
{
connection.close();
}
}