TopicSubscriber sub1 = session.createDurableSubscriber(topic, "sub1");
TopicSubscriber sub2 = session.createSubscriber(topic);
TopicSubscriber sub3 = session.createSubscriber(topic);
//Now a sender
TopicPublisher sender = session.createPublisher(topic);
//send some messages
sender.publish(session.createTextMessage("Message 1"));
sender.publish(session.createTextMessage("Message 2"));
sender.publish(session.createTextMessage("Message 3"));
drainMessagesForTopic(sub1);
drainMessagesForTopic(sub2);
drainMessagesForTopic(sub3);
//close some subscribers
sub1.close();
sub2.close();
//send some more messages
sender.publish(session.createTextMessage("Message 4"));
sender.publish(session.createTextMessage("Message 5"));
sender.publish(session.createTextMessage("Message 6"));
//give time for message 4 to be negatively acked (as it will be cause last receive timed out)
try
{
Thread.sleep(5 * 1000);
}
catch (InterruptedException e)
{
}
drainMessagesForTopic(sub3);
//open subscribers again.
sub1 = session.createDurableSubscriber(topic, "sub1");
sub2 = session.createSubscriber(topic);
//Send a final message
sender.publish(session.createTextMessage("Final message"));
sender.close();
drainMessagesForTopic(sub1);
drainMessagesForTopic(sub2);
drainMessagesForTopic(sub3);