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

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();
                  }
              }
          }
      }
  }