KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.Connection JavaDoc;
21 import javax.jms.DeliveryMode JavaDoc;
22 import javax.jms.Destination JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28 import javax.jms.Topic JavaDoc;
29
30 import org.apache.activemq.test.TestSupport;
31 import org.apache.activemq.util.IdGenerator;
32
33 /**
34  * @version $Revision: 1.1.1.1 $
35  */

36 public class TopicRedeliverTest extends TestSupport {
37     
38     private static final int RECEIVE_TIMEOUT = 10000;
39     private IdGenerator idGen = new IdGenerator();
40     protected int deliveryMode = DeliveryMode.PERSISTENT;
41     public TopicRedeliverTest(){
42     }
43     
44     public TopicRedeliverTest(String JavaDoc n){
45         super(n);
46     }
47     
48     protected void setup() throws Exception JavaDoc{
49         super.setUp();
50         topic = true;
51     }
52     
53     
54     /**
55      * test messages are acknowledged and recovered properly
56      * @throws Exception
57      */

58     public void testClientAcknowledge() throws Exception JavaDoc {
59         Destination JavaDoc destination = createDestination(getClass().getName());
60         Connection JavaDoc connection = createConnection();
61         connection.setClientID(idGen.generateId());
62         connection.start();
63         Session JavaDoc consumerSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
64         MessageConsumer JavaDoc consumer = consumerSession.createConsumer(destination);
65         Session JavaDoc producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
66         MessageProducer JavaDoc producer = producerSession.createProducer(destination);
67         producer.setDeliveryMode(deliveryMode);
68         
69         //send some messages
70

71         TextMessage JavaDoc sent1 = producerSession.createTextMessage();
72         sent1.setText("msg1");
73         producer.send(sent1);
74         
75         TextMessage JavaDoc sent2 = producerSession.createTextMessage();
76         sent1.setText("msg2");
77         producer.send(sent2);
78         
79         TextMessage JavaDoc sent3 = producerSession.createTextMessage();
80         sent1.setText("msg3");
81         producer.send(sent3);
82         
83         Message JavaDoc rec1 = consumer.receive(RECEIVE_TIMEOUT);
84         Message JavaDoc rec2 = consumer.receive(RECEIVE_TIMEOUT);
85         Message JavaDoc rec3 = consumer.receive(RECEIVE_TIMEOUT);
86         
87         //ack rec2
88
rec2.acknowledge();
89         
90         TextMessage JavaDoc sent4 = producerSession.createTextMessage();
91         sent4.setText("msg4");
92         producer.send(sent4);
93         
94         Message JavaDoc rec4 = consumer.receive(RECEIVE_TIMEOUT);
95         assertTrue(rec4.equals(sent4));
96         consumerSession.recover();
97         rec4 = consumer.receive(RECEIVE_TIMEOUT);
98         assertTrue(rec4.equals(sent4));
99         assertTrue(rec4.getJMSRedelivered());
100         rec4.acknowledge();
101         connection.close();
102         
103     }
104     
105     /**
106      * Test redelivered flag is set on rollbacked transactions
107      * @throws Exception
108      */

109     public void testRedilveredFlagSetOnRollback() throws Exception JavaDoc {
110         Destination JavaDoc destination = createDestination(getClass().getName());
111         Connection JavaDoc connection = createConnection();
112         connection.setClientID(idGen.generateId());
113         connection.start();
114         Session JavaDoc consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
115         MessageConsumer JavaDoc consumer = null;
116         if (topic){
117             consumer = consumerSession.createDurableSubscriber((Topic JavaDoc)destination, "TESTRED");
118         }else{
119         consumer = consumerSession.createConsumer(destination);
120         }
121         Session JavaDoc producerSession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
122         MessageProducer JavaDoc producer = producerSession.createProducer(destination);
123         producer.setDeliveryMode(deliveryMode);
124         
125         TextMessage JavaDoc sentMsg = producerSession.createTextMessage();
126         sentMsg.setText("msg1");
127         producer.send(sentMsg);
128         producerSession.commit();
129         
130         Message JavaDoc recMsg = consumer.receive(RECEIVE_TIMEOUT);
131         assertTrue(recMsg.getJMSRedelivered() == false);
132         recMsg = consumer.receive(RECEIVE_TIMEOUT);
133         consumerSession.rollback();
134         recMsg = consumer.receive(RECEIVE_TIMEOUT);
135         assertTrue(recMsg.getJMSRedelivered());
136         consumerSession.commit();
137         assertTrue(recMsg.equals(sentMsg));
138         assertTrue(recMsg.getJMSRedelivered());
139         connection.close();
140     }
141
142
143     /**
144      * Check a session is rollbacked on a Session close();
145      * @throws Exception
146      */

147
148     public void XtestTransactionRollbackOnSessionClose() throws Exception JavaDoc {
149         Destination JavaDoc destination = createDestination(getClass().getName());
150         Connection JavaDoc connection = createConnection();
151         connection.setClientID(idGen.generateId());
152         connection.start();
153         Session JavaDoc consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
154         MessageConsumer JavaDoc consumer = null;
155         if (topic){
156             consumer = consumerSession.createDurableSubscriber((Topic JavaDoc)destination, "TESTRED");
157         }else{
158         consumer = consumerSession.createConsumer(destination);
159         }
160         Session JavaDoc producerSession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
161         MessageProducer JavaDoc producer = producerSession.createProducer(destination);
162         producer.setDeliveryMode(deliveryMode);
163         
164         TextMessage JavaDoc sentMsg = producerSession.createTextMessage();
165         sentMsg.setText("msg1");
166         producer.send(sentMsg);
167       
168         producerSession.commit();
169         
170         Message JavaDoc recMsg = consumer.receive(RECEIVE_TIMEOUT);
171         assertTrue(recMsg.getJMSRedelivered() == false);
172         consumerSession.close();
173         consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
174         consumer = consumerSession.createConsumer(destination);
175         
176         recMsg = consumer.receive(RECEIVE_TIMEOUT);
177         consumerSession.commit();
178         assertTrue(recMsg.equals(sentMsg));
179         connection.close();
180     }
181
182     /**
183      * check messages are actuallly sent on a tx rollback
184      * @throws Exception
185      */

186
187     public void testTransactionRollbackOnSend() throws Exception JavaDoc {
188         Destination JavaDoc destination = createDestination(getClass().getName());
189         Connection JavaDoc connection = createConnection();
190         connection.setClientID(idGen.generateId());
191         connection.start();
192         Session JavaDoc consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
193         MessageConsumer JavaDoc consumer = consumerSession.createConsumer(destination);
194         Session JavaDoc producerSession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
195         MessageProducer JavaDoc producer = producerSession.createProducer(destination);
196         producer.setDeliveryMode(deliveryMode);
197         
198         TextMessage JavaDoc sentMsg = producerSession.createTextMessage();
199         sentMsg.setText("msg1");
200         producer.send(sentMsg);
201         producerSession.commit();
202         
203         Message JavaDoc recMsg = consumer.receive(RECEIVE_TIMEOUT);
204         consumerSession.commit();
205         assertTrue(recMsg.equals(sentMsg));
206         
207         sentMsg = producerSession.createTextMessage();
208         sentMsg.setText("msg2");
209         producer.send(sentMsg);
210         producerSession.rollback();
211         
212         sentMsg = producerSession.createTextMessage();
213         sentMsg.setText("msg3");
214         producer.send(sentMsg);
215         producerSession.commit();
216         
217         recMsg = consumer.receive(RECEIVE_TIMEOUT);
218         assertTrue(recMsg.equals(sentMsg));
219         consumerSession.commit();
220         
221         connection.close();
222     }
223    
224 }
225
Popular Tags