Using the PMI client interfaces

This section discusses the use of the PMI client interfaces in applications. The basic programming model is as follows:

  1. A client uses the CpdCollection interface to retrieve an initial collection, or snapshot, of performance data from the server. This snapshot, which is called Snapshot in this example, is provided in a hierarchical structure as described in Data organization and hierarchy, and contains the current values of all performance data collected by the server. The snapshot maintains the same structure throughout the lifetime of the CpdCollection instance.

  2. The client processes and displays the data as specified. Processing and display objects (for example, filters and GUIs) can register as CpdEvent listeners to data of interest; see The CpdEventListener and CpdEvent interfaces for details. When the client receives updated data, all listeners are notified.

  3. When the client collects new or changed data (for example, data collections named S1, S2, and so on) from the server, the client uses the update method to update Snapshot with the new data:
    Snapshot.update(S1);
    // ...later...
    Snapshot.update(S2);
    

  4. Step 2 and Step 3 are repeated through the lifetime of the client.

Figure 11 lists a sample of PMI client code.

Figure 11. Example of PMI client code


import com.ibm.websphere.pmi.*;
import com.ibm.websphere.pmi.server.*;
import com.ibm.websphere.pmi.client.*;
 
public class PmiTest implements PmiConstants {
 
   // A test driver
   // If arguments are provided:
   //   args[0] = node name
   //   args[1] = port number
   //   args[2] = The JNDI name of PerfRetrieve
   //
   // Note: This will not work unless an admin server and 
   // perfServer are running
   //
 
public static void main(String[] args) {
     String hostName = null;
     String portNumber = null;
     String homeName = null;
     if (args.length >= 1)
        hostName = args[0];
     if (args.length >=2)
        portNumber = args[1];
     if (args.length >=3)
        homeName = args[2];
 
     PmiClient pmiClnt = new PmiClient(hostName, portNumber, homeName);
 
     // Root of PMI data tree
     CpdCollection rootCol = pmiClnt.createRootCollection();
 
 
     // Set performance descriptor (pd) list
     // pdList will include all PerfDescriptors for data retrieval
     PerfDescriptorList pdList = new PerfDescriptorList();
     try {
          // If you want to query PmiClient to find the PerfDescriptor
          // you need, you can go through listNodes, listServers, and
          // listMembers to list all the PerfDescriptors and extract
          // the one you want.
          PerfDescriptor[] nodePds = pmiClnt.listNodes();
          String nodeName = nodePds[0].getName();
          System.out.println("after listNodes:" + nodeName);
          PerfDescriptor[] serverPds = pmiClnt.listServers(
            nodePds[0].getName());
          System.out.println("after listServers");
 
          if (serverPds == null || serverPds.length == 0) {
               System.out.println("NO app server in node");
               return;
          }
 
         // For a simple test, get from the first server
          PerfDescriptor[] myPds = pmiClnt.listMembers(serverPds[0]);
          // You can add all pds to PerfDescriptorList
          for (int i = 0; i < myPds.length; i++) {
            if (myPds[i].getModuleName().equals(
                "com.ibm.websphere.pmi.beanModule")
            || myPds[i].getModuleName().equals(
                "com.ibm.websphere.pmi.connectionPoolModule")
            || myPds[i].getModuleName.equals(
                "com.ibm.websphere.pmi.webAppModule"))
               pdList.addDescriptor(myPds[i]);
          }
 
          // Or, if you know the data path you want, you can create your own
          String[] thisPath = new String[]{"thisNode", "thisServer",
               "com.ibm.websphere.pmi.transactionModule"};
          // Suppose you are interested only in dataIds 1, 2, and 3
          PerfDescriptor thisPd = pmiClnt.createPerfDescriptor(thisPath,
               new int[]{1, 2, 3});
          pdList.addDescriptor(thisPd);
 
} catch (Exception ex) {
               System.out.println("Exception calling CollectorAE");
               ex.printStackTrack();
     }
 
 
     // Retrieve the data in pdList
     CpdCollection[] cpdCols = null;
     try {
          for (int i = 0; i < 10; i++) {
               java.lang.Thread.sleep(1000);
               cpdCols = pmiClnt.gets(pdList, true);
               if (cpdCols == null || cpdCols.length == 0) {
                    System.out.println(
                      "PMI data return null--possible wrong pds");
               }
               for (int j = 0; j < cpdCols.length; j=++) {
                    rootCol.update(cpdCols[j]);
                    report(cpdCols[j]);
               }
          }
     } catch (Exception ex {
               System.out.println("Exception to call thread sleep");
     }
   }
 
   // Simple method to make sure we are getting the correct CpdCollection
   private static void report(CpdCollection col) {
        System.out.println("\n\n");
        if (col == null) {
             System.out.println("report: null CpdCollection");
             return;
        }
        System.out.println("report--CpdCollection ");
        printPD(col.getDescriptor());
        CpdData[] dataMembers = col.dataMembers();
        if (dataMembers != null) {
             System.out.println("report CpdCollection: dataMembers is " +
                  dataMembers.length);
             for (int i = 0; i < dataMembers.length; i++) {
                  CpdData data = dataMembers[i];
                  printPD(data.getDescriptor());
             }
 
        }
        CpdCollection[] subCollections = col.subcollections();
        if (subCollections != null) {
             for (int i = 0; i < subCollections.length; i++) {
                  report(subCollections[i]);
             }
        }
   }
 
   // Simple method to write the full name of a pd
   private static void printPD(PerfDescriptor pd) {
        System.out.println(pd.getFullName());
   }
}