// Create a durable subscriber on one connection and close it
conn1 = cf.createConnection();
conn1.setClientID(CLIENT_ID1);
sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer durable = sess1.createDurableSubscriber(topic, "mySubscription");
conn1.close();
// Send some messages on another connection and close it
conn2 = cf.createConnection();
conn2.setClientID(CLIENT_ID1);
Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod2 = sess2.createProducer(topic);
prod2.setDeliveryMode(DeliveryMode.PERSISTENT);
final int NUM_MESSAGES = 10;
for (int i = 0; i < NUM_MESSAGES; i++)
{
TextMessage tm = sess2.createTextMessage("hello");
prod2.send(topic, tm);
}
conn2.close();
// Receive the messages on another connection
conn3 = cf.createConnection();
conn3.setClientID(CLIENT_ID1);
conn3.start();
sess3 = conn3.createSession(false, Session.AUTO_ACKNOWLEDGE);
durable = sess3.createDurableSubscriber(topic, "mySubscription");
int count = 0;
while (true)
{
TextMessage tm = (TextMessage)durable.receive(1000);
if (tm == null)
{
break;
}
assertEquals("hello", tm.getText());
count++;
}
assertEquals(NUM_MESSAGES, count);
conn3.close();
// Try and receive them again
conn4 = cf.createConnection();
conn4.setClientID(CLIENT_ID1);
conn4.start();
sess4 = conn4.createSession(false, Session.AUTO_ACKNOWLEDGE);
durable = sess4.createDurableSubscriber(topic, "mySubscription");
TextMessage tm = (TextMessage)durable.receive(1000);
assertNull(tm);
conn4.close();
// Send some more messages and unsubscribe
conn5 = cf.createConnection();
conn5.setClientID(CLIENT_ID1);
conn5.start();
Session sess5 = conn5.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod5 = sess5.createProducer(topic);
prod5.setDeliveryMode(DeliveryMode.PERSISTENT);
log.debug("sending.1 " + NUM_MESSAGES + " messages");
for (int i = 0; i < NUM_MESSAGES; i++)
{
TextMessage tm2 = sess5.createTextMessage("hello");
prod5.send(topic, tm2);
}
log.debug("unsubscribing mySubscription");
durable.close();
sess5.unsubscribe("mySubscription");
log.debug("unsubscribing done");
conn5.close();
// Resubscribe with the same name
conn6 = cf.createConnection();
conn6.setClientID(CLIENT_ID1);
conn6.start();
sess6 = conn6.createSession(false, Session.AUTO_ACKNOWLEDGE);
durable = sess6.createDurableSubscriber(topic, "mySubscription");
TextMessage tm3 = (TextMessage)durable.receive(1000);
assertNull(tm3);
durable.close();
}
finally
{
if (conn1 != null)
{