Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
AMQTopic topic = new AMQTopic((AMQConnection) conn, "testResubscribeWithChangedSelectorAndRestart");
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
TextMessage msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart1");
msg.setBooleanProperty("Match", true);
producer.send(msg);
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart2");
msg.setBooleanProperty("Match", false);
producer.send(msg);
Message rMsg = subA.receive(1000);
assertNotNull(rMsg);
assertEquals("Content was wrong",
"testResubscribeWithChangedSelectorAndRestart1",
((TextMessage) rMsg).getText());
// Queue has no messages left
AMQQueue subQueueTmp = new AMQQueue("amq.topic", "clientid" + ":" + "testResubscribeWithChangedSelectorAndRestart");
assertEquals("Msg count should be 0", 0, ((AMQSession<?, ?>) session).getQueueDepth(subQueueTmp));
rMsg = subA.receive(1000);
assertNull(rMsg);
// Send another 1 matching message and 1 non-matching message
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart1");
msg.setBooleanProperty("Match", true);
producer.send(msg);
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart2");
msg.setBooleanProperty("Match", false);
producer.send(msg);
// Disconnect subscriber without receiving the message to
//leave it on the underlying queue
subA.close();
// Reconnect with new selector that matches B
TopicSubscriber subB = session.createDurableSubscriber(topic,
"testResubscribeWithChangedSelectorAndRestart",
"Match = false", false);
//verify no messages are now present on the queue as changing selector should have issued
//an unsubscribe and thus deleted the previous durable backing queue for the subscription.
//check the dur sub's underlying queue now has msg count 0
AMQQueue subQueue = new AMQQueue("amq.topic", "clientid" + ":" + "testResubscribeWithChangedSelectorAndRestart");
assertEquals("Msg count should be 0", 0, ((AMQSession<?, ?>) session).getQueueDepth(subQueue));
// Check that new messages are received properly
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart1");
msg.setBooleanProperty("Match", true);
producer.send(msg);
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart2");
msg.setBooleanProperty("Match", false);
producer.send(msg);
rMsg = subB.receive(1000);
assertNotNull(rMsg);
assertEquals("Content was wrong",
"testResubscribeWithChangedSelectorAndRestart2",
((TextMessage) rMsg).getText());
rMsg = subB.receive(1000);
assertNull(rMsg);
//check the dur sub's underlying queue now has msg count 0
subQueue = new AMQQueue("amq.topic", "clientid" + ":" + "testResubscribeWithChangedSelectorAndRestart");
assertEquals("Msg count should be 0", 0, ((AMQSession<?, ?>) session).getQueueDepth(subQueue));
//now restart the server
try
{
restartBroker();
}
catch (Exception e)
{
_logger.error("problems restarting broker: " + e);
throw e;
}
// Reconnect to broker
Connection connection = getConnectionFactory().createConnection("guest", "guest");
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
topic = new AMQTopic((AMQConnection) connection, "testResubscribeWithChangedSelectorAndRestart");
producer = session.createProducer(topic);
//verify no messages now present on the queue after we restart the broker
//check the dur sub's underlying queue now has msg count 0
subQueue = new AMQQueue("amq.topic", "clientid" + ":" + "testResubscribeWithChangedSelectorAndRestart");
assertEquals("Msg count should be 0", 0, ((AMQSession<?, ?>) session).getQueueDepth(subQueue));
// Reconnect with new selector that matches B
TopicSubscriber subC = session.createDurableSubscriber(topic,
"testResubscribeWithChangedSelectorAndRestart",
"Match = False", false);
// Check that new messages are still sent and recieved properly
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart1");
msg.setBooleanProperty("Match", true);
producer.send(msg);
msg = session.createTextMessage("testResubscribeWithChangedSelectorAndRestart2");
msg.setBooleanProperty("Match", false);
producer.send(msg);
//check the dur sub's underlying queue now has msg count 1
subQueue = new AMQQueue("amq.topic", "clientid" + ":" + "testResubscribeWithChangedSelectorAndRestart");
assertEquals("Msg count should be 1", 1, ((AMQSession<?, ?>) session).getQueueDepth(subQueue));
rMsg = subC.receive(1000);
assertNotNull(rMsg);
assertEquals("Content was wrong",
"testResubscribeWithChangedSelectorAndRestart2",
((TextMessage) rMsg).getText());
rMsg = subC.receive(1000);
assertNull(rMsg);
session.unsubscribe("testResubscribeWithChangedSelectorAndRestart");
subC.close();
session.close();
connection.close();
}