Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
AMQTopic topic = new AMQTopic((AMQConnection) conn, "testResubscribeWithChangedSelector");
MessageProducer producer = session.createProducer(topic);
// Create durable subscriber that matches A
TopicSubscriber subA = session.createDurableSubscriber(topic,
"testResubscribeWithChangedSelector",
"Match = True", false);
// Send 1 matching message and 1 non-matching message
sendMatchingAndNonMatchingMessage(session, producer);
Message rMsg = subA.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertNotNull(rMsg);
assertEquals("Content was wrong",
"testResubscribeWithChangedSelector1",
((TextMessage) rMsg).getText());
rMsg = subA.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertNull(rMsg);
// Disconnect subscriber
subA.close();
// Reconnect with new selector that matches B
TopicSubscriber subB = session.createDurableSubscriber(topic,
"testResubscribeWithChangedSelector","Match = False", false);
//verify no messages are now recieved.
rMsg = subB.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertNull("Should not have received message as the selector was changed", rMsg);
// Check that new messages are received properly
sendMatchingAndNonMatchingMessage(session, producer);
rMsg = subB.receive(POSITIVE_RECEIVE_TIMEOUT);
assertNotNull("Message should have been received", rMsg);
assertEquals("Content was wrong",
"testResubscribeWithChangedSelector2",
((TextMessage) rMsg).getText());
rMsg = subB.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertNull("Message should not have been received",rMsg);
session.unsubscribe("testResubscribeWithChangedSelector");
}