Example: Administering Java Management Extension-based interface

Example: Administering Java Management Extension-based interface

The page provides example code directly using Java Management Extension (JMX) API. For information on compiling your source code, see "Compiling your monitoring applications."

Example

package com.ibm.websphere.pmi;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.ConnectorException;
import com.ibm.websphere.management.exception.InvalidAdminClientTypeException;
import com.ibm.websphere.management.exception.*;

import java.util.*;
import javax.management.*;
import com.ibm.websphere.pmi.*;
import com.ibm.websphere.pmi.client.*;
import com.ibm.websphere.pmi.stat.*;

/**
 * Sample code using AdminClient API to get PMI data from PerfMBean  * and individual MBeans.
 * 
 * @ibm-api  */

public class PmiJmxTest implements PmiConstants 
{

    private AdminClient    ac = null;
    private ObjectName     perfOName   = null;
    private ObjectName     serverOName = null;
    private ObjectName     wlmOName    = null;
    private ObjectName     jvmOName    = null;
    private ObjectName     orbtpOName    = null;
    private boolean failed = false;
    private PmiModuleConfig[] configs = null;

    /**
     *  Creates a new test object      *  (Need a default constructor for the testing framework)
     */
    public PmiJmxTest() 
    {
    }

    /**
     * @param args[0] host      * @param args[1] port, optional, default is 8880
     * @param args[2] connectorType, optional, default is SOAP connector      *   
     */
    public static void main(String[] args) 
    {
        PmiJmxTest instance = new PmiJmxTest();

        // parse arguments and create AdminClient object         instance.init(args);

        // navigate all the MBean ObjectNames and cache those we are interested         instance.getObjectNames();

        boolean v6 = !(new Boolean(System.getProperty ("websphereV5Statistics"))).
     booleanValue();
        
        if( v6 )
        {
            // test V6 APIs             instance.doTestV6();
        }
        else         {

            // set level, get data, display data             instance.doTest();

            // test for EJB data             instance.testEJB();

            // how to use JSR77 getStats method for individual MBean other than PerfMBean             instance.testJSR77Stats();
        }            

    }

