// Create consumer 2.
AMQConnection con2 = (AMQConnection) getConnection("guest", "guest");
con2.start();
Session session2 = con2.createSession(false, ackMode);
TopicSubscriber consumer2 = session2.createDurableSubscriber(topic, "MySubscription");
// Send message and check that both consumers get it and only it.
producer.send(session0.createTextMessage("A"));
msg = consumer1.receive(POSITIVE_RECEIVE_TIMEOUT);
assertNotNull("Message should be available", msg);
assertEquals("Message Text doesn't match", "A", ((TextMessage) msg).getText());
msg = consumer1.receive(500);
assertNull("There should be no more messages for consumption on consumer1.", msg);
msg = consumer2.receive(POSITIVE_RECEIVE_TIMEOUT);
assertNotNull("Message should have been received",msg);
assertEquals("Consumer 2 should also received the first msg.", "A", ((TextMessage) msg).getText());
msg = consumer2.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertNull("There should be no more messages for consumption on consumer2.", msg);
// Send message and receive on consumer 1.
producer.send(session0.createTextMessage("B"));
_logger.info("Receive message on consumer 1 :expecting B");
msg = consumer1.receive(POSITIVE_RECEIVE_TIMEOUT);
assertEquals("B", ((TextMessage) msg).getText());
_logger.info("Receive message on consumer 1 :expecting null");
msg = consumer1.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertEquals(null, msg);
// Detach the durable subscriber.
consumer2.close();
session2.close();
con2.close();
// Send message C and receive on consumer 1
producer.send(session0.createTextMessage("C"));
_logger.info("Receive message on consumer 1 :expecting C");
msg = consumer1.receive(POSITIVE_RECEIVE_TIMEOUT);
assertEquals("C", ((TextMessage) msg).getText());
_logger.info("Receive message on consumer 1 :expecting null");
msg = consumer1.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertEquals(null, msg);
// Re-attach a new consumer to the durable subscription, and check that it gets message B it left (if not NO_ACK)
// and also gets message C sent after it was disconnected.
AMQConnection con3 = (AMQConnection) getConnection("guest", "guest");
con3.start();
Session session3 = con3.createSession(false, ackMode);
TopicSubscriber consumer3 = session3.createDurableSubscriber(topic, "MySubscription");
if(ackMode == AMQSession.NO_ACKNOWLEDGE)
{
//Do nothing if NO_ACK was used, as prefetch means the message was dropped
//when we didn't call receive() to get it before closing consumer 2
}
else
{
_logger.info("Receive message on consumer 3 :expecting B");
msg = consumer3.receive(POSITIVE_RECEIVE_TIMEOUT);
assertNotNull(msg);
assertEquals("B", ((TextMessage) msg).getText());
}
_logger.info("Receive message on consumer 3 :expecting C");
msg = consumer3.receive(POSITIVE_RECEIVE_TIMEOUT);
assertNotNull("Consumer 3 should get message 'C'.", msg);
assertEquals("Incorrect Message recevied on consumer3.", "C", ((TextMessage) msg).getText());
_logger.info("Receive message on consumer 3 :expecting null");
msg = consumer3.receive(NEGATIVE_RECEIVE_TIMEOUT);
assertNull("There should be no more messages for consumption on consumer3.", msg);
consumer1.close();
consumer3.close();
session3.unsubscribe("MySubscription");
con0.close();
con1.close();