Posted By:
Andreas_Mueller
Posted On:
Friday, May 28, 2004 03:17 AM
Here's an example how to use JMS XA (sends 5 messages to a queue and then receives them):
XAQueueConnectionFactory qcf = null;
XAQueueConnection qc = null;
XAQueueSession qs = null;
QueueSender sender = null;
QueueReceiver receiver = null;
Queue queue = null;
XAResource xares = null;
qcf = (XAQueueConnectionFactory) ctx.lookup(name);
qc = qcf.createXAQueueConnection();
qc.start();
queue = (Queue)ctx.lookup(queueName);
qs = qc.createXAQueueSession();
xares = qs.getXAResource();
sender = qs.getQueueSession().createSender(queue);
receiver = qs.getQueueSession().createReceiver(queue);
Xid xid = new XidImpl();
xares.start(xid, XAResource.TMNOFLAGS);
TextMessage msg = qs.createTextMessage();
for (int i = 0; i < 5; i++)
{
msg.setText("Msg: " + i);
sender.send(msg, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
}
xares.end(xid, XAResource.TMSUCCESS);
xares.prepare(xid);
xares.commit(xid, false);
xid = new XidImpl();
xares.start(xid, XAResource.TMNOFLAGS);
for (int i = 0; i < 5; i++)
{
msg = (TextMessage) receiver.receive(2000);
assertTrue("Received msg==null", msg != null);
}
xares.end(xid, XAResource.TMSUCCESS);
xares.prepare(xid);
xares.commit(xid, false);
Note that the Xid is what you have to provide (in the above example it's our test XidImpl). It must be a separate branch for JMS.