* @version $Revision: 1.1.1.1 $
*/
public class SubscribeClosePublishThenConsumeTest extends TestSupport {
public void testDurableTopic() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://locahost");
connectionFactory.setUseEmbeddedBroker(true);
String topicName = "TestTopic";
String clientID = getName();
String subscriberName = "MySubscriber:"+System.currentTimeMillis();
Connection connection = connectionFactory.createConnection();
connection.setClientID(clientID);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topicName);
// this should register a durable subscriber, we then close it to
// test that we get messages from the producer later on
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriberName);
connection.start();
topic = null;
subscriber.close();
subscriber = null;
session.close();
session = null;
// Create the new connection before closing to avoid the broker shutting down.
// now create a new Connection, Session & Producer, send some messages & then close
Connection t = connectionFactory.createConnection();
connection.close();
connection = t;
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
topic = session.createTopic(topicName);
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage textMessage = session.createTextMessage("Hello World");
producer.send(textMessage);
textMessage = null;
topic = null;
session.close();
session = null;
// Now (re)register the Durable subscriber, setup a listener and wait for messages that should
// have been published by the previous producer
t = connectionFactory.createConnection();
connection.close();
connection = t;
connection.setClientID(clientID);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);