}
public void testMessageCount() throws Exception
{
InitialContext ic = new InitialContext(ServerManagement.getJNDIEnvironment());
TopicConnectionFactory cf = (JBossConnectionFactory)ic.lookup("/ConnectionFactory");
ServerManagement.deployTopic("TopicGetAllMessageCount");
ServerManagement.invoke(ServerManagement.getServerPeerObjectName(), "enableMessageCounters", null, null);
TopicConnection conn = null;
try
{
Topic topic = (Topic)ic.lookup("/topic/TopicGetAllMessageCount");
conn = cf.createTopicConnection();
conn.setClientID("Client1");
TopicSession s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = s.createProducer(topic);
prod.setDeliveryMode(DeliveryMode.PERSISTENT);
// Create 1 durable subscription and 2 non-durable subscription
s.createDurableSubscriber(topic, "SubscriberA");
s.createSubscriber(topic);
s.createSubscriber(topic);
//Send a couple of messages
TextMessage tm1 = s.createTextMessage("message1");
TextMessage tm2 = s.createTextMessage("message2");
prod.send(tm1);
prod.send(tm2);
// There should be 3 subscriptions
ObjectName destObjectName =
new ObjectName("jboss.messaging.destination:service=Topic,name=TopicGetAllMessageCount");
Integer count = (Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount");
assertEquals(6, count.intValue());
count = (Integer)ServerManagement.getAttribute(destObjectName, "DurableMessageCount");
assertEquals(2, count.intValue());
count = (Integer)ServerManagement.getAttribute(destObjectName, "NonDurableMessageCount");
assertEquals(4, count.intValue());
// Now disconnect
conn.close();
// Only the durable should survive
count = (Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount");
assertEquals(2, count.intValue());
count = (Integer)ServerManagement.getAttribute(destObjectName, "DurableMessageCount");
assertEquals(2, count.intValue());
count = (Integer)ServerManagement.getAttribute(destObjectName, "NonDurableMessageCount");
assertEquals(0, count.intValue());
// Now connect again and restore the durable subscription
conn = cf.createTopicConnection();
conn.setClientID("Client1");
s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = s.createDurableSubscriber(topic, "SubscriberA");
count = (Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount");