OverviewThis document provides code examples demonstrating how to use the OpenJMS administration API. Preparatory workIn order to use the administration API, a JmsAdminServerIfc instance is required. This is obtained via the AdminConnectionFactory class: import org.exolab.jms.administration.AdminConnectionFactory; import org.exolab.jms.administration.JmsAdminServerIfc; // ... String url = "rmi://localhost:1099/"; JmsAdminServerIfc admin = AdminConnectionFactory.create(url); In the above, url specifies the URL of the OpenJMS server, and is connector specific. Refer to the connector documentation for more details. Note: the admin reference should be closed when it is no longer needed, e.g.: admin.close(); Listing destinationsThe following example shows how to list all administered destinations: Vector destinations = admin.getAllDestinations(); Iterator iterator = destinations.iterator(); while (iterator.hasNext()) { Destination destination = (Destination) iterator.next(); if (destination instanceof Queue) { Queue queue = (Queue) destination; System.out.println("queue:" + queue.getQueueName()); } else { Topic topic = (Topic) destination; System.out.println("topic:" + topic.getTopicName()); } } Creating administered destinationsTo create an administered queue named 'myqueue' : String queue = "myqueue"; Boolean isQueue = Boolean.TRUE; if (!admin.addDestination(queue, isQueue)) { System.err.println("Failed to create queue " + queue); } To create an administered topic named 'mytopic' : String topic = "mytopic"; Boolean isQueue = Boolean.FALSE; if (!admin.addDestination(topic, isQueue)) { System.err.println("Failed to create topic " + topic); } Counting messages in a queueTo determine the number of messages available in an administered queue named 'myqueue' : String queue = "myqueue"; int count = admin.getQueueMessageCount(queue); System.out.println("Queue " + queue + " has " + count + " messages"); Counting messages for a durable subscriberTo determine count the number of messages available in an administered topic named 'mytopic' for the subscriber 'sub1' : String topic = "mytopic"; String name = "sub1"; int count = admin.getDurableConsumerMessageCount(topic, name); System.out.println("Subscriber " + name + " has " + count + " messages " + "for topic " + topic); Removing a destinationTo remove the administered destination 'myqueue' : String destination = "myqueue"; if (!admin.removeDestination(destination)) { System.err.println("Failed to remove destination " + queue); } Determining if a destination existsTo determine if the 'mytopic' destination exists: String destination = "mytopic"; if (admin.destinationExists(destination)) { System.out.println(destination + " exists"); } else { System.out.println(destination + " doesn't exist"); } |