{
getLog().debug("Starting TopicNoLocal test");
connect();
TopicConnectionFactory topicFactory = (TopicConnectionFactory) context.lookup(TOPIC_FACTORY);
TopicConnection topicConnection1 = topicFactory.createTopicConnection();
topicConnection1.start();
TopicConnection topicConnection2 = topicFactory.createTopicConnection();
topicConnection2.start();
// We don't want local messages on this topic.
TopicSession session1 = topicConnection1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup(TEST_TOPIC);
TopicSubscriber subscriber1 = session1.createSubscriber(topic, null, true);
TopicPublisher sender1 = session1.createPublisher(topic);
//Now a sender
TopicSession session2 = topicConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher sender2 = session2.createPublisher(topic);
drainMessagesForTopic(subscriber1);
//send some messages
sender1.publish(session1.createTextMessage("Local Message"));
sender2.publish(session2.createTextMessage("Remote Message"));
// Get the messages, we should get the remote message
// but not the local message
TextMessage msg1 = (TextMessage) subscriber1.receive(2000);
if (msg1 == null)
{
fail("Did not get any messages");
}
else
{
getLog().debug("Got message: " + msg1);
if (msg1.getText().equals("Local Message"))
{
fail("Got a local message");
}
TextMessage msg2 = (TextMessage) subscriber1.receive(2000);
if (msg2 != null)
{
getLog().debug("Got message: " + msg2);
fail("Got an extra message. msg1:" + msg1 + ", msg2:" + msg2);
}
}
topicConnection1.stop();
topicConnection1.close();
topicConnection2.stop();
topicConnection2.close();
disconnect();
getLog().debug("TopicNoLocal test passed");
}