KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > TransactionTest


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.usecases;
19
20 import java.util.Date JavaDoc;
21
22 import javax.jms.Connection JavaDoc;
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.MessageConsumer JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Session JavaDoc;
30 import javax.jms.TextMessage JavaDoc;
31
32 import junit.framework.TestCase;
33
34 import org.apache.activemq.ActiveMQConnectionFactory;
35 import org.apache.activemq.command.ActiveMQQueue;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import java.util.concurrent.CountDownLatch JavaDoc;
40
41 /**
42  * @author pragmasoft
43  * @version $Revision: 1.1.1.1 $
44  */

45 public final class TransactionTest extends TestCase {
46     
47     private static final Log log = LogFactory.getLog(TransactionTest.class);
48
49     private volatile String JavaDoc receivedText;
50
51     private Session JavaDoc producerSession;
52     private Session JavaDoc consumerSession;
53     private Destination JavaDoc queue;
54
55     private MessageProducer JavaDoc producer;
56     private MessageConsumer JavaDoc consumer;
57     private Connection JavaDoc connection;
58     private CountDownLatch JavaDoc latch = new CountDownLatch JavaDoc(1);
59
60     public void testTransaction() throws Exception JavaDoc {
61
62         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
63         connection = factory.createConnection();
64         queue = new ActiveMQQueue(getClass().getName() + "." + getName());
65
66         producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
67         consumerSession = connection.createSession(true, 0);
68
69         producer = producerSession.createProducer(queue);
70
71         consumer = consumerSession.createConsumer(queue);
72         consumer.setMessageListener(new MessageListener JavaDoc() {
73
74             public void onMessage(Message JavaDoc m) {
75                 try {
76                     TextMessage JavaDoc tm = (TextMessage JavaDoc) m;
77                     receivedText = tm.getText();
78                     latch.countDown();
79
80                     log.info("consumer received message :" + receivedText);
81                     consumerSession.commit();
82                     log.info("committed transaction");
83                 }
84                 catch (JMSException JavaDoc e) {
85                     try {
86                         consumerSession.rollback();
87                         log.info("rolled back transaction");
88                     }
89                     catch (JMSException JavaDoc e1) {
90                         log.info(e1);
91                         e1.printStackTrace();
92                     }
93                     log.info(e);
94                     e.printStackTrace();
95                 }
96             }
97         });
98
99         connection.start();
100
101         TextMessage JavaDoc tm = null;
102         try {
103             tm = producerSession.createTextMessage();
104             tm.setText("Hello, " + new Date JavaDoc());
105             producer.send(tm);
106             log.info("producer sent message :" + tm.getText());
107         }
108         catch (JMSException JavaDoc e) {
109             e.printStackTrace();
110         }
111
112         log.info("Waiting for latch");
113         latch.await();
114
115         log.info("test completed, destination=" + receivedText);
116     }
117
118     protected void tearDown() throws Exception JavaDoc {
119         if (connection != null) {
120             connection.close();
121         }
122         super.tearDown();
123     }
124 }
125
Popular Tags