KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JmsRedeliveredTest


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;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.DeliveryMode JavaDoc;
22 import javax.jms.Destination JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageConsumer JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.TextMessage JavaDoc;
30 import javax.jms.Topic JavaDoc;
31
32 import org.apache.activemq.ActiveMQConnectionFactory;
33
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38 /**
39  * @version $Revision: 1.5 $
40  */

41 public class JmsRedeliveredTest extends TestCase {
42
43     private Connection JavaDoc connection;
44     
45     /* (non-Javadoc)
46      * @see junit.framework.TestCase#setUp()
47      */

48     protected void setUp() throws Exception JavaDoc {
49         connection = createConnection();
50     }
51
52     /**
53      * @see junit.framework.TestCase#tearDown()
54      */

55     protected void tearDown() throws Exception JavaDoc {
56         if (connection != null) {
57             connection.close();
58             connection = null;
59         }
60     }
61     
62     /**
63      * Creates a connection.
64      *
65      * @return connection
66      * @throws Exception
67      */

68     protected Connection JavaDoc createConnection() throws Exception JavaDoc{
69         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
70         return factory.createConnection();
71     }
72
73     /**
74      * Tests if a message unacknowledged message gets to be resent when the session is closed and
75      * then a new consumer session is created.
76      *
77      */

78     public void testQueueSessionCloseMarksMessageRedelivered() throws JMSException JavaDoc {
79         connection.start();
80
81         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
82         Queue JavaDoc queue = session.createQueue("queue-"+getName());
83         MessageProducer JavaDoc producer = createProducer(session, queue);
84         producer.send(createTextMessage(session));
85
86         // Consume the message...
87
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
88         Message msg = consumer.receive(1000);
89         assertNotNull(msg);
90         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
91         // Don't ack the message.
92

93         // Reset the session. This should cause the Unacked message to be redelivered.
94
session.close();
95         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
96                 
97         // Attempt to Consume the message...
98
consumer = session.createConsumer(queue);
99         msg = consumer.receive(2000);
100         assertNotNull(msg);
101         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
102         msg.acknowledge();
103         
104         session.close();
105     }
106
107     /**
108      * Tests session recovery and that the redelivered message is marked as such.
109      * Session uses client acknowledgement, the destination is a queue.
110      *
111      * @throws JMSException
112      */

113     public void testQueueRecoverMarksMessageRedelivered() throws JMSException JavaDoc {
114         connection.start();
115
116         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
117         Queue JavaDoc queue = session.createQueue("queue-"+getName());
118         MessageProducer JavaDoc producer = createProducer(session, queue);
119         producer.send(createTextMessage(session));
120
121         // Consume the message...
122
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
123         Message msg = consumer.receive(1000);
124         assertNotNull(msg);
125         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
126         // Don't ack the message.
127

128         // Reset the session. This should cause the Unacked message to be redelivered.
129
session.recover();
130                 
131         // Attempt to Consume the message...
132
msg = consumer.receive(2000);
133         assertNotNull(msg);
134         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
135         msg.acknowledge();
136         
137         session.close();
138     }
139     
140     /**
141      * Tests rollback message to be marked as redelivered.
142      * Session uses client acknowledgement and the destination is a queue.
143      *
144      * @throws JMSException
145      */

146     public void testQueueRollbackMarksMessageRedelivered() throws JMSException JavaDoc {
147         connection.start();
148
149         Session JavaDoc session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
150         Queue JavaDoc queue = session.createQueue("queue-"+getName());
151         MessageProducer JavaDoc producer = createProducer(session, queue);
152         producer.send(createTextMessage(session));
153         session.commit();
154         
155         // Get the message... Should not be redelivered.
156
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
157         Message msg = consumer.receive(1000);
158         assertNotNull(msg);
159         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
160         
161         // Rollback.. should cause redelivery.
162
session.rollback();
163                         
164         // Attempt to Consume the message...
165
msg = consumer.receive(2000);
166         assertNotNull(msg);
167         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
168         
169         session.commit();
170         session.close();
171     }
172     
173     /**
174      * Tests if the message gets to be re-delivered when the session closes and
175      * that the re-delivered message is marked as such.
176      * Session uses client acknowledgment, the destination is a topic and
177      * the consumer is a durable subscriber.
178      *
179      * @throws JMSException
180      */

181     public void testDurableTopicSessionCloseMarksMessageRedelivered() throws JMSException JavaDoc {
182         connection.setClientID(getName());
183         connection.start();
184
185         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
186         Topic JavaDoc topic = session.createTopic("topic-"+getName());
187         MessageConsumer JavaDoc consumer = session.createDurableSubscriber(topic, "sub1");
188
189         // This case only works with persistent messages since transient messages
190
// are dropped when the consumer goes offline.
191
MessageProducer JavaDoc producer = session.createProducer(topic);
192         producer.setDeliveryMode(DeliveryMode.PERSISTENT);
193         producer.send(createTextMessage(session));
194
195         // Consume the message...
196
Message msg = consumer.receive(1000);
197         assertNotNull(msg);
198         assertFalse("Message should not be re-delivered.", msg.getJMSRedelivered());
199         // Don't ack the message.
200

201         // Reset the session. This should cause the Unacked message to be re-delivered.
202
session.close();
203         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
204                 
205         // Attempt to Consume the message...
206
consumer = session.createDurableSubscriber(topic, "sub1");
207         msg = consumer.receive(2000);
208         assertNotNull(msg);
209         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
210         msg.acknowledge();
211         
212         session.close();
213     }
214     
215     /**
216      * Tests session recovery and that the redelivered message is marked as such.
217      * Session uses client acknowledgement, the destination is a topic and
218      * the consumer is a durable suscriber.
219      *
220      * @throws JMSException
221      */

222     public void testDurableTopicRecoverMarksMessageRedelivered() throws JMSException JavaDoc {
223         connection.setClientID(getName());
224         connection.start();
225
226         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
227         Topic JavaDoc topic = session.createTopic("topic-"+getName());
228         MessageConsumer JavaDoc consumer = session.createDurableSubscriber(topic, "sub1");
229
230         MessageProducer JavaDoc producer = createProducer(session, topic);
231         producer.send(createTextMessage(session));
232
233         // Consume the message...
234
Message msg = consumer.receive(1000);
235         assertNotNull(msg);
236         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
237         // Don't ack the message.
238

239         // Reset the session. This should cause the Unacked message to be redelivered.
240
session.recover();
241                 
242         // Attempt to Consume the message...
243
msg = consumer.receive(2000);
244         assertNotNull(msg);
245         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
246         msg.acknowledge();
247         
248         session.close();
249     }
250     
251     /**
252      * Tests rollback message to be marked as redelivered.
253      * Session uses client acknowledgement and the destination is a topic.
254      *
255      * @throws JMSException
256      */

257     public void testDurableTopicRollbackMarksMessageRedelivered() throws JMSException JavaDoc {
258         connection.setClientID(getName());
259         connection.start();
260
261         Session JavaDoc session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
262         Topic JavaDoc topic = session.createTopic("topic-"+getName());
263         MessageConsumer JavaDoc consumer = session.createDurableSubscriber(topic, "sub1");
264
265         MessageProducer JavaDoc producer = createProducer(session, topic);
266         producer.send(createTextMessage(session));
267         session.commit();
268         
269         // Get the message... Should not be redelivered.
270
Message msg = consumer.receive(1000);
271         assertNotNull(msg);
272         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
273         
274         // Rollback.. should cause redelivery.
275
session.rollback();
276                         
277         // Attempt to Consume the message...
278
msg = consumer.receive(2000);
279         assertNotNull(msg);
280         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
281         
282         
283         session.commit();
284         session.close();
285     }
286      
287     /**
288      *
289      *
290      * @throws JMSException
291      */

292     public void testTopicRecoverMarksMessageRedelivered() throws JMSException JavaDoc {
293
294         connection.setClientID(getName());
295         connection.start();
296
297         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
298         Topic JavaDoc topic = session.createTopic("topic-"+getName());
299         MessageConsumer JavaDoc consumer = session.createConsumer(topic);
300
301         MessageProducer JavaDoc producer = createProducer(session, topic);
302         producer.send(createTextMessage(session));
303
304         // Consume the message...
305
Message msg = consumer.receive(1000);
306         assertNotNull(msg);
307         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
308         // Don't ack the message.
309

310         // Reset the session. This should cause the Unacked message to be redelivered.
311
session.recover();
312                 
313         // Attempt to Consume the message...
314
msg = consumer.receive(2000);
315         assertNotNull(msg);
316         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
317         msg.acknowledge();
318         
319         session.close();
320     }
321     
322     /**
323      * Tests rollback message to be marked as redelivered.
324      * Session uses client acknowledgement and the destination is a topic.
325      *
326      * @throws JMSException
327      */

328     public void testTopicRollbackMarksMessageRedelivered() throws JMSException JavaDoc {
329         connection.setClientID(getName());
330         connection.start();
331
332         Session JavaDoc session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
333         Topic JavaDoc topic = session.createTopic("topic-"+getName());
334         MessageConsumer JavaDoc consumer = session.createConsumer(topic);
335
336         MessageProducer JavaDoc producer = createProducer(session, topic);
337         producer.send(createTextMessage(session));
338         session.commit();
339         
340         // Get the message... Should not be redelivered.
341
Message msg = consumer.receive(1000);
342         assertNotNull(msg);
343         assertFalse("Message should not be redelivered.", msg.getJMSRedelivered());
344         
345         // Rollback.. should cause redelivery.
346
session.rollback();
347                         
348         // Attempt to Consume the message...
349
msg = consumer.receive(2000);
350         assertNotNull(msg);
351         assertTrue("Message should be redelivered.", msg.getJMSRedelivered());
352         
353         session.commit();
354         session.close();
355     }
356
357     /**
358      * Creates a text message.
359      *
360      * @param session
361      * @return TextMessage.
362      * @throws JMSException
363      */

364     private TextMessage JavaDoc createTextMessage(Session JavaDoc session) throws JMSException JavaDoc {
365         return session.createTextMessage("Hello");
366     }
367    
368     /**
369      * Creates a producer.
370      *
371      * @param session
372      * @param queue - destination.
373      * @return MessageProducer
374      * @throws JMSException
375      */

376     private MessageProducer JavaDoc createProducer(Session JavaDoc session, Destination JavaDoc queue) throws JMSException JavaDoc {
377          MessageProducer JavaDoc producer = session.createProducer(queue);
378          producer.setDeliveryMode(getDeliveryMode());
379          return producer;
380     }
381     
382     /**
383      * Returns delivery mode.
384      *
385      * @return int - persistent delivery mode.
386      */

387     protected int getDeliveryMode() {
388         return DeliveryMode.PERSISTENT;
389     }
390
391     /**
392      * Run the JmsRedeliverTest with the delivery mode set as persistent.
393      */

394     static final public class PersistentCase extends JmsRedeliveredTest {
395
396         /**
397          * Returns delivery mode.
398          *
399          * @return int - persistent delivery mode.
400          */

401         protected int getDeliveryMode() {
402             return DeliveryMode.PERSISTENT;
403         }
404     }
405
406     /**
407      * Run the JmsRedeliverTest with the delivery mode set as non-persistent.
408      */

409     static final public class TransientCase extends JmsRedeliveredTest {
410
411         /**
412          * Returns delivery mode.
413          *
414          * @return int - non-persistent delivery mode.
415          */

416         protected int getDeliveryMode() {
417             return DeliveryMode.NON_PERSISTENT;
418         }
419     }
420     
421     public static Test suite() {
422         TestSuite suite= new TestSuite();
423         suite.addTestSuite(PersistentCase.class);
424         suite.addTestSuite(TransientCase.class);
425         return suite;
426     }
427 }
428
Popular Tags