You can use the AdminClient Java Management Extension (JMX) interface to invoke methods on MBeans. The AdminClient API can retrieve Performance Monitoring Infrastructure (PMI) data with PerfMBean or with individual MBeans.
Individual MBeans provide the Stats attribute from which you can get PMI data. PerfMBean provides extended methods for PMI administration and more efficient ways to access PMI data. To set the PMI module instrumentation level, you must invoke methods on PerfMBean. To query PMI data from multiple MBeans, it is faster to invoke the getStatsArray method in PerfMBean than to get the Stats attribute from multiple individual MBeans. PerfMBean can deliver PMI data in a single JMX cell, but multiple JMX calls must be made through individual MBeans.
If you want to retrieve PMI data with an individual MBean, see Use individual MBeans.
Note: Read the Code example disclaimer for important legal information.
Use PerfMBean
After the performance monitoring service is enabled and the application server is started or restarted, a PerfMBean is located in each application server. This PerfMBean provides access to PMI data. To use PerfMBean, follow these steps:
Create an instance of AdminClient.
When you use the AdminClient API, you must pass the host name, port number and connector type. The example code is:
AdminClient ac = null; java.util.Properties props = new java.util.Properties(); props.put(AdminClient.CONNECTOR_TYPE, connector); props.put(AdminClient.CONNECTOR_HOST, host); props.put(AdminClient.CONNECTOR_PORT, port); try { ac = AdminClientFactory.createAdminClient(props); } catch(Exception ex) { failed = true; new AdminException(ex).printStackTrace(); System.out.println("getAdminClient: exception"); }
Use AdminClient to query the MBean ObjectNames.
After you get the AdminClient instance, you can call queryNames to get a list of MBean ObjectNames based on a query string. To get all ObjectNames, you can use the following example code. If you specify a query string, you get a subset of ObjectNames.
javax.management.ObjectName on = new javax.management.ObjectName("WebSphere:*"); Set objectNameSet= ac.queryNames(on, null); // you can check properties like type, name, and process to find a specified ObjectName
After you get all the ObjectNames, you can use this example code to get all of the node names:
HashSet nodeSet = new HashSet(); for(Iterator i = objectNameSet.iterator(); i.hasNext(); on = (ObjectName)i.next()) { String type = on.getKeyProperty("type"); if(type != null && type.equals("Server")) { nodeSet.add(servers[i].getKeyProperty("node")); } }
Note: This step returns only the nodes that are running.
To list running servers on the node, you can either check the node name and type for all ObjectNames or use the following example code:
StringBuffer oNameQuery= new StringBuffer(41); oNameQuery.append("WebSphere:*"); oNameQuery.append(",type=").append("Server"); oNameQuery.append(",node=").append(mynode); oSet= ac.queryNames(new ObjectName(oNameQuery.toString()), null); Iterator i = objectNameSet.iterator (); while (i.hasNext ()) { on=(objectName) i.next(); String process= on[i].getKeyProperty("process"); serversArrayList.add(process); }
Get the PerfMBean ObjectName for the application server from which you want to get PMI data.
Use this example code:
for(Iterator i = objectNameSet.iterator(); i.hasNext(); on = (ObjectName)i.next()) { // First make sure the node name and server name is what you want // Second, check if the type is Perf String type = on.getKeyProperty("type"); String node = on.getKeyProperty("node"); String process= on.getKeyProperty("process"); if (type.equals("Perf") && node.equals(mynode) & & server.equals(myserver)) { perfOName = on; } }
Invoke operations on PerfMBean through the AdminClient.
After you get one or more PerfMBeans in the application server from which you want to get PMI data, you can use the AdminClient API to invoke these operations on PerfMBean:
- setInstrumentationLevel: set the instrmentation level params[0] = new MBeanLevelSpec(objectName, optionalSD, level); params[1] = new Boolean(true); signature= new String[]{ "com.ibm.websphere.pmi.stat.MBeanLevelSpec", "java.lang.Boolean"}; ac.invoke(perfOName, "setInstrumentationLevel", params, signature); - getInstrumentationLevel: get the instrumentation level Object[] params = new Object[2]; params[0] = new MBeanStatDescriptor(objectName, optionalSD); 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); - getConfigs: get PMI static config info for all the MBeans configs = (PmiModuleConfig[])ac.invoke(perfOName, "getConfigs", null, null); - getConfig: get PMI static config info for a specific MBean ObjectName[] params = {objectName}; String[] signature= { "javax.management.ObjectName" }; config = (PmiModuleConfig)ac.invoke(perfOName, "getConfig", params, signature); - getStatsObject: you can use either ObjectName or MBeanStatDescriptor Object[] params = new Object[2]; params[0] = objectName; // either ObjectName or or MBeanStatDescriptor params[1] = new Boolean(recursive); String[] signature = new String[] { "javax.management.ObjectName", "java.lang.Boolean"}; Stats stats = (Stats)ac.invoke(perfOName, "getStatsObject", params, signature); - getStatsArray: you can use either ObjectName or MBeanStatDescriptor ObjectName[] onames = new ObjectName[]{objectName1, objectName2}; 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); - listStatMembers: navigate the PMI module trees Object[] params = new Object[]{mName}; String[] signature= new String[]{"javax.management.ObjectName"}; MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke(perfOName, "listStatMembers", params, signature); or, Object[] params = new Object[]{mbeanSD}; String[] signature= new String[] {"com.ibm.websphere.pmi.stat.MBeanStatDescriptor"}; MBeanStatDescriptor[] msds = (MBeanStatDescriptor[])ac.invoke (perfOName, "listStatMembers", params, signature);
Note: For getStatsObject and getStatsArray, the returned data only have dynamic information (value and time stamp). For sample code that links the configuration information with the returned data, see Example: JMX administrative client that retrieves PMI data.
To use an individual MBean, follow these steps: