1 18 package org.apache.activemq.atomikos; 19 20 import com.atomikos.datasource.xa.DefaultXidFactory; 21 import org.apache.activemq.broker.BrokerService; 22 import org.apache.activemq.ActiveMQXAConnectionFactory; 23 import org.apache.activemq.command.ActiveMQQueue; 24 25 import javax.jms.MessageConsumer ; 26 import javax.jms.XAConnection ; 27 import javax.jms.XASession ; 28 import javax.transaction.xa.XAResource ; 29 import javax.transaction.xa.Xid ; 30 31 import junit.framework.TestCase; 32 33 34 37 public class XATest extends TestCase { 38 39 public void testXA() throws Exception { 40 BrokerService broker = new BrokerService(); 41 broker.addConnector("tcp://localhost:61616"); 42 broker.start(); 43 44 45 String url = "tcp://localhost:61616"; 46 String qName = "MyQueue"; 47 int timeout = 5; 48 DefaultXidFactory xidFactory = new DefaultXidFactory(); 49 ActiveMQXAConnectionFactory xacf = new ActiveMQXAConnectionFactory(); 50 xacf.setBrokerURL(url); 51 ActiveMQQueue queue = new ActiveMQQueue(); 52 queue.setPhysicalName(qName); 53 XAConnection xaconn = xacf.createXAConnection(); 54 xaconn.start(); 55 XASession session = xaconn.createXASession(); 56 XAResource xares = session.getXAResource(); 57 MessageConsumer receiver = session.getSession().createConsumer(queue); 58 xares.recover(XAResource.TMSTARTRSCAN); 59 xares.recover(XAResource.TMNOFLAGS); 60 xares.setTransactionTimeout(timeout); 61 xares.isSameRM(xares); 62 Xid xid = xidFactory.createXid("part1", "part2"); 63 xares.start(xid, XAResource.TMNOFLAGS); 64 receiver.receive(timeout); 65 xares.end(xid, XAResource.TMSUCCESS); 66 xares.rollback(xid); 67 68 System.out.println("Done!"); 69 } 70 71 } 72 | Popular Tags |