    /**
     * parse args and getAdminClient      */
    public void init(String[] args) 
    {

        try 
        {
            String  host    = null;
            String  port    = "8880";     
            String  connector = AdminClient.CONNECTOR_TYPE_SOAP;
            if(args.length < 1) {
                System.err.println("ERROR: Usage: PmiJmxTest <host> [<port>] 
[<connector>]");
                System.exit(2);
            }
            else 
            {
                host = args[0];

                if (args.length > 1)
                    port = args[1];

                if (args.length > 2)
                    connector = args[2];
            }

            if(host == null) {
                host = "localhost";
            }
            if(port == null) {
                port = "2809";
            }
            if (connector == null) {
                connector = AdminClient.CONNECTOR_TYPE_SOAP;
            }
            System.out.println("host=" + host + " , port=" + port + 
",connector=" + connector);

            //--------------------------------------------------------------------
            // Get the ac object for the AppServer             //---------------------------------------------------------------------
            System.out.println("main: create the adminclient");
            ac = getAdminClient(host, port, connector);

        } 
        catch (Exception ex) 
        {
            failed = true;
            new AdminException(ex).printStackTrace();
            ex.printStackTrace();
        }
    }

    /**
     * get AdminClient using the given host, port, and connector      */
    public AdminClient getAdminClient(String hostStr, String portStr, String 
    connector) {
        System.out.println("getAdminClient: host=" + hostStr + " , portStr=" + 
portStr);
        AdminClient ac = null;
        java.util.Properties props = new java.util.Properties();
        props.put(AdminClient.CONNECTOR_TYPE, connector);
        props.put(AdminClient.CONNECTOR_HOST, hostStr);
        props.put(AdminClient.CONNECTOR_PORT, portStr);

        /* set the following properties if security is enabled and using SOAP 
   connector */
        /* The following shows how to set properties for SOAP connector when 
   security is enabled.
           See AdminClient javadoc for more info.
        Properties props = new Properties();     
       props.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
       props.setProperty(AdminClient.CONNECTOR_PORT, "8880");
       props.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_
   TYPE_SOAP);
       props.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
       props.setProperty(AdminClient.USERNAME, "test2");
      props.setProperty(AdminClient.PASSWORD, "user24test");
        props.setProperty("javax.net.ssl.trustStore", 
  "C:/WebSphere/AppServer/etc/DummyClientTrustFile.jks");
    props.setProperty("javax.net.ssl.keyStore", 
  "C:/WebSphere/AppServer/etc/DummyClientKeyFile.jks");
    props.setProperty("javax.net.ssl.trustStorePassword", "WebAS");
    props.setProperty("javax.net.ssl.keyStorePassword", "WebAS");
        */ 

        try {
            ac = AdminClientFactory.createAdminClient(props);
        }
        catch(Exception ex) {
            failed = true;
            new AdminException(ex).printStackTrace();
            System.out.println("getAdminClient: exception");
        }
        return ac;
    }


    /**
     * get all the ObjectNames.
     */
    public void getObjectNames() {

        try {

            //--------------------------------------------------------------------
            // Get a list of object names             //--------------------------------------------------------------------
            javax.management.ObjectName on = new javax.management.ObjectName    ("WebSphere:*");

            //---------------------------------------------------------------------
            // get all objectnames for this server             //--------------------------------------------------------------------
           Set objectNameSet= ac.queryNames(on, null);

            //--------------------------------------------------------------------
            // get the object names that we care about: Perf, Server, JVM, WLM (only applicable in 
ND)
            //--------------------------------------------------------------------
            if(objectNameSet != null) {
                Iterator i = objectNameSet.iterator();
                while (i.hasNext()) {
                    on = (ObjectName)i.next();
                    String type = on.getKeyProperty("type");

                    // uncomment it if you want to print the ObjectName for each MBean                     // System.out.println("\n\n" + on.toString());

                    // find the MBeans we are interested                     if(type != null && type.equals("Perf")) {
                        System.out.println("\nMBean: perf =" + on.toString());
                        perfOName = on;
                    }
                    if(type != null && type.equals("Server")) {
                        System.out.println("\nMBean: Server =" + on.toString());
                        serverOName = on;
                    }
                    if(type != null && type.equals("JVM")) {
                        System.out.println("\nMBean: jvm =" + on.toString());
                        jvmOName = on;
                    }
                    if(type != null && type.equals("WLMAppServer")) {
                        System.out.println("\nmain: WLM =" + on.toString());
                        wlmOName = on;
                    }
                    if(type != null && type.equals("ThreadPool")) 
                    {
                        String name = on.getKeyProperty("name");
                        if (name.equals("ORB.thread.pool"))
                            System.out.println("\nMBean: ORB ThreadPool =" + 
on.toString());
                        orbtpOName = on;
                    }
                }
            }
            else {
                System.err.println("main: ERROR: no object names found");
                System.exit(2);
            }

            // You must have Perf MBean in order to get PMI data.
            if (perfOName == null) 
            {
                System.err.println("main: cannot get PerfMBean. Make sure PMI is enabled");
                System.exit(3);
            }
        }
        catch(Exception ex) 
        {
            failed = true;
            new AdminException(ex).printStackTrace();
            ex.printStackTrace();
        }

    }

    /** Test V6 APIs */
    public void doTestV6 ()
    {
        System.out.println ("\ndoTestV6() output:\n");

        // the following methods are specific to V6 and demonstrates the V6 API..so set the flag to 
false         String v5PropFlag = System.setProperty ("websphereV5Statistics", 
"false");
        try         {        
            Object[] params;
            String[] signature;
    
            // get current statistic set that is used for monitoring             System.out.println ("\nCurrent statistic set: " + ac.invoke(perfOName, 
"getStatisticSet", null, null));
    

            // get all statistics from the server using Perf MBean             System.out.println ("\nGet all statistics in PMI tree");
            signature = new 
String[]{"[Lcom.ibm.websphere.pmi.stat.StatDescriptor;","java.lang.Boolean"};
            params = new Object[] {new StatDescriptor[]{new StatDescriptor(null)}, new 
Boolean(true)};
    
            com.ibm.websphere.pmi.stat.WSStats[] wsStats = (com.ibm.websphere.pmi.stat.WSStats[])
            ac.invoke(perfOName, "getStatsArray", params, signature);   
            System.out.println (wsStats[0].toString());


            // get statistics from one JVM MBean using J2EE JMX
            System.out.println ("\nGet JVM statistics using JVM MBean");
            javax.management.j2ee.statistics.Stats j2eeStats = 
