getLog().debug("Retrieving Topic");
Topic topic = (Topic) context.lookup(TEST_DURABLE_TOPIC);
getLog().debug("Creating a send session");
TopicSession sendSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher sender = sendSession.createPublisher(topic);
getLog().debug("Clearing the topic");
TopicSession subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subSession.createDurableSubscriber(topic, "test");
Message message = subscriber.receive(50);
while (message != null)
message = subscriber.receive(50);
subSession.close();
getLog().debug("Subscribing to topic, looking for Value = 'A'");
subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'A'", false);
getLog().debug("Send some messages");
message = sendSession.createTextMessage("Message1");
message.setStringProperty("Value", "A");
sender.publish(message);
message = sendSession.createTextMessage("Message2");
message.setStringProperty("Value", "A");
sender.publish(message);
message = sendSession.createTextMessage("Message3");
message.setStringProperty("Value", "B");
sender.publish(message);
getLog().debug("Retrieving the A messages");
message = subscriber.receive(2000);
assertTrue("Expected message 1", message != null);
assertTrue("Should get an A", message.getStringProperty("Value").equals("A"));
message = subscriber.receive(2000);
assertTrue("Expected message 2", message != null);
assertTrue("Should get a second A", message.getStringProperty("Value").equals("A"));
assertTrue("That should be it for A", subscriber.receive(2000) == null);
getLog().debug("Closing the subscriber without acknowledgement");
subSession.close();
getLog().debug("Subscribing to topic, looking for Value = 'B'");
subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'B'", false);
getLog().debug("Retrieving the non-existent B messages");
assertTrue("B should not be there", subscriber.receive(2000) == null);
getLog().debug("Closing the subscriber.");
subSession.close();
getLog().debug("Subscribing to topic, looking for those Value = 'A'");
subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'A'", false);
assertTrue("Should not be any A the subscription was changed", subscriber.receive(2000) == null);
subSession.close();
getLog().debug("Subscribing to topic, looking for everything");
subSession = topicConnection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
subscriber = subSession.createDurableSubscriber(topic, "test", null, false);
message = sendSession.createTextMessage("Message4");
message.setStringProperty("Value", "A");
sender.publish(message);
message = subscriber.receive(2000);
assertTrue("Expected message 4", message != null);
assertTrue("Should be an A which we don't acknowledge", message.getStringProperty("Value").equals("A"));
subSession.close();