//set up some subscribers to the topic
Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup(TEST_TOPIC);
TopicSubscriber sub1 = session.createDurableSubscriber(topic, "sub1");
MessageConsumer sub2 = session.createConsumer(topic);
MessageConsumer sub3 = session.createConsumer(topic);
//Now a sender
MessageProducer sender = session.createProducer(topic);
//send some messages
sender.send(session.createTextMessage("Message 1"));
sender.send(session.createTextMessage("Message 2"));
sender.send(session.createTextMessage("Message 3"));
drainMessagesForTopic(sub1);
drainMessagesForTopic(sub2);
drainMessagesForTopic(sub3);
//close some subscribers
sub1.close();
sub2.close();
//send some more messages
sender.send(session.createTextMessage("Message 4"));
sender.send(session.createTextMessage("Message 5"));
sender.send(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.createConsumer(topic);
//Send a final message
sender.send(session.createTextMessage("Final message"));
sender.close();
drainMessagesForTopic(sub1);
drainMessagesForTopic(sub2);
drainMessagesForTopic(sub3);
sub1.close();
sub2.close();
sub3.close();
session.unsubscribe("sub1");