KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > atomikos > XATest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
26 import javax.jms.XAConnection JavaDoc;
27 import javax.jms.XASession JavaDoc;
28 import javax.transaction.xa.XAResource JavaDoc;
29 import javax.transaction.xa.Xid JavaDoc;
30
31 import junit.framework.TestCase;
32
33
34 /**
35  * @version $Revision: 480097 $
36  */

37 public class XATest extends TestCase {
38
39     public void testXA() throws Exception JavaDoc {
40         BrokerService broker = new BrokerService();
41         broker.addConnector("tcp://localhost:61616");
42         broker.start();
43         
44     
45         String JavaDoc url = "tcp://localhost:61616";
46         String JavaDoc 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 JavaDoc xaconn = xacf.createXAConnection();
54         xaconn.start();
55         XASession JavaDoc session = xaconn.createXASession();
56         XAResource JavaDoc xares = session.getXAResource();
57         MessageConsumer JavaDoc 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 JavaDoc 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