技术开发 频道

使用JMX 接口来编写PMI 应用程序

 使用 PerfMBean 从单个 MBean 获取性能数据

  以下的代码返回 MBean 中的 Stats 对象。由于在这里只传递一个 MBean,所以它等价于直接从 MBean获取 Stats 属性。

1 Object[] params = new Object[]{myObjectName,new Boolean(false)};
2   // set true to return data in the submodules
3 string[] signature= new String[]{"javax.management.ObjectName""java.lang.Boolean"};
4 Stats myStats =  (Stats)ac.invoke(perfOn,"getStatsObject",params,signature);
5                 

  在上面的代码中,将 PMI 的 Boolean 参数设置为 true 就可返回那个 MBean 及其子模块中的所有 PMI 数据。例如,如果您为某一 Web 应用程序查询数据并将 Boolean 参数设置为 true ,那么 PMI 数据就会返回给该 Web 应用程序以及它下面的所有 servlet。在其他 API 中也可以将 Boolean 参数设置为 true 来获取 PMI 数据。

  使用 PerfMBean 从多个 MBean 获取性能数据

  以下代码返回多个 MBean 的 Stats对象。这往往比单独获取数据来得有效。

1 ObjectName[] myOnArray;
2   // myOnArray is the array of all the Mbeans you want to get PMI data
3 Object[] params = new Object[]{myOnArray,new Boolean(false)};
4   // set true to return data recursively
5 String[] signature= new String[]{"[Ljavax.management.ObjectName;""java.lang.Boolean"}; Stats[] myStatsArray = (Stats[])ac.invoke(perfOn,"getStatsArray",params,signature);

  从没有 MBean 的 PMI 模块/子模块获取性能数据

  调用 getStatsObject 方法可以访问在服务器中没有匹配的MBean 的 PMI 模块和子模块。例如,PMI 模块“threadPoolModule”是一个逻辑组,它包含所有线程池聚集的 PMI数据。然而,并没有用于这一逻辑组运行时 MBean。在本例中,对于 PMI 模块/子模块,我们构建了一个 MBeanStatDescriptor,它包含了一个 MBean ObjectName和一个 StatDescriptor。其中 ObjectName 标识一个 MBean,而 StatDescriptor 指明 PMI 树中的 MBean的相关路径。MBeanStatDescriptor 比 MBean 提供更好的粒度的 PMI 访问。在步骤 3 中也使用 ObjectName 和 StatDescriptor来构建 MBeanLevelSpec。

  以下的代码包含了 threadPoolModule 的 PMI 数据。服务器 ObjectName 表明它是从以该服务器为根的 PMI 数据开始的,并且 StatDescriptor 中的相关路径是来自该根的 threadPoolModule。

1 ObjectName serverON;
2   //  this is the ObjectName for the server you want to query PMI data
3 StatDescriptor mysd = new StatDescriptor(new String[]{PmiConstants.THREADPOOL_MODULE});
4 MBeanStatDescriptor mymsd = new MBeanStatDescriptor(serverON,mysd);
5 Object[] params = new Object[]{mymsd,new Boolean(false)};  
6 String[] signature= new String[]{"com.ibm.websphere.pmi.stat.MBeanStatDescriptor",
7    "java.lang.Boolean"};
8 Stats myStats = (Stats)ac.invoke(perfOn,"getStatsObject",params,signature);
9                 

  如果您知道 ObjectName 和 StatDescriptor,就可直接构造一个 MBeanStatDescriptor。否则您需要通过 PerfMBean中的 listStatMembers 方法来找出所有的 MBeanStatDescriptor。listStatMembers(ObjectName)和 listStatMembers(MBeanStatDescriptor)方法返回 PMI 树一级子节点的一组 MBeanStatDescriptor。因此,递归调用 listStatMembers方法就可以得到所有的 MBeanStatDescriptor,从而找到您期望得到的那一个。请注意 StatDescriptor 必须为空;这样的话 MBeanStatDescriptor 就提供了与它的 ObjectName 一样的 PMI 数据。

  从多个 MBeanStatDescriptor 获取性能数据

  这种方法通过一次 JMX 调用就可返回多个 MBeanStatDescriptor 的一批 Stats 对象,它通常比多次 JMX调用来得有效。以下的代码显示了如何使用这种方法:

