This document provides code examples demonstrating how to use the OpenJMS administration API.

Preparatory work

In 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 = "tcp://localhost:3035/";
    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.

If security is enabled, a valid user name and password must be supplied, e.g.:

    String url = "tcp://localhost:3035/";
    String user = "admin";
    String password = "openjms";
    JmsAdminServerIfc admin = AdminConnectionFactory.create(url, user, password);

Note: the admin reference should be closed when it is no longer needed, e.g.:


Listing destinations

The 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 destinations

To 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 queue

To 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 subscriber

To 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 destination

To remove the administered destination 'myqueue':

    String destination = "myqueue";
    if (!admin.removeDestination(destination)) {
       System.err.println("Failed to remove destination " + queue);

Determining if a destination exists

To 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");

Shutting down OpenJMS

To shut down the OpenJMS server: