KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class DurableConsumerCloseAndReconnectTest extends TestSupport {
38     protected static final long RECEIVE_TIMEOUT = 5000L;
39
40     private Connection JavaDoc connection;
41     private Session JavaDoc session;
42     private MessageConsumer JavaDoc consumer;
43     private MessageProducer JavaDoc producer;
44     private Destination JavaDoc destination;
45     private int messageCount=0;
46     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc {
47         return new ActiveMQConnectionFactory("vm://localhost?broker.deleteAllMessagesOnStartup=false");
48     }
49
50     public void testCreateDurableConsumerCloseThenReconnect() throws Exception JavaDoc {
51         // force the server to stay up across both connection tests
52
Connection JavaDoc dummyConnection = createConnection();
53         dummyConnection.start();
54
55         consumeMessagesDeliveredWhileConsumerClosed();
56
57         dummyConnection.close();
58
59         // now lets try again without one connection open
60
consumeMessagesDeliveredWhileConsumerClosed();
61         //now delete the db
62
ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory("vm://localhost?broker.deleteAllMessagesOnStartup=true");
63         dummyConnection = fac.createConnection();
64         dummyConnection.start();
65         dummyConnection.close();
66     }
67
68     protected void consumeMessagesDeliveredWhileConsumerClosed() throws Exception JavaDoc {
69         makeConsumer();
70         closeConsumer();
71
72         publish();
73
74         // wait a few moments for the close to really occur
75
Thread.sleep(1000);
76
77         makeConsumer();
78
79         Message JavaDoc message = consumer.receive(RECEIVE_TIMEOUT);
80         assertTrue("Should have received a message!", message != null);
81
82         closeConsumer();
83
84         log.info("Now lets create the consumer again and because we didn't ack, we should get it again");
85         makeConsumer();
86
87         message = consumer.receive(RECEIVE_TIMEOUT);
88         assertTrue("Should have received a message!", message != null);
89         message.acknowledge();
90        
91         closeConsumer();
92
93         log.info("Now lets create the consumer again and because we didn't ack, we should get it again");
94         makeConsumer();
95
96         message = consumer.receive(2000);
97         assertTrue("Should have no more messages left!", message == null);
98
99         closeConsumer();
100
101         log.info("Lets publish one more message now");
102         publish();
103
104         makeConsumer();
105         message = consumer.receive(RECEIVE_TIMEOUT);
106         assertTrue("Should have received a message!", message != null);
107         message.acknowledge();
108        
109         closeConsumer();
110     }
111
112     protected void publish() throws Exception JavaDoc {
113         connection = createConnection();
114         connection.start();
115
116         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
117         destination = createDestination();
118
119         producer = session.createProducer(destination);
120         producer.setDeliveryMode(DeliveryMode.PERSISTENT);
121         TextMessage JavaDoc msg = session.createTextMessage("This is a test: " + messageCount++);
122         producer.send(msg);
123       
124         producer.close();
125         producer = null;
126         closeSession();
127     }
128
129     protected Destination JavaDoc createDestination() throws JMSException JavaDoc {
130         if (isTopic()) {
131             return session.createTopic(getSubject());
132         }
133         else {
134             return session.createQueue(getSubject());
135         }
136     }
137
138     protected boolean isTopic() {
139         return true;
140     }
141
142     protected void closeConsumer() throws JMSException JavaDoc {
143         consumer.close();
144         consumer = null;
145         closeSession();
146     }
147
148     protected void closeSession() throws JMSException JavaDoc {
149         session.close();
150         session = null;
151         connection.close();
152         connection = null;
153     }
154
155     protected void makeConsumer() throws Exception JavaDoc {
156         String JavaDoc durableName = getName();
157         String JavaDoc clientID = getSubject();
158         log.info("Creating a durable subscribe for clientID: " + clientID + " and durable name: " + durableName);
159         createSession(clientID);
160         consumer = createConsumer(durableName);
161     }
162
163     private MessageConsumer JavaDoc createConsumer(String JavaDoc durableName) throws JMSException JavaDoc {
164         if (destination instanceof Topic JavaDoc) {
165             return session.createDurableSubscriber((Topic JavaDoc) destination, durableName);
166         }
167         else {
168             return session.createConsumer(destination);
169         }
170     }
171
172     protected void createSession(String JavaDoc clientID) throws Exception JavaDoc {
173         connection = createConnection();
174         connection.setClientID(clientID);
175         connection.start();
176
177         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
178         destination = createDestination();
179     }
180 }
181
Popular Tags