Connection connection = getConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Topic topic = session.createTopic(MY_TOPIC_SUBSCRIPTION_NAME);
TopicSubscriber noLocalSubscriber = session.
createDurableSubscriber(topic, MY_TOPIC_SUBSCRIPTION_NAME + "-NoLocal",
null, true);
TopicSubscriber normalSubscriber = session.
createDurableSubscriber(topic, MY_TOPIC_SUBSCRIPTION_NAME + "-Normal",
null, false);
sendMessage(session, topic, SEND_COUNT);
// Check messages can be received as expected.
connection.start();
//As the no-local subscriber was on the same connection the messages were
//published on, tit will receive no messages as they will be discarded on the broker
List<Message> received = receiveMessage(noLocalSubscriber, SEND_COUNT);
assertEquals("No Local Subscriber Received messages", 0, received.size());
received = receiveMessage(normalSubscriber, SEND_COUNT);
assertEquals("Normal Subscriber Received no messages",
SEND_COUNT, received.size());
session.commit();
normalSubscriber.close();
connection.close();
//Ensure the no-local subscribers messages were discarded by restarting the broker
//and reconnecting to the subscription to ensure they were not recovered.
restartBroker();
Connection connection2 = getConnection();
connection2.start();
Session session2 = connection2.createSession(true, Session.SESSION_TRANSACTED);
Topic topic2 = session2.createTopic(MY_TOPIC_SUBSCRIPTION_NAME);
TopicSubscriber noLocalSubscriber2 = session2.
createDurableSubscriber(topic2, MY_TOPIC_SUBSCRIPTION_NAME + "-NoLocal",
null, true);
// The NO-local subscriber should not get any messages
received = receiveMessage(noLocalSubscriber2, SEND_COUNT);
session2.commit();
assertEquals("No Local Subscriber Received messages", 0, received.size());
noLocalSubscriber2.close();
}