(javax.management.j2ee.statistics.Stats) 
            ac.getAttribute(jvmOName, "stats");
            System.out.println (j2eeStats.toString());


            // get statistics from a specific thread pool -- WebContainer thread pool             System.out.println ("\nGet statistics for a specific thread pool");
            signature = new 
String[]{"[Lcom.ibm.websphere.pmi.stat.StatDescriptor;","java.lang.Boolean"};

            StatDescriptor webContainerPoolSD = new StatDescriptor (new String[] 
{WSThreadPoolStats.NAME, "WebContainer"});
            params = new Object[] {new StatDescriptor[]{webContainerPoolSD}, new Boolean(true)};
    
            wsStats = (com.ibm.websphere.pmi.stat.WSStats[])
            ac.invoke(perfOName, "getStatsArray", params, signature);   
            System.out.println (wsStats[0].toString());


            // set monitoring to statistic set "extended"
            System.out.println ("\nSet monitoring to statistic set 'Extended'");
            signature = new String[]{"java.lang.String"};
            params = new Object[] {StatConstants.STATISTIC_SET_EXTENDED};
    
            ac.invoke(perfOName, "setStatisticSet", params, signature);   
            
            // get current statistic set that is used for monitoring             System.out.println ("\nCurrent statistic set: "+ ac.invoke(perfOName, 
"getStatisticSet", null, null));

            
            // selectively enable statistics for all thread pools             System.out.println ("\nSelectively enable statistics (ActiveCount and PoolSize 
statistics) for thread pool -- fine grained control");

            StatDescriptor threadPoolSD = new StatDescriptor (new String[]
   {WSThreadPoolStats.NAME});    
                // create a spec object to enable ActiveCount and PoolSize on the thread pool             StatLevelSpec[] spec = new StatLevelSpec[1];
            spec[0] = new StatLevelSpec (threadPoolSD.getPath(), new int[] 
   {WSThreadPoolStats.ActiveCount, WSThreadPoolStats.PoolSize});
            
            signature = new 
String[]{"[Lcom.ibm.websphere.pmi.stat.StatLevelSpec;","java.lang.Boolean"};
            params = new Object[] {spec, new Boolean(true)};                
               
            ac.invoke(perfOName, "setInstrumentationLevel", params, signature);


            // get current statistic set that is used for monitoring             System.out.println ("\nCurrent statistic set: "+ ac.invoke(perfOName, 
"getStatisticSet", null, null));

            // get statistics from all thread pools             System.out.println ("\nGet statistics from all thread pools");
            signature = new 
