使用 JMX 接口开发自己的监视应用程序
通过使用 PerfMBean 或个别 MBean,您可使用 AdminClient API 获取性能监控基础结构 (PMI) 数据。
开始之前
个别 MBean 提供您能够从中获取 PMI 数据的 Stats 属性。PerfMBean 向 PMI 管理提供扩展的方法以及访问 PMI 数据更为有效的方法。要设置 PMI 模块检测级别,您必须调用 PerfMBean 上的方法。 要从多个 MBean 查询 PMI 数据,调用 PerfMBean 中的 getStatsArray 方法比从多个个别 MBean 中获取 Stats 属性更快。Perf MBean 可以使用单个 JMX 调用提供来自多个 MBean 的 PMI 数据,但是必须通过个别 MBean 进行多个 JMX 调用。
有关 AdminClient JMX 的更多信息,请参阅主题“开发管理客户机程序”。
关于此任务
过程
- 创建 AdminClient 的实例。 当使用 AdminClient API 时,您需要首先通过传递主机名、端口号和连接器类型创建 AdminClient 的实例。示例代码为:
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"); }
- 使用 AdminClient 查询 MBean ObjectName。 在您获取 AdminClient 实例之后,您可根据您的查询字符串,调用 queryNames 以获取 MBean ObjectName 的列表。要获取所有 ObjectName,您可使用下列示例代码。如果您具有已指定的查询字符串,您将获取 ObjectName 的子集。
在您获取所有 ObjectName 后,您可使用下列示例代码获取所有节点名:javax.management.ObjectName on = new javax.management.ObjectName("WebSphere:*"); Set objectNameSet= ac.queryNames(on, null); // Check properties like type, name, and process to find a specified ObjectName
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")); } }
注: 这将仅返回已启动的节点。要列出节点上正在运行的服务器,您可检查节点名并为所有 ObjectName 输入或使用下列示例代码: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); }
- 为您要从中获取 PMI 数据的应用程序服务器获取 PerfMBean ObjectName。 使用此示例代码:
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; } }
- 通过 AdminClient 在 PerfMBean 上调用操作。 在要从中获取 PMI 数据的应用程序服务器中获取 PerfMBean 后,您可通过 AdminClient API 在 PerfMBean 上调用下列操作:
// setStatisticSet: Enable PMI data using the pre-defined statistic sets. Object[] params = new Object[] { com.ibm.websphere.pmi.stat.StatConstants.STATISTIC_SET_EXTENDED}; String[] signature = new String[] {"java.lang.String"}; ac.invoke (perfOName, "setStatisticSet", params, signature); // getStatisticSet: Returns the current statistic set. String setname = (String) ac.invoke (perfOName, "getStatisticSet", null, null); // setCustomSetString: Customizing PMI data that is enabled using fine-grained control. // This method allows to enable or disable statistics selectively. The format of the custom // set specification string is STATS_NAME=ID1,ID2,ID3 seperated by ':', where STATS_NAME //and IDs are defined in WS*Stat interfaces in com.ibm.websphere.pmi.stat package. params[0] = new String (WSJVMStats.NAME + "=" + WSJVMStats.HeapSize); params[1] = new Boolean (false); signature = new String[] {"java.lang.String", "java.lang.Boolean"}; ac.invoke (perfOName, "setCustomSetString", params, signature); // Note: Statistics that are not listed in the set string are not changed. // getCustomSetString: Returns the current custom set specification as a string String setstring = (String) ac.invoke (perfOName, "getCustomSetString", null, null); // setInstrumentationLevel: set the instrumentation level params[0] = new MBeanLevelSpec(objectName, new int[]{WSJVMStats.HEAPSIZE}); 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 params[0] = objectName; params[1] = new Boolean(recursive); String[] signature= new String[]{ "javax.management.ObjectName", "java.lang.Boolean"}; MBeanLevelSpec[] mlss = (MBeanLevelSpec[])ac.invoke(perfOName, "getInstrumentationLevel", params, signature); // setInstrumentationLevel: set the instrumentation level (deprecated in V6.0) 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 (deprecated in V6.0) 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); // Note: The returned data only have dynamic information (value and time stamp). // See PmiJmxTest.java for additional code to link the configuration information with the // returned data. // 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); // Note: The returned data only have dynamic information (value and time stamp). // See PmiJmxTest.java for additional code to link the configuration information with the // returned data. // 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); // Refer to the API documentation for deprecated classes
- 要使用个别 MBean:您需要获取 AdminClient 实例和个别 MBean 的 ObjectName。然后,您能够简单地获取 MBean 上的 Stats 属性。


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tprf_pmijmx
文件名:tprf_pmijmx.html