InitialContext ic = new InitialContext(ServerManagement.getJNDIEnvironment());
TopicConnectionFactory cf = (JBossConnectionFactory)ic.lookup("/ConnectionFactory");
ServerManagement.deployTopic("TopicMessageList");
TopicConnection conn = null;
try
{
Topic topic = (Topic)ic.lookup("/topic/TopicMessageList");
conn = cf.createTopicConnection();
conn.setClientID("Client1");
TopicSession s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = s.createProducer(topic);
prod.setDeliveryMode(DeliveryMode.PERSISTENT);
MessageConsumer cons = s.createDurableSubscriber(topic, "SubscriberA");
// Send some persistent message
TextMessage tm1 = s.createTextMessage("message1");
tm1.setStringProperty("vegetable", "parsnip");
TextMessage tm2 = s.createTextMessage("message2");
tm2.setStringProperty("vegetable", "parsnip");
TextMessage tm3 = s.createTextMessage("message3");
tm3.setStringProperty("vegetable", "parsnip");
prod.send(tm1);
prod.send(tm2);
prod.send(tm3);
// and some non persistent with a selector
prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage tm4 = s.createTextMessage("message4");
tm4.setStringProperty("vegetable", "artichoke");
TextMessage tm5 = s.createTextMessage("message5");
tm5.setStringProperty("vegetable", "artichoke");
TextMessage tm6 = s.createTextMessage("message6");
tm6.setStringProperty("vegetable", "artichoke");
prod.send(tm4);
prod.send(tm5);
prod.send(tm6);
// Start the connection for delivery
conn.start();
// There should be 1 subscription
ObjectName destObjectName =
new ObjectName("jboss.messaging.destination:service=Topic,name=TopicMessageList");
List durableSubs = (List)ServerManagement.invoke(destObjectName, "listDurableSubscriptions", null, null);
assertNotNull(durableSubs);
assertEquals(1, durableSubs.size());
//Note that listing messages DOES NOT list messages that are in the process of being delivered
//or scheduled
//so we need to close the consumers otherwise the messages will be buffered in them and not
//visible
cons.close();
//Give time for cancel to occur
Thread.sleep(500);
String sub1Id = ((SubscriptionInfo)durableSubs.get(0)).getId();
List allMsgs = (List)ServerManagement.invoke(destObjectName, "listAllMessages", new Object[] { sub1Id }, new String[] { "java.lang.String" });
assertNotNull(allMsgs);
assertEquals(6, allMsgs.size());
TextMessage rm1 = (TextMessage)allMsgs.get(0);
TextMessage rm2 = (TextMessage)allMsgs.get(1);
TextMessage rm3 = (TextMessage)allMsgs.get(2);
TextMessage rm4 = (TextMessage)allMsgs.get(3);
TextMessage rm5 = (TextMessage)allMsgs.get(4);
TextMessage rm6 = (TextMessage)allMsgs.get(5);
assertEquals(tm1.getText(), rm1.getText());
assertEquals(tm2.getText(), rm2.getText());
assertEquals(tm3.getText(), rm3.getText());
assertEquals(tm4.getText(), rm4.getText());
assertEquals(tm5.getText(), rm5.getText());
assertEquals(tm6.getText(), rm6.getText());
assertTrue(rm1.getJMSDeliveryMode() == DeliveryMode.PERSISTENT);
assertTrue(rm2.getJMSDeliveryMode() == DeliveryMode.PERSISTENT);
assertTrue(rm3.getJMSDeliveryMode() == DeliveryMode.PERSISTENT);
assertTrue(rm4.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm5.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm6.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
List durMsgs = (List)ServerManagement.invoke(destObjectName, "listDurableMessages", new Object[] { sub1Id }, new String[] { "java.lang.String" });
assertNotNull(durMsgs);
assertEquals(3, durMsgs.size());
rm1 = (TextMessage)durMsgs.get(0);
rm2 = (TextMessage)durMsgs.get(1);
rm3 = (TextMessage)durMsgs.get(2);
assertEquals(tm1.getText(), rm1.getText());
assertEquals(tm2.getText(), rm2.getText());
assertEquals(tm3.getText(), rm3.getText());
assertTrue(rm1.getJMSDeliveryMode() == DeliveryMode.PERSISTENT);
assertTrue(rm2.getJMSDeliveryMode() == DeliveryMode.PERSISTENT);
assertTrue(rm3.getJMSDeliveryMode() == DeliveryMode.PERSISTENT);
List nondurMsgs = (List)ServerManagement.invoke(destObjectName, "listNonDurableMessages", new Object[] { sub1Id }, new String[] { "java.lang.String" });
assertNotNull(nondurMsgs);
assertEquals(3, nondurMsgs.size());
rm4 = (TextMessage)nondurMsgs.get(0);
rm5 = (TextMessage)nondurMsgs.get(1);
rm6 = (TextMessage)nondurMsgs.get(2);
assertEquals(tm4.getText(), rm4.getText());
assertEquals(tm5.getText(), rm5.getText());
assertEquals(tm6.getText(), rm6.getText());
assertTrue(rm4.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm5.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm6.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
//Now with a selector
String sel = "vegetable='artichoke'";
allMsgs = (List)ServerManagement.invoke(destObjectName, "listAllMessages", new Object[] { sub1Id, sel }, new String[] { "java.lang.String", "java.lang.String" });
assertNotNull(allMsgs);
assertEquals(3, allMsgs.size());
rm4 = (TextMessage)allMsgs.get(0);
rm5 = (TextMessage)allMsgs.get(1);
rm6 = (TextMessage)allMsgs.get(2);
assertEquals(tm4.getText(), rm4.getText());
assertEquals(tm5.getText(), rm5.getText());
assertEquals(tm6.getText(), rm6.getText());
assertTrue(rm4.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm5.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm6.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
durMsgs = (List)ServerManagement.invoke(destObjectName, "listDurableMessages", new Object[] { sub1Id, sel }, new String[] { "java.lang.String" , "java.lang.String" });
assertNotNull(durMsgs);
assertEquals(0, durMsgs.size());
nondurMsgs = (List)ServerManagement.invoke(destObjectName, "listNonDurableMessages", new Object[] { sub1Id, sel }, new String[] { "java.lang.String", "java.lang.String" });
assertNotNull(nondurMsgs);
assertEquals(3, nondurMsgs.size());
rm4 = (TextMessage)nondurMsgs.get(0);
rm5 = (TextMessage)nondurMsgs.get(1);
rm6 = (TextMessage)nondurMsgs.get(2);
assertEquals(tm4.getText(), rm4.getText());
assertEquals(tm5.getText(), rm5.getText());
assertEquals(tm6.getText(), rm6.getText());
assertTrue(rm4.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm5.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
assertTrue(rm6.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT);
}
finally
{
if (conn != null)
{
conn.close();
}
ServerManagement.undeployTopic("TopicMessageList");
}