* @version $Revision: 1.4 $
*/
public class SubscribeClosePublishThenConsumeTest extends TestSupport {
public void testDurableTopic() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setUseEmbeddedBroker(true);
String topicName = "TestTopic";
String clientID = "MyClientID";
String subscriberName = "MySubscriber";
// TODO remove this hack!
//Connection dummy = connectionFactory.createConnection();
//dummy.start();
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;
connection.close();
connection = null;
// now create a new Connection, Session & Producer, send some messages & then close
connection = connectionFactory.createConnection();
// connection.setClientID(clientID); // this should not be required for the Producer
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;
connection.close();
connection = null;
// Now (re)register the Durable subscriber, setup a listener and wait for messages that should
// have been published by the previous producer
connection = connectionFactory.createConnection();
connection.setClientID(clientID);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
topic = session.createTopic(topicName);
subscriber = session.createDurableSubscriber(topic, subscriberName);