[Java programming language only]

Accessing Managed Beans (MBeans) programmatically

You can connect to MBeans with Java applications. These applications use the interfaces in the com.ibm.websphere.objectgrid.management package.

About this task

Programmatic methods for accessing MBeans vary depending on the type of server to which you are connecting.

Procedure

  • Connect to a stand-alone catalog service MBean server:

    The following example program connects to a stand-alone catalog service MBean server and returns an XML formatted string that lists each container server along with its allocated shards for a given ObjectGrid and MapSet.

    Figure 1. CollectPlacementPlan.java
    package com.ibm.websphere.sample.xs.admin;
    
    import java.util.Set;
    
    import javax.management.MBeanServerConnection;
    import javax.management.ObjectName;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    
    /**
     * Collects the placement information from the Catalog Server for a given ObjectGrid.
     */
    public final class CollectPlacementPlan {
        private static String hostName = "localhost";
    
        private static int port = 1099;
    
        private static String objectGridName = "library";
    
        private static String mapSetName = "ms1";
    
        /**
         * Connects to the ObjectGrid Catalog Service to retrieve placement information and 
    	   * prints it out.
         *
         * @param args
         * @throws Exception
         *             If there is a problem connecting to the catalog service MBean server.
         */
        public static void main(String[] args) throws Exception {
            String serviceURL = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port +
                        "/objectgrid/MBeanServer";
            JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
            JMXConnector jmxCon = JMXConnectorFactory.connect(jmxUrl);
    
            try {
                MBeanServerConnection catalogServerConnection = jmxCon.getMBeanServerConnection();
    
                Set placementSet = catalogServerConnection.queryNames(new 
    						ObjectName("com.ibm.websphere.objectgrid"
                 + ":*,type=PlacementService"), null);
                ObjectName placementService = (ObjectName) placementSet.iterator().next();
                Object placementXML = catalogServerConnection.invoke(placementService,
                    "listObjectGridPlacement", new Object[] {
                    objectGridName, mapSetName }, new String[] { String.class.getName(), 
    							String.class.getName() });
                System.out.println(placementXML);
            } catch (Exception e) {
                if(jmxCon != null) {
                    jmxCon.close();
                }
            }
        }
    
    }
    A few notes regarding the sample program:
    • The JMXServiceURL value for the catalog service is always of the following form: service:jmx:rmi:///jndi/rmi://<host>:<port>/objectgrid/MBeanServer, where <host> is the host on which the catalog service is running and <port> is the JMX service port that is provided with the -JMXServicePort option when starting the catalog service. If no port is specified, the default is 1099.
    • For the ObjectGrid or map statistics to be enabled, you must specify the following property in the server properties file when you are starting an ObjectGrid container: statsSpec=all=enabled
    • To disable the MBeans that are running in the container servers, specify the following property in the server properties file: enableMBeans=false.
    An example of the output follows. This output indicates that two container servers are active. The Container-0 container server hosts four primary shards. The Container-1 container server hosts a synchronous replica for each of the primary shards on the Container-0 container server. In this configuration, two synchronous replicas and one asynchronous replica are configured. As a result, the Unassigned container server is left with the remaining shards. If two more container servers are started, the Unassigned container server is not displayed.
    <objectGrid name="library" mapSetName="ms1">
      <container name="Container-1" zoneName="DefaultZone"
        hostName="myhost.mycompany.com" serverName="ogserver">
        <shard type="SynchronousReplica" partitionName="0"/>
        <shard type="SynchronousReplica" partitionName="1"/>
        <shard type="SynchronousReplica" partitionName="2"/>
        <shard type="SynchronousReplica" partitionName="3"/>
      </container>
      <container name="Container-0" zoneName="DefaultZone"
        hostName="myhost.mycompany.com" serverName="ogserver">
        <shard type="Primary" partitionName="0"/>
        <shard type="Primary" partitionName="1"/>
        <shard type="Primary" partitionName="2"/>
        <shard type="Primary" partitionName="3"/>
      </container>
      <container name="library:ms1:_UnassignedContainer_" zoneName="_ibm_SYSTEM"
        hostName="UNASSIGNED" serverName="UNNAMED">
        <shard type="SynchronousReplica" partitionName="0"/>
        <shard type="SynchronousReplica" partitionName="1"/>
        <shard type="SynchronousReplica" partitionName="2"/>
        <shard type="SynchronousReplica" partitionName="3"/>
        <shard type="AsynchronousReplica" partitionName="0"/>
        <shard type="AsynchronousReplica" partitionName="1"/>
        <shard type="AsynchronousReplica" partitionName="2"/>
        <shard type="AsynchronousReplica" partitionName="3"/>
      </container>
    </objectGrid>
    
  • Connect to a container MBean server:

    Container servers host MBeans to query information about the individual maps and ObjectGrid instances that are running within the container server. The following example program prints the status of each container server that is hosted by the catalog server with the JMX address of localhost:1099:

    Figure 2. CollectContainerStatus.java
    package com.ibm.websphere.sample.xs.admin;
    
    import java.util.List;
    import java.util.Set;
    
    import javax.management.MBeanServerConnection;
    import javax.management.ObjectInstance;
    import javax.management.ObjectName;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    
    /**
     * Collects placement status from each of the available containers directly.
     */
    public final class CollectContainerStatus {
        private static String hostName = "localhost";
    
        private static int port = 1099;
    
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
            String serviceURL = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/objectgrid/MBeanServer";
            JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
            JMXConnector jmxCon = JMXConnectorFactory.connect(jmxUrl);
    
            try {
                MBeanServerConnection catalogServerConnection = jmxCon.getMBeanServerConnection();
    
                Set placementSet = catalogServerConnection.queryNames(new ObjectName("com.ibm.websphere.objectgrid"
                        + ":*,type=PlacementService"), null);
    
                ObjectName placementService = (ObjectName) placementSet.iterator().next();
                List<String> containerJMXAddresses = (List<String>) catalogServerConnection.invoke(placementService,
                        "retrieveAllServersJMXAddresses", new Object[0], new String[0]);
                for (String address : containerJMXAddresses) {
                    JMXServiceURL containerJMXURL = new JMXServiceURL(address);
                    JMXConnector containerConnector = JMXConnectorFactory.connect(containerJMXURL);
                    MBeanServerConnection containerConnection = containerConnector.getMBeanServerConnection();
                    Set<ObjectInstance> containers = containerConnection.queryMBeans(
                            new ObjectName("*:*,type=ObjectGridContainer"), null);
                    for (ObjectInstance container : containers) {
                        System.out.println(containerConnection.getAttribute(container.getObjectName(), "Status"));
                    }
                }
            } finally {
                if(jmxCon != null) {
                    jmxCon.close();
                }
            }
        }
    }
    The example program prints out the container server status for each container. An example of the output follows:
    <container name="Container-0" zoneName="DefaultZone" hostName="descartes.rchland.ibm.com" 
    		serverName="ogserver">
      <shard type="Primary" partitionName="1"/>
      <shard type="Primary" partitionName="0"/>
      <shard type="Primary" partitionName="3"/>
      <shard type="Primary" partitionName="2"/>
    </container>
    
  • Connect to a catalog service MBean server that is hosted in WebSphere Application Server:

    The method for programmatically accessing MBeans in WebSphere Application Server is slightly different from accessing MBeans in a stand-alone configuration.

    1. Create and compile a Java program to connect to the MBean server.
      An example program follows:
      Figure 3. CollectPlacementPlan.java
      package com.ibm.websphere.sample.xs.admin;
      
      import java.util.Set;
      
      import javax.management.MBeanServerConnection;
      import javax.management.ObjectName;
      import javax.management.remote.JMXConnector;
      import javax.management.remote.JMXConnectorFactory;
      import javax.management.remote.JMXServiceURL;
      
      /**
       * Collects the placement information from the catalog server running in a deployment manager for a given ObjectGrid.
       */
      public final class CollectPlacementPlanWAS {
          private static String hostName = "localhost";
      
          private static int port = 9809;
      
          private static String objectGridName = "library";
      
          private static String mapSetName = "ms1";
      
          /**
           * Connects to the catalog service to retrieve placement information and prints it out.
           *
           * @param args
           * @throws Exception
           *             If there is a problem connecting to the catalog service MBean server.
           */
          public static void main(String[] args) throws Exception {
      
              // connect to bootstrap port of the deployment manager
              String serviceURL = "service:jmx:iiop://" + hostName + ":" + port + "/jndi/JMXConnector";
              JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
              JMXConnector jmxCon = JMXConnectorFactory.connect(jmxUrl);
      
              try {
                  MBeanServerConnection catalogServerConnection = jmxCon.getMBeanServerConnection();
      
                  Set placementSet = 
      						catalogServerConnection.queryNames(new ObjectName("com.ibm.websphere.objectgrid"
                   + ":*,type=PlacementService"), null);
      
                  ObjectName placementService = (ObjectName) placementSet.iterator().next();
                  Object placementXML = catalogServerConnection.invoke(placementService,
                              "listObjectGridPlacement", new Object[] {
                          objectGridName, mapSetName }, new String[] { String.class.getName(), 
      										String.class.getName() });
                  System.out.println(placementXML);
              } finally {
                  if(jmxCon != null) {
                      jmxCon.close();
                  }
              }
          }
      
      }
    2. Run the following command.
      "$JAVA_HOME/bin/java" "$WAS_LOGGING" -Djava.security.auth.login.config="$app_server_root/properties/wsjaas_client.conf" \
       -Djava.ext.dirs="$JAVA_HOME/jre/lib/ext:$WAS_EXT_DIRS:$WAS_HOME/plugins:$WAS_HOME/lib/WMQ/java/lib" \
       -Djava.naming.provider.url=<an_IIOP_URL_or_a_corbaloc_URL_to_your_application_server_machine_name> \
       -Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory \
       -Dserver.root="$WAS_HOME" "$CLIENTSAS" "$CLIENTSSL" $USER_INSTALL_PROP \
       -classpath "$WAS_CLASSPATH":<list_of_your_application_jars_and_classes> \
       <fully_qualified_class_name_to_run> <your_application_parameters>
      This command assumes that the was_root/bin/setupCmdLine.sh script has been run to set the variables properly. An example of the format of the java.naming.provider.url property value is corbaloc:iiop:1.0@<host>:<port>/NameService.
  • Connect to a catalog service MBean server with security enabled:

    For more information about connecting to the catalog service MBean with security enabled, see Java Management Extensions (JMX) security.

What to do next

For more examples on how to display statistics and perform administrative operations with MBeans, see the xsadmin sample application. You can look at the source code of the xsadmin sample application in the wxs_home/samples/xsadmin.jar file in a stand-alone installation, or in the wxs_home/xsadmin.jar file in a WebSphere Application Server installation. See Sample: xsadmin utility for more information about the operations you can complete with the xsAdmin sample application.

You can also find more information about MBeans in the com.ibm.websphere.objectgrid.management package.