String[]{"[Lcom.ibm.websphere.pmi.stat.StatDescriptor;","java.lang.Boolean"};
            params = new Object[] {new StatDescriptor[]{threadPoolSD},new Boolean(true)};
    
            wsStats = (com.ibm.websphere.pmi.stat.WSStats[])
            ac.invoke(perfOName, "getStatsArray", params, signature);   
            System.out.println (wsStats[0].toString());

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        // set the property to original value         System.setProperty ("websphereV5Statistics", v5PropFlag);
    }
        
    /**
     * Some sample code to set level, get data, and display data. (V5)
     * @deprecated Use 6.0 APIs.
     */
    public void doTest() 
    {
        try 
        {
            // first get all the configs  - used to set static info for Stats 
            // Note: server only returns the value and time info. 
            //       No description, unit, etc is returned with PMI data to reduce communication 
cost.
            //       You have to call setConfig to bind the static info and Stats data later.
            configs = (PmiModuleConfig[])ac.invoke(perfOName, "getConfigs", null, null);

            // print out all the PMI modules and matching mbean types             for (int i=0; i<configs.length; i++)
                System.out.println("config: moduleName=" + configs[i].getShortName() + 
", mbeanType=" + configs[i].getMbeanType());

            // set the instrumentation level for the server             setInstrumentationLevel(serverOName, null, PmiConstants.LEVEL_HIGH);

            // example to use StatDescriptor.
            // Note WLM module is only available in ND.
            StatDescriptor sd = new StatDescriptor(new String[] {"wlmModule.server"});
            setInstrumentationLevel(wlmOName, sd, PmiConstants.LEVEL_HIGH);

            // example to getInstrumentationLevel             MBeanLevelSpec[] mlss = getInstrumentationLevel(wlmOName, sd, true);
            // you can call getLevel(), getObjectName(), getStatDescriptor() on mlss[i]

            // get data for the server             Stats stats = getStatsObject(serverOName, true);
            System.out.println(stats.toString());

            // get data for WLM server submodule             stats = getStatsObject(wlmOName, sd, true);
            if (stats == null)
                System.out.println("Cannot get Stats for WLM data");
            else 
                System.out.println(stats.toString());
            
            // get data for JVM MBean             stats = getStatsObject(jvmOName, true);
            processStats(stats);

            // get data for multiple MBeans             ObjectName[] onames = new ObjectName[]{orbtpOName, jvmOName};
            Object[] params = new Object[]{onames, new Boolean(true)};
            String[] signature = new String[]{"[Ljavax.management.ObjectName;", 
"java.lang.Boolean"};
            Stats[] statsArray = (Stats[])ac.invoke(perfOName, "getStatsArray", params, 
signature);
            // you can call toString or processStats on statsArray[i]

            if (!failed)
                System.out.println("All tests passed");
            else                 System.out.println("Some tests failed");
        }
        catch(Exception ex) 
        {
            new AdminException(ex).printStackTrace();
            ex.printStackTrace();
        }
    }


    /**
     * Sample code to get level      */
    protected MBeanLevelSpec[] getInstrumentationLevel(ObjectName on, StatDescriptor sd, boolean 
recursive) 
    {
        if (sd == null)
            return getInstrumentationLevel(on, recursive);
        System.out.println("\ntest getInstrumentationLevel\n");
        try {
            Object[] params = new Object[2];
            params[0] = new MBeanStatDescriptor(on, sd);
            params[1] = new Boolean(recursive);
            String[] signature= new String[]{ 
"com.ibm.websphere.pmi.stat.MBeanStatDescriptor", "java.lang.Boolean"};
            MBeanLevelSpec[] mlss = (MBeanLevelSpec[])ac.invoke(perfOName, 
"getInstrumentationLevel", params, signature);
            return mlss;
        }
        catch(Exception e) {
            new AdminException(e).printStackTrace();
            System.out.println("getInstrumentationLevel: Exception Thrown");
            return null;
        }
    }

    /**
     * Sample code to get level      */
    protected MBeanLevelSpec[] getInstrumentationLevel(ObjectName on, 
     boolean recursive) {
        if (on == null)
            return null;
        System.out.println("\ntest getInstrumentationLevel\n");
        try {
            Object[] params = new Object[]{on, new Boolean(recursive)};
            String[] signature= new String[]{ "javax.management.ObjectName", 
     "java.lang.Boolean"};
            MBeanLevelSpec[] mlss = (MBeanLevelSpec[])ac.invoke(perfOName, 
     "getInstrumentationLevel", params, signature);
            return mlss;
        }
        catch(Exception e) {
            new AdminException(e).printStackTrace();
            failed = true;
            System.out.println("getInstrumentationLevel: Exception Thrown");
            return null;
        }
    }

    /**
     * Sample code to set level      * @deprecated Use 6.0 APIs.
     */
    protected void setInstrumentationLevel(ObjectName on, StatDescriptor sd, 
     int level) {
        System.out.println("\ntest setInstrumentationLevel\n");
        try {
            Object[] params       = new Object[2];
            String[] signature    = null;
            MBeanLevelSpec[] mlss = null; 
            params[0] = new MBeanLevelSpec(on, sd, level);
            params[1] = new Boolean(true);

            signature= new String[]{ 
"com.ibm.websphere.pmi.stat.MBeanLevelSpec","java.lang.Boolean"};
            ac.invoke(perfOName, "setInstrumentationLevel", params, signature);
        }
        catch(Exception e) {
            failed = true;
            new AdminException(e).printStackTrace();
            System.out.println("setInstrumentationLevel: FAILED: Exception Thrown");
        }
    }

    /**
     * Sample code to get a Stats object      * @deprecated Use 6.0 APIs.
     */
    public Stats getStatsObject(ObjectName on, StatDescriptor sd, boolean 
     recursive) {

        if (sd == null)  
            return getStatsObject(on, recursive);

        System.out.println("\ntest getStatsObject\n");
        try {
            Object[] params    = new Object[2];
            params[0] = new MBeanStatDescriptor(on, sd);  // construct MBeanStatDescriptor             params[1] = new Boolean(recursive);
            String[] signature = new String[] { 
"com.ibm.websphere.pmi.stat.MBeanStatDescriptor", "java.lang.Boolean"};
            Stats stats  = (Stats)ac.invoke(perfOName, "getStatsObject", params, 
signature);
            
            if (stats == null) return null;

            
            // find the PmiModuleConfig and bind it with the data             String type = on.getKeyProperty("type");
            if (type.equals(MBeanTypeList.SERVER_MBEAN))
                setServerConfig(stats);
            else                 stats.setConfig(PmiClient.findConfig(configs, on));
            
            return stats;

        } catch(Exception e) {
            failed = true;
            new AdminException(e).printStackTrace();
            System.out.println("getStatsObject: Exception Thrown");
            return null;
        }
    }

    /**
     * Sample code to get a Stats object      */
    public Stats getStatsObject(ObjectName on, boolean recursive) {
        if (on == null)  
            return null;

        System.out.println("\ntest getStatsObject\n");

        try {
            Object[] params  = new Object[]{on, new Boolean(recursive)};
            String[] signature = new String[] { "javax.management.ObjectName", 
     "java.lang.Boolean"};
            Stats stats  = (Stats)ac.invoke(perfOName, "getStatsObject", params, 
     signature);
            
            
            // find the PmiModuleConfig and bind it with the data             String type = on.getKeyProperty("type");
            if (type.equals(MBeanTypeList.SERVER_MBEAN))
                setServerConfig(stats);
            else                 stats.setConfig(PmiClient.findConfig(configs, on));
            
            return stats;

        } 
        catch(Exception e) {
            failed = true;
            new AdminException(e).printStackTrace();
            System.out.println("getStatsObject: Exception Thrown");
            return null;
        }
    }

    /**
     * Sample code to navigate and get the data value from the Stats object.
     */
    private void processStats(Stats stat) {
        processStats(stat, "");
    }

    /**
     * Sample code to navigate and get the data value from the Stats and 
   Statistic object.
     * @deprecated Use 6.0 APIs.
     */
    private void processStats(Stats stat, String indent) {
        if(stat == null)  return;

        System.out.println("\n\n");

        // get name of the Stats         String name = stat.getName();
        System.out.println(indent + "stats name=" + name);

        // list data names         String[] dataNames = stat.getStatisticNames();
        for (int i=0; i<dataNames.length; i++)
            System.out.println(indent + "    " + "data name=" + dataNames[i]);
        System.out.println("");

        // list all datas         //com.ibm.websphere.management.statistics.Statistic[] allData = 
   stat.getStatistics();

        // cast it to be PMI's Statistic type so that we can have get more         // Also show how to do translation.
        //Statistic[] dataMembers = (Statistic[])allData;
        Statistic[] dataMembers = stat.listStatistics();
        if(dataMembers != null) {
            for(int i=0; i<dataMembers.length; i++)  {
                System.out.print(indent + "    " + "data name=" + 
   PmiClient.getNLSValue(dataMembers[i].getName())
                                 + ", description=" + PmiClient.getNLSValue    (dataMembers[i].getDescription())
                                 + ", startTime=" + dataMembers[i].getStartTime()
                                 + ", lastSampleTime=" + 
dataMembers[i].getLastSampleTime());
                if(dataMembers[i].getDataInfo().getType() == TYPE_LONG) {
                    System.out.println(", count=" + 
((CountStatisticImpl)dataMembers[i]).getCount());
                }
                else if(dataMembers[i].getDataInfo().getType() == TYPE_STAT) {
                    TimeStatisticImpl data = (TimeStatisticImpl)dataMembers[i];
                    System.out.println(", count=" + data.getCount()
                                       + ", total=" + data.getTotal()
                                       + ", mean=" + data.getMean()
                                       + ", min=" + data.getMin()
                                       + ", max=" + data.getMax());
                }
                else if(dataMembers[i].getDataInfo().getType() == TYPE_LOAD) {
                    RangeStatisticImpl data = (RangeStatisticImpl)dataMembers[i];
                    System.out.println(", current=" + data.getCurrent()
                                       + ", integral=" + data.getIntegral()
                                       + ", avg=" + data.getMean()
                                       + ", lowWaterMark=" + data.getLowWaterMark()
                                       + ", highWaterMark=" + data.getHighWaterMark());
                }
            }
        }

        // recursively for sub-stats         Stats[] substats = (Stats[])stat.getSubStats();
        if(substats == null || substats.length == 0)
            return;
        for(int i=0; i<substats.length; i++) {
            processStats(substats[i], indent + "    ");
        }
    }

    /**
     * The Stats object returned from server does not have static config info. 
  You have to set it on client side.     
     */
    public void setServerConfig(Stats stats) {
        if(stats == null) return;
        if(stats.getType() != TYPE_SERVER) return;
        
        PmiModuleConfig config = null;

        Stats[] statList = stats.getSubStats();
        if (statList == null || statList.length == 0)
            return;
        Stats oneStat = null;
        for(int i=0; i<statList.length; i++) {
            oneStat = statList[i];
            if (oneStat == null) continue;
            config = PmiClient.findConfig(configs, oneStat.getStatsType());     
     //getName             if(config != null)
                oneStat.setConfig(config);
            else 
            {                
                config = getStatsConfig (oneStat.getStatsType());
                if (config != null)
                    oneStat.setConfig(config);
                else                     System.out.println("Error: get null config for " + 
oneStat.getStatsType());
            }
        }
    }

    /**
     * sample code to show how to get a specific MBeanStatDescriptor      * @deprecated Use 6.0 APIs.
     */
    public MBeanStatDescriptor getStatDescriptor(ObjectName oName, String name) {
        try {
            Object[] params = new Object[]{serverOName};
            String[] signature= new String[]{"javax.management.ObjectName"};
            MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke    (perfOName, "listStatMembers", params, signature);
            if (msds == null)
                return null;
            for (int i=0; i<msds.length; i++) {
                if (msds[i].getName().equals(name))
                    return msds[i];
            }
            return null;
        }
        catch(Exception e) {
            new AdminException(e).printStackTrace();
            System.out.println("listStatMembers: Exception Thrown");
            return null;
        }

    }

    /** 
     * sample code to show you how to navigate MBeanStatDescriptor via 
    listStatMembers 
     * @deprecated Use 6.0 APIs.
     */
    public MBeanStatDescriptor[] listStatMembers(ObjectName mName) {
        if (mName == null)
            return null;

        try {
            Object[] params = new Object[]{mName};
            String[] signature= new String[]{"javax.management.ObjectName"};
            MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke      (perfOName, "listStatMembers", params, signature);
            if (msds == null)
                return null;
            for (int i=0; i<msds.length; i++) {
                MBeanStatDescriptor[] msds2 = listStatMembers(msds[i]);
            }
            return null;
        }
        catch(Exception e) {
            new AdminException(e).printStackTrace();
            System.out.println("listStatMembers: Exception Thrown");
            return null;
        }

    }


    /**
     * Sample code to get MBeanStatDescriptors      * @deprecated Use 6.0 APIs.
     */
    public MBeanStatDescriptor[] listStatMembers(MBeanStatDescriptor mName) {
        if (mName == null)
            return null;

        try {
            Object[] params = new Object[]{mName};
            String[] signature= new 
String[]{"com.ibm.websphere.pmi.stat.MBeanStatDescriptor"};
            MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke(perfOName, 
"listStatMembers", params, signature);
            if (msds == null)
                return null;
            for (int i=0; i<msds.length; i++) {
                MBeanStatDescriptor[] msds2 = listStatMembers(msds[i]);
                // you may recursively call listStatMembers until find the one you want             }
            return msds;
        }
        catch(Exception e) {
            new AdminException(e).printStackTrace();
            System.out.println("listStatMembers: Exception Thrown");
            return null;
        }

    }

    /** 
     * sample code to get PMI data from beanModule      * @deprecated Use 6.0 APIs.
     */
    public void testEJB() {

        // This is the MBeanStatDescriptor for Enterprise EJB
        MBeanStatDescriptor beanMsd = getStatDescriptor(serverOName, 
     PmiConstants.BEAN_MODULE);
        if (beanMsd == null)
            System.out.println("Error: cannot find beanModule");
        
        // get the Stats for module level only since recursive is false         Stats stats = getStatsObject(beanMsd.getObjectName(), beanMsd.
   getStatDescriptor(), false); // pass true if you wannt data from individual beans         
        // find the avg method RT 
        TimeStatisticImpl rt = (TimeStatisticImpl)stats.getStatistic    (EJBStatsImpl.METHOD_RT);
        System.out.println("rt is " + rt.getMean());

        try {
            java.lang.Thread.sleep(5000);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        // get the  Stats again         Stats stats2 = getStatsObject(beanMsd.getObjectName(), beanMsd.
   getStatDescriptor(), false); // pass true if you wannt data from individual beans         
        // find the avg method RT
        TimeStatisticImpl rt2 = (TimeStatisticImpl)stats2.getStatistic    (EJBStatsImpl.METHOD_RT);
        System.out.println("rt2 is " + rt2.getMean());

        // calculate the difference between this time and last time.
        TimeStatisticImpl deltaRt = (TimeStatisticImpl)rt2.delta(rt);
        System.out.println("deltaRt is " + rt.getMean());

    }

    /**
     * Sample code to show how to call getStats on StatisticProvider MBean 
   directly.
     * @deprecated Use 6.0 APIs.
     */
    public void testJSR77Stats() {
        // first, find the MBean ObjectName you are interested.
        // Refer method getObjectNames for sample code.

        // assume we want to call getStats on JVM MBean to get statistics         try {

            com.ibm.websphere.management.statistics.JVMStats stats = 
                    (com.ibm.websphere.management.statistics.JVMStats)ac.
    invoke(jvmOName, "getStats", null, null);
            
            System.out.println("\n get data from JVM MBean");
            
            if (stats == null) {
                System.out.println("WARNING: getStats on JVM MBean returns null");
            } else {

                // first, link with the static info if you care                 ((Stats)stats).setConfig(PmiClient.findConfig(configs, jvmOName));

                // print out all the data if you want                 //System.out.println(stats.toString());

                // navigate and get the data in the stats object                 processStats((Stats)stats);

                // call JSR77 methods on JVMStats to get the related data                 com.ibm.websphere.management.statistics.CountStatistic upTime = 
      stats.getUpTime();
                com.ibm.websphere.management.statistics.BoundedRangeStatistic 
      heapSize = stats.getHeapSize();

                if (upTime != null)
                    System.out.println("\nJVM up time is " + upTime.getCount());
                if (heapSize != null)
                    System.out.println("\nheapSize is " + heapSize.getCurrent());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            new AdminException(ex).printStackTrace();
        }
    }


    /**
     Get PmiModuleConfig from server      */   
    public PmiModuleConfig getStatsConfig (String statsType)
    {
       try        {       
            return (PmiModuleConfig)ac.invoke(perfOName, "getConfig", 
                                                new String[]{statsType}, 
                                                new String[]{"java.lang.String"});
       }
       catch(Exception e)
       {
            e.printStackTrace();
            return null;
       }
    }
    
    /** 
     * Get PmiModuleConfig based on MBean ObjectName
      @deprecated Use com.ibm.websphere.pmi.client.PmiClient.findConfig()
     */
    public PmiModuleConfig findConfig(ObjectName on) {
        if (on == null) return null;
        
        String type = on.getKeyProperty("type");
        System.out.println("findConfig: mbean type =" + type);
        
        for (int i=0; i<configs.length ; i++) {

            if (configs[i].getMbeanType().equals(type))
                return configs[i];
        }
        System.out.println("Error: cannot find the config");
        return null;
        
    }

    /**
     * Get PmiModuleConfig based on PMI module name 
     @deprecated Use com.ibm.websphere.pmi.client.PmiClient.findConfig()
     */
    public PmiModuleConfig findConfig(String moduleName) {
        if (moduleName == null) return null;

        for (int i=0; i<configs.length ; i++) {

            if (configs[i].getShortName().equals(moduleName))
                return configs[i];
        }
        System.out.println("Error: cannot find the config");
        return null;
        
    }
}





 

Related tasks


Using PMI client to develop your monitoring application (deprecated)
Compiling your monitoring applications

Searchable topic ID: rprf_samplecodeapidirect