KickJava   Java API By Example, From Geeks To Geeks.

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


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.Message JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TextMessage JavaDoc;
29 import javax.jms.Topic JavaDoc;
30 import javax.jms.TopicSubscriber JavaDoc;
31
32 /**
33  * @author Paul Smith
34  * @version $Revision: 1.1.1.1 $
35  */

36 public class SubscribeClosePublishThenConsumeTest extends TestSupport {
37
38     public void testDurableTopic() throws Exception JavaDoc {
39         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://locahost");
40
41         String JavaDoc topicName = "TestTopic";
42         String JavaDoc clientID = getName();
43         String JavaDoc subscriberName = "MySubscriber:"+System.currentTimeMillis();
44
45         Connection JavaDoc connection = connectionFactory.createConnection();
46         connection.setClientID(clientID);
47
48         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
49         Topic JavaDoc topic = session.createTopic(topicName);
50
51         // this should register a durable subscriber, we then close it to
52
// test that we get messages from the producer later on
53
TopicSubscriber JavaDoc subscriber = session.createDurableSubscriber(topic, subscriberName);
54         connection.start();
55
56         topic = null;
57         subscriber.close();
58         subscriber = null;
59         session.close();
60         session = null;
61
62         // Create the new connection before closing to avoid the broker shutting down.
63
// now create a new Connection, Session & Producer, send some messages & then close
64
Connection JavaDoc t = connectionFactory.createConnection();
65         connection.close();
66         connection = t;
67         
68         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
69         topic = session.createTopic(topicName);
70         MessageProducer JavaDoc producer = session.createProducer(topic);
71         producer.setDeliveryMode(DeliveryMode.PERSISTENT);
72         TextMessage JavaDoc textMessage = session.createTextMessage("Hello World");
73         producer.send(textMessage);
74         textMessage = null;
75
76         topic = null;
77         session.close();
78         session = null;
79
80         // Now (re)register the Durable subscriber, setup a listener and wait for messages that should
81
// have been published by the previous producer
82
t = connectionFactory.createConnection();
83         connection.close();
84         connection = t;
85         
86         connection.setClientID(clientID);
87         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
88         topic = session.createTopic(topicName);
89
90         subscriber = session.createDurableSubscriber(topic, subscriberName);
91         connection.start();
92
93         log.info("Started connection - now about to try receive the textMessage");
94
95         long time = System.currentTimeMillis();
96         Message JavaDoc message = subscriber.receive(15000L);
97         long elapsed = System.currentTimeMillis() - time;
98
99         log.info("Waited for: " + elapsed + " millis");
100
101         assertNotNull("Should have received the message we published by now", message);
102         assertTrue("should be text textMessage", message instanceof TextMessage JavaDoc);
103         textMessage = (TextMessage JavaDoc) message;
104         assertEquals("Hello World", textMessage.getText());
105     }
106 }
107
Popular Tags