Sun Java System Message Queue 4.1 Developer's Guide for JMX Clients

Using MBeans

Once you have obtained an MBean server connection, you can use it to communicate with Message Queue (and other) MBeans and to access their attributes, operations, and notifications. The following sections describe how this is done.

Accessing MBean Attributes

The MBean server connection's getAttribute method accepts the object name of an MBean along with a string representing the name of one of its attributes, and returns the value of the designated attribute. Example 2–4 shows an example, obtaining and printing the value of a destination's MaxNumProducers attribute from its configuration MBean (described in Destination Configuration).


Example 2–4 Getting an Attribute Value

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  GetAttrValue
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  destConfigName
                    = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue");
            
            //  Get and print attribute value
                Integer  attrValue
                    = (Integer)mbsc.getAttribute(destConfigName, DestinationAttributes.MAX_NUM_PRODUCERS);
                System.out.println( "Maximum number of producers: " + attrValue );
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

There is also an MBeanServerConnection method named getAttributes, which accepts an MBean object name and an array of attribute name strings, and returns a result of class AttributeList. This is an array of Attribute objects, each of which provides methods (getName and getValue) for retrieving the name and value of one of the requested attributes. Example 2–5 shows a modified version of Example 2–4 that uses getAttributes to retrieve the values of a destination's MaxNumProducers and maxNumActiveConsumers attributes from its configuration MBean (see Destination Configuration).


Example 2–5 Getting Multiple Attribute Values

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  GetAttrValues
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  destConfigName
                    = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue");
            
            //  Create array of attribute names
                String  attrNames[] = 
                            { DestinationAttributes.MAX_NUM_PRODUCERS,
                              DestinationAttributes.MAX_NUM_ACTIVE_CONSUMERS
                            };
            
            //  Get attributes
                AttributeList  attrList = mbsc.getAttributes(destConfigName, attrNames);
            
            //  Extract and print attribute values
                
                Object  attrValue;
                
                attrValue = attrList.get(0).getValue();
                System.out.println( "Maximum number of producers: " + attrValue.toString() );
                
                attrValue = attrList.get(1).getValue();
                System.out.println( "Maximum number of active consumers: " + attrValue.toString() );
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

To set the value of an attribute, use the MBeanServerConnection method setAttribute. This takes an MBean object name and an Attribute object specifying the name and value of the attribute to be set. Example 2–6 uses this method to set a destination's MaxNumProducers attribute to 25.


Example 2–6 Setting an Attribute Value

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  SetAttrValue
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  destConfigName
                    = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue");
            
            //  Create attribute object
                Attribute  attr = new Attribute(DestinationAttributes.MAX_NUM_PRODUCERS, 25);
            
            //  Set attribute value
                mbsc.setAttribute(destConfigName, attr);
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

Just as for getting attribute values, there is an MBeanServerConnection method named setAttributes for setting the values of multiple attributes at once. You supply an MBean object name and an attribute list giving the names and values of the attributes to be set. Example 2–7 illustrates the use of this method to set a destination's MaxNumProducers and MaxNumActiveConsumers attributes to 25 and 50, respectively.


Example 2–7 Setting Multiple Attribute Values

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  SetAttrValues
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  destConfigName
                    = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue");
            
            //  Create and populate attribute list
                
                AttributeList  attrList = new AttributeList();
                Attribute      attr;
                
                attr = new Attribute(DestinationAttributes.MAX_NUM_PRODUCERS, 25);
                attrList.add(attr);
                
                attr = new Attribute(DestinationAttributes.MAX_NUM_ACTIVE_CONSUMERS, 50);
                attrList.add(attr);
            
            //  Set attribute values
                mbsc.setAttributes(destConfigName, attrList);
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

Invoking MBean Operations

To invoke an MBean operation, use the MBeanServerConnection method invoke. The first two parameters to this method are an MBean object name and a string specifying the name of the operation to be invoked. (The two remaining parameters are used for supplying parameters to the invoked operation, and are discussed in the next example.) The method returns an object that is the operation's return value (if any). Example 2–8 shows the use of this method to pause the jms connection service by invoking the pause operation of its service configuration MBean (see Service Configuration).


Example 2–8 Invoking an Operation

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  InvokeOp
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  serviceConfigName = MQObjectName.createServiceConfig("jms");
            
            //  Invoke operation
                mbsc.invoke(serviceConfigName, ServiceOperations.PAUSE, null, null);
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

When the operation being invoked requires parameters, you supply them in an array as the third parameter to the MBeanServerConnection.invoke method. The method's fourth parameter is a signature array giving the class or interface names of the invoked operation's parameters. Example 2–9 shows an illustration, invoking the destination manager configuration MBean's create operation to create a new queue destination named MyQueue with the same attributes that were set in Example 2–7. The create operation (see Destination Manager Configuration) takes three parameters: the type (QUEUE or TOPIC) and name of the new destination and an attribute list specifying any initial attribute values to be set. The example shows how to set up a parameter array (opParams) containing these values, along with a signature array (opSig) giving their classes, and pass them to the invoke method.


Example 2–9 Invoking an Operation with Parameters

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  InvokeOpWithParams
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  destMgrConfigName 
                    = new ObjectName(MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
            
            //  Create and populate attribute list
                
                AttributeList  attrList = new AttributeList();
                Attribute      attr;
                
                attr = new Attribute(DestinationAttributes.MAX_NUM_PRODUCERS, 25);
                attrList.add(attr);
                
                attr = new Attribute(DestinationAttributes.MAX_NUM_ACTIVE_CONSUMERS, 50);
                attrList.add(attr);
            
            //  Create operation's parameter and signature arrays
                
                Object  opParams[] = { DestinationType.QUEUE,
                                       "MyQueue",
                                       attrList
                                     };
                
                String  opSig[] = { String.class.getName(),
                                    String.class.getName(),
                                    attrList.getClass().getName()
                                  };
            
            //  Invoke operation
                mbsc.invoke(destMgrConfigName, DestinationOperations.CREATE, opParams, opSig);
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

Example 2–10 shows a more elaborate example combining the use of MBean operations and attributes. The destination manager monitor MBean operation getDestinations (see Destination Manager Monitor) returns an array of object names of the destination monitor MBeans for all current destinations. The example then iterates through the array, printing the name, destination type (QUEUE or TOPIC), and current state (such as RUNNING or PAUSED) for each destination.


Example 2–10 Combining Operations and Attributes

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  OpsAndAttrs
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name for destination manager monitor MBean
                ObjectName  destMgrMonitorName
                    = new ObjectName(MQObjectName.DESTINATION_MANAGER_MONITOR_MBEAN_NAME);
            
            //  Get destination object names
                ObjectName  destNames[] = mbsc.invoke(destMgrMonitorName,
                                                      DestinationOperations.GET_DESTINATIONS,
                                                      null,
                                                      null);
            
            //  Step through array of object names, printing information for each destination
                
                System.out.println( "Listing destinations: " );
                
                ObjectName  eachDestName;
                Object      attrValue;
                
                for ( int i = 0; i < destNames.length; ++i )
                  { eachDestName = destNames[i];
                    
                    attrValue = mbsc.getAttribute(eachDestName, DestinationAttributes.NAME);
                    System.out.println( "\tName: " + attrValue );
                    
                    attrValue = mbsc.getAttribute(eachDestName, DestinationAttributes.TYPE);
                    System.out.println( "\tTypeYPE: " + attrValue );
                    
                    attrValue = mbsc.getAttribute(eachDestName, DestinationAttributes.STATE_LABEL);
                    System.out.println( "\tState: " + attrValue );
                    
                    System.out.println( "" );
                  }
            
            //  Close JMX connector
                jmxc.close();
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }

Some of the Message Queue MBeans’ operations and attributes return a composite data object (implementing the JMX CompositeData interface). This type of object consists of a collection of data values accessed by means of associative lookup keys. The specific keys vary from one MBean to another, and are described in the relevant sections of Chapter 3, Message Queue MBean Reference. Example 2–11 shows an illustration, invoking the consumer manager MBean's GetConsumerInfo operation (see Consumer Manager Monitor to obtain an array of composite data objects describing all current message consumers. It then steps through the array, using the lookup keys listed in Table 3–63 to retrieve and print the characteristics of each consumer.


Example 2–11 Using a Composite Data Object

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  CompData
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name
                ObjectName  consumerMgrMonitorName
                    = new ObjectName(MQObjectName.CONSUMER_MANAGER_MONITOR_MBEAN_NAME);
            
            //  Invoke operation
                Object  result
                    = mbsc.invoke(consumerMgrMonitorName, ConsumerOperations.GET_CONSUMER_INFO, null, null);
            
            //  Typecast result to an array of composite data objects
                CompositeData  cdArray[] = (CompositeData[])result;
            
            //  Step through array, printing information for each consumer
                
                if ( cdArray == null )
                  { System.out.println( "No message consumers found" );
                  }
                else
                  { for ( int  i = 0; i < cdArray.length; ++i )
                      { CompositeData  cd = cdArray[i];
                        
                        System.out.println( "Consumer ID: "
                                                 + cd.get(ConsumerInfo.CONSUMER_ID) );
                        System.out.println( "User: "
                                                 + cd.get(ConsumerInfo.USER) );
                        System.out.println( "Host: "
                                                 + cd.get(ConsumerInfo.HOST) );
                        System.out.println( "Connection service: "
                                                 + cd.get(ConsumerInfo.SERVICE_NAME) );
                        System.out.println( "Acknowledgment mode: "
                                                 + cd.get(ConsumerInfo.ACKNOWLEDGE_MODE_LABEL) );
                        System.out.println( "Destination name: "
                                                 + cd.get(ConsumerInfo.DESTINATION_NAME) );
                        System.out.println( "Destination type: "
                                                 + cd.get(ConsumerInfo.DESTINATION_TYPE) );
                      }
                  }
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
        
        finally
          { if ( jmxc != null )
              { try
                  { jmxc.close();
                  }
                catch (IOException ioe)
                  { System.out.println( "I/O exception occurred: " + ioe.toString() );
                    ioe.printStackTrace();
                  }
              }
          }
      }
  }

Receiving MBean Notifications

To receive notifications from an MBean, you must register a notification listener with the MBean server. This is an object implementing the JMX interface NotificationListener, which consists of the single method handleNotification. In registering the listener with the MBean server (using the MBeanServerConnection method addNotificationListener), you supply the object name of the MBean from which you wish to receive notifications, along with a notification filter specifying which types of notification you wish to receive. (You can also provide an optional handback object to be passed to your listener whenever it is invoked, and which you can use for any purpose convenient to your application.) The MBean server will then call your listener's handleNotification method whenever the designated MBean broadcasts a notification satisfying the filter you specified.

The notification listener's handleNotification method receives two parameters: a notification object (belonging to the JMX class Notification) describing the notification being raised, along with the handback object, if any, that you supplied when you registered the listener. The notification object provides methods for retrieving various pieces of information about the notification, such as its type, the MBean raising it, its time stamp, and an MBean-dependent user data object and message string further describing the notification. The notifications raised by Message Queue MBeans belong to Message Queue–specific subclasses of Notification, such as BrokerNotification, ServiceNotification, and DestinationNotification, which add further information retrieval methods specific to each particular type of notification; see the relevant sections of Chapter 3, Message Queue MBean Reference for details.

Example 2–12 shows a notification listener for responding to Message Queue service notifications, issued by a service manager monitor MBean. On receiving a notification belonging to the Message Queue class ServiceNotification, the listener simply prints an informational message containing the notification's type and the name of the connection service affected.


Example 2–12 Notification Listener

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.jms.management.server.*;


public class  ServiceNotificationListener implements NotificationListener
  { 
    public void  handleNotification (Notification  notification,
                                     Object        handback)
      { 
        if ( notification instanceOf ServiceNotification )
          { ServiceNotification  n = (ServiceNotification)notification;
          }
        else
          { System.err.println( "Wrong type of notification for listener" );
            return;
          }
        
        System.out.println( "\nReceived service notification: " );
        System.out.println( "\tNotification type: " + n.getType() );
        System.out.println( "\tService name: " + n.getServiceName() );
        
        System.out.println( "" );
      }
  }

Example 2–13 shows how to register the notification listener from Example 2–12, using the MBeanServerConnection method addNotificationListener. The notification filter is an object of the standard JMX class NotificationFilterSupport; the calls to this object's enableType method specify that the listener should be invoked whenever a connection service is paused or resumed. The listener itself is an instance of class ServiceNotificationListener, as defined in Example 2–12.


Example 2–13 Registering a Notification Listener

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;
import java.io.IOException


public class  NotificationService
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            
            //  Create object name for service manager monitor MBean
                ObjectName  svcMgrMonitorName
                    = new ObjectName( MQObjectName.SERVICE_MANAGER_MONITOR_MBEAN_NAME );
            
            //  Create notification filter
                NotificationFilterSupport  myFilter = new NotificationFilterSupport();
                myFilter.enableType(ServiceNotification.SERVICE_PAUSE);
                myFilter.enableType(ServiceNotification.SERVICE_RESUME);
            
            //  Create notification listener
                ServiceNotificationListener  myListener = new ServiceNotificationListener();
                mbsc.addNotificationListener(svcMgrMonitorName, myListener, myFilter, null);
                
                ...
          }
        
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
        
        finally
          { if ( jmxc != null )
              { try
                  { jmxc.close();
                  }
                catch (IOException ioe)
                  { System.out.println( "I/O exception occurred: " + ioe.toString() );
                    ioe.printStackTrace();
                  }
              }
          }
      }
  }