TopicConnection 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 1 non-durable subscription
s.createDurableSubscriber(topic, "Durable1");
s.createSubscriber(topic);
// Send 1 message
prod.send(s.createTextMessage("First one"));
ObjectName destObjectName =
new ObjectName("jboss.messaging.destination:service=Topic,name=TopicRemoveAllMessages");
int count = ((Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount")).intValue();
assertEquals(2, count);
// Start the connection for delivery
conn.start();
// Remove all messages from the topic
//Need to pause since delivery may still be in progress
Thread.sleep(2000);
count = ((Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount")).intValue();
//Note we only keep track of deliveries for DURABLE subs so count should be 1
//no durable are effectively acked immediately
assertEquals(1, count);
//This should fail since you cannot remove messages if there are deliveries in progress
try
{
ServerManagement.invoke(destObjectName, "removeAllMessages", null, null);
fail();
}
catch (Exception e)
{
//OK
}
count = ((Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount")).intValue();
assertEquals(1, count);
// Now close the connection
conn.close();
ServerManagement.invoke(destObjectName, "removeAllMessages", null, null);
count = ((Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount")).intValue();
assertEquals(0, count);
// Connect again to the same topic
conn = cf.createTopicConnection();
conn.setClientID("Client1");
s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
prod = s.createProducer(topic);
prod.setDeliveryMode(DeliveryMode.PERSISTENT);
// Send another message
prod.send(s.createTextMessage("Second one"));
count = ((Integer)ServerManagement.getAttribute(destObjectName, "AllMessageCount")).intValue();
assertEquals(1, count);