1 MBeanStatDescriptor[] myMsdArray;
2   // myMsdArray is the array of MBeanStatDescriptors you want to get PMI data
3 Object[] params = new Object[]{myMsdArray,new Boolean(false)};
4   // set true to return data recursively
5 String[] signature= new String[]{"[Lcom.ibm.websphere.pmi.stat.MBeanStatDescriptor;",
6   "java.lang.Boolean"};
7 Stats[] myStatsArray = (Stats[])ac.invoke(perfOn,"getStatsArray",params,signature);
8

  在步骤 5 中,我们用不同的方式来调用 PerfMBean 中的 getStatsObject和 getStatsArray 以获取期望的 PMI 数据。使用 ObjectName 或者 MBeanStatDescriptor,您就可检索 PMI数据而不必考虑是否存在作为该数据的统计提供者的 MBean。在所有的 getStatsXXX 方法中,给 Boolean 参数传递 true 或 false 表明是否要为 PMI 提供整棵的子树。

  步骤 6:检索静态配置信息

  为了降低通信成本,在步骤 4 和 5 中检索PMI 数据时 PMI 并不返回静态配置信息(如数据名称和描述)。然而,您可以单独得到静态信息,并且在需要时将它与 PMI数据绑定。请使用以下的代码来从服务器获取所有的配置信息,查找某一 MBean 的 PmiModuleConfig,并将配置数据和 Stats数据绑定在一块。请将 PmiModuleConfigs 高速缓存,这样就不需要每次都启动远程调用了。

1 // get all the configs in one call and cache it in your client program
2 PmiModuleConfig[] configs = (PmiModuleConfig[])ac.invoke(perfOn,
3 "getConfigs"nullnull);
4 PmiModuleConfig myConfig = PmiClient.findConfig(configs,
5 myMBeanObjectName);
6 myStats.setConfig(myConfig); // this is an API extended by PMI
7

  步骤 7:从Stats 对象获取各种 PMI 计数器

  步骤 4 和 5 当使用单个 MBean 或 PerfMBean 时返回了作为Stats 对象的 PMI。一个 Stats 对象包含一列实现不同数据类型(如表 1 所示)的 PMI 数据。Stats对象在以递归模式查询数据时也可以选择性地包括 Stats 对象列表。以下样本代码显示如何从 Stats 对象获取各种 PMI 计数器。请注意 WebSphereApplication Server V5将 J2EE 性能数据框架的接口打包在 com.ibm.websphere.management.statistics 中。以下的代码使用这个包中的接口。在样本 下载代码中,我们使用 com.ibm.websphere.pmi.stat 包中的 WebSphere 扩展接口来提供其他方法,如获取 sub-stat。

1 // get all the data from the Stats object        
2 Statistic[] dataMembers = stat.getStatistics();
3 if (dataMembers != null)  {
4 forint i=0; i<dataMembers.length; i++) {
5 // For each data,cast it to be the real Statistic type so that we can get the value from each Statistic.
6 // Can call the following common methods without knowing the data type:    
7 // getName(),getDescription(),getStartTime(),getLastSampleTime()
8                   
9 // check the data type and cast the data to the right type
10 if (dataMembers[i] instanceof CountStatistic) {
11   // cast it to CountStatistic and call the API to get the data value
12 } else if (dataMembers[i] instanceof TimeStatistic) {
13   // cast it to TimeStatistic and call the API to get the data value
14 } else if (dataMembers[i] instanceof RangeStatistic) {
15   // cast it to RangeStatistic and call the API to get the data value
16 }
17    }
18 }
19

  结束语

  本文介绍了 PMI、JMX MBean 和 J2EE性能数据框架之间的关系。PMI 实现了 J2EE 性能数据框架并允许通过 JMX 接口进行数据访问。PerfMBean 通过程序为每个 PMI模块设置装备级别。一旦 PMI 数据可用,这种数据就可通过各种 MBean 或 PerfMBean 获取。使用 MBeanStatDescriptor来从在服务器中没有映射的 MBean 的 PMI 模块/子模块获取 PMI 数据。从多个 MBean 获取 PMI 数据的一种更有效的方法是使用 PerfMBean来产生单个 JMX 调用,而不是为各个 MBean 产生多个 JMX 调用。检索静态配置信息及导航 Stats 对象的 API 也同样可用。

  PMI 数据有助于实现性能监控和调优。例如,WebSphere Application Server V5.02 附带的性能顾问监控 PMI数据并通过调整应用服务器配置给出性能优化的建议。基于每台服务器中的性能数据,使用 PMI 的监控工具有助于进行工作负载管理、容量规划并通过检测相关PMI 计数器的值来生成警告。

        相关内容下载:pmi_jmx.zip

0
相关文章