使用定制 MBean 扩展 WebSphere Application Server 管理系统

可以提供和注册新的 Java™ 管理扩展 (JMX) MBean(请参阅 JMX 1.x 规范获取详细信息)在 WebSphere 的一个进程中扩展 WebSphere® Application Server 管理系统。

关于此任务

JMX MBean 代表特殊逻辑段的管理接口。标准产品基础结构中的所有受管资源都以 JMX MBeans 表示。有很多方法可以创建您自已的 MBean,并且可以将他们注册到以任何 WebSphere 进程方式运行的 JMX MBeanServer。有关更多信息,请参阅 MBean Java 应用程序编程接口 (API) 文档。 在此信息中心中,单击引用 > MBean 接口

过程

  1. 创建定制 JMX MBean。

    创建 MBean 以扩展产品管理系统时会有一些备用选项以进行选择。可以使用另一个应用程序的任何现有 JMX MBean。在产品进程中,您可以注册 WebSphere Application Server 环境以外的 JMX MBean 服务器中测试的任何 MBean,包括标准 MBean、动态 MBean、开放式 MBean 和模型 MBean。

    除任何现有 JMX MBean 以及已编写并在产品环境外部经过测试的 MBean 之外,您还可以使用 WebSphere 提供的特殊分布式扩展并创建 WebSphere ExtensionMBean 提供程序。 此替代项能够与产品管理系统的所有分布式功能更好地集成。ExtensionMBean 提供程序表明您提供了特定 XML 文件,该文件包含基于 DTD(与本产品一起提供)的 MBean 描述符。此描述符将 MBean 支持的所有属性、操作以及通知告知 WebSphere 系统。通过这些信息,WebSphere 系统可以将远程请求路由至 MBean 并注册远程侦听器,以接收 MBean 事件通知。

    所有内部 WebSphere MBean 都遵循模型 MBean 的模式。纯 Java 类会提供管理功能的实际逻辑;WebSphere MBeanFactory 类会从 XML MBean 描述符读取这些功能的描述并创建与该描述符匹配的 ModelMBean 的实例。此 ModelMBean 实例绑定至 Java 类并使用正在该类的进程中运行的 MBeanServer 进行注册。现在,您的 Java 代码可通过创建并注册的表示该代码的 ModelMBean 从任何 WebSphere Application Server 管理客户机进行调用。

    [z/OS]WebSphere Application Server 分布式平台和 WebSphere Application Server for z/OS® 平台上运行的用户 MBean 可能要求特殊编码功能能够在 z/OS 多进程模型中正常运行。在每个应用程序服务器运行于单个 Java 虚拟机 (JVM) 中的分布式平台上,仅有一个 MBean 服务器。此 MBean Server 控制所有在该应用程序服务器内注册的 MBean。在 z/OS 平台上,有一个控制进程和多个服务方进程(每个进程都有他们自己的 MBean Server)的一个联合。控制流程有其自己的 MBean 代理服务器,用来在服务方流程间分布请求。请参阅“JMX MBean 多处理模型请求流”的详细讨论。

  2. 可选择定义显式 MBean 安全策略。

    如果没有定义 MBean 安全策略,那么该产品会使用缺省安全策略

  3. 注册新的 MBean。 有多种方法可以注册 MBean。

    可以向 WebSphere Application Server 管理服务注册您的 MBean。

    可以在 WebSphere Application Server 进程中向 MBeanServer 注册您的 MBean。以下列表描述了按照优先顺序来排列的可用的选项:
    • 通过 MBeanFactory 类。如果您希望实现与 WebSphere Application Server 系统的最大可能的集成,那么通过 MBeanFactory 类的 activateMBean 和 deactivateMBean 方法,使用 MBeanFactory 类来管理 MBean 的生命周期。使用这些方法,需要提供 RuntimeCollaborator 抽象超类的子类和 XML MBean 描述符文件。通过使用此方法,可以提供纯 Java 类,以便实现 MBean 描述符中定义的管理接口。MBeanFactory 类会创建实际的 ModelMBean 并代表您向产品管理系统注册该 ModelMBean。

      建议将此选项用于注册模型 MBean。

    • 使用 JMXManageable 和 CustomService 接口。可以通过实现 CustomService 接口(也会实现 JMXManageable 接口),使与 WebSphere 管理的集成更加容易。使用此方法,您可以避免提供 RuntimeCollaborator。初始化 CustomService 接口时,WebSphere MBeanFactory 类会读取 XML MBean 描述符文件并自动对 CustomService 接口创建、绑定以及注册 MBean。调用 CustomService 的关闭方法后,产品系统会自动取消激活 MBean。
    • 通过 AdminService 接口。您可以在 AdminService 接口上调用 registerMBean() 方法,并且在相应的安全检查后,该调用将委派给进程的底层 MBeanServer。您可以使用 AdminServiceFactory 类的 getAdminService() 方法获取对 AdminService 的引用。

      建议将此选项用于注册标准、动态和开放式 MBean。实现 UserCollaborator 类以使用 MBean 并为他们提供跨分布式及 z/OS 平台的一致级别支持。

      [z/OS]对于 z/OS 平台,在 AdminService 接口上通过 registerMBean() 方法注册的 MBean 在服务器外部不可视,且仅可从注册它的服务方进程进行调用。

    • 直接获取 MBeanServer 实例。可以通过调用 MBeanFactory 类的 getMBeanServer() 方法,获取在任何产品进程中运行的 JMX MBeanServer 实例的直接引用。您可以通过调用 AdminService 接口的 getMBeanFactory() 方法获取对 MBeanFactory 类的引用。

      直接向 MBean Server 注册定制 MBean 时,在缺省情况下,将使用单元名、节点和进程名关键字来增强 MBean 对象名。此注册允许 MBean 参与管理系统的分布式功能。可以通过设置 com.ibm.websphere.mbeans.disableRouting 定制属性来关闭该缺省行为。

      请参阅“管理服务定制属性”,以获取有关如何设置和使用 com.ibm.websphere.mbeans.disableRouting 定制属性的更多信息。

结果

不管用于创建并注册 MBean 的方法如何,您必须为新 MBean 代码设置适当的 Java 2 安全许可权。使用 Java 2 安全许可权可以严格保护 WebSphere AdminService 和 MBeanServer,且如果您没有将基本许可权显式授予代码,那么尝试调用这些类的方法时,系统会抛出安全性异常。如果您正在提供您的 MBean 作为应用程序的一部分,那么可以在作为一部分应用程序元数据提供的 was.policy 文件中设置许可权。如果您正在使用 CustomService 接口或其他不作为应用程序传递的代码,那么您可以编辑节点配置中的 library.policy 文件,或者甚至为特定安装编辑 properties 目录中的 server.policy 文件。

[z/OS]

示例:SampleStateMBean MBean

MBeanDescriptor

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MBean SYSTEM "MbeanDescriptor.dtd">
<MBean type="SampleStateMBean"
  aggregationHandlerClass="com.ibm.ws390.sample.SampleStateAggregationHandler"
  eventHandlerClass="com.ibm.ws390.sample.SampleStateEventHandler"
  invocationHandlerClass="com.ibm.ws390.sample.SampleStateInvocationHandler"
  stateObjectClass="com.ibm.ws390.sample.SampleState"
  version="6.0"
  platform="dynamicproxy"
  description="Sample State MBean for the documentation example.">

  <attribute description="The name of the MBean."
    getMethod="getMBeanName" name="mbeanName" type="java.lang.String"
    proxyInvokeType="unicall"/>
  <attribute description="The state of the MBean." name="state"
    getMethod="getState" setMethod="setState" type="java.lang.String"
    proxyInvokeType="multicall" proxySetterInvokeType="multicall"/>
  <operation
    description="Initialize the State MBean."
    impact="ACTION" name="initializeState" role="operation"
    targetObjectType="objectReference" type="void" proxyInvokeType="multicall">
    <signature>
      <parameter description="The name of the MBean."
        name="mbeanName" type="java.lang.String"/>
      <parameter description="The initial state of the MBean."
        name="mbeanName" type="java.lang.String"/>
    </signature>
  </operation>
  
  <notification name="j2ee.state.starting" severity="6" log="false"
                description="This sample state MBean is in starting state.">
      <notificationType>j2ee.state.starting</notificationType>
  </notification>
  <notification name="j2ee.state.running" severity="6" log="false"
                description="This sample state MBean is in running state.">
      <notificationType>j2ee.state.running</notificationType>
  </notification>
  <notification name="j2ee.state.stopping" severity="6" log="false"
                description="This sample state MBean is in stopping state.">
      <notificationType>j2ee.state.stopping</notificationType>
  </notification>
  <notification name="j2ee.state.stopped" severity="6" log="false"
                description="This sample state MBean is in stopped state.">
      <notificationType>j2ee.state.stopped</notificationType>
  </notification>    
</MBean>

SampleState 实现

package com.ibm.ws390.sample;

import com.ibm.ejs.ras.Tr;
import com.ibm.ejs.ras.TraceComponent;
import java.io.Serializable;
import com.ibm.websphere.management.dynamicproxy.StateObject;

public class SampleState extends StateObject {
    
    private static TraceComponent tc =
    Tr.register(SampleState.class,"SampleState",null);
    
    // Package protected STATE constants.
    static final String STATE_STARTING = "j2ee.state.starting";
    static final String STATE_RUNNING  = "j2ee.state.running";
    static final String STATE_STOPPING = "j2ee.state.stopping";
    static final String STATE_STOPPED  = "j2ee.state.stopped";
    
    // Dynamicproxy State is initialized with STOPPED state.
    private String state = STATE_STOPPED;
    
    public SampleState() {
        if (tc.isEntryEnabled()) Tr.entry(tc,"<init>");
        
        // State is initialized during "state" initialization,
        // but can also be initialized here in the constructor as well.
        /*
        state = "WebSphere Application Server for z/OS ready for e-business";
        */
        
        if (tc.isEntryEnabled()) Tr.exit(tc,"<init>");
    }
    
    public synchronized String getState() {
        if (tc.isEntryEnabled()) Tr.entry(tc,"getState");
        if (tc.isEntryEnabled()) Tr.exit(tc,"getState",state);
        return state;
    }
    
    public synchronized void setState(String state) {
        if (tc.isEntryEnabled()) Tr.entry(tc,"setState",state);
        this.state = state;
        if (tc.isEntryEnabled()) Tr.exit(tc,"setState");
    }
    
    public synchronized String getStateObjectInfo() {
        return state;
    }
}

SampleStateAggregationHandler 实现

package com.ibm.ws390.sample;

import com.ibm.websphere.management.dynamicproxy.AggregationHandler;
import com.ibm.websphere.management.dynamicproxy.StateObject;

import com.ibm.ejs.ras.Tr;
import com.ibm.ejs.ras.TraceComponent;

public class SampleStateAggregationHandler implements AggregationHandler {
    
    private static TraceComponent tc =
    Tr.register(SampleStateAggregationHandler.class,"SampleState",null);
    
    /**
     * Return an aggregated result from a multicall Mbean operation which
     * compiles through all servant MBeans' results and returns a respective
     * single return value for an invoked method.
     *
     * @param  methodName           MBean method name
     * @param  params               MBean method parameters
     * @param  signatures           MBean method signatures
     * @param  servantMBeanResults  Result of each servant MBean instances
     *                              invoked by the dynamicproxy multicast 
     *                              invocation.
     *                              Note: this value can be "null" OR can be
     *                              an array of "null"s in case return value
     *                              of the method is "void." Implementation
     *                              of this method MUST handle this case to
     *                              avoid a <code>NullPointerException</code>.
     * @param stateObject
     *        MBean provider provided <code>StateObject</code> used by 
     *        dynamicproxy MBean in CR to manage its state. Note: this object
     *        MAY BE null if "stateObjectClass" was not specified OR internal
     *        error occurred during initialization of this dynamicproxy MBean.
     *        Implmentation MUST properly handle "null" input.
     *                
     * @return aggregated result as defined by MBean xml for specified
     *         MBean operation.
     */
    public Object aggregateResults(String methodName,
                                   Object[] params,
                                   String[] signatures,
                                   Object[] servantMBeanResults,
                                   StateObject stateObject) {
                                       
        if (tc.isEntryEnabled()) Tr.entry(tc,"aggregateResults",methodName);
        
        // As you can see from the MBeanDescriptor of SampleStateMBean,
        // it declares the following four methods:
        // 1. String getMBeanName()         [proxyInvokeType == unicall]
        // 2. String getState()             [proxyInvokeType == multicall]
        // 3. void setState(String)         [proxyInvokeType == multicall]
        // 4. void initializeState()        [proxyInvokeType == multicall]
        //
        // From these methods, the only one that requires aggregation
        // is #2 getState method which is a multicall MBean operation AND
        // it returns a value that can be aggregated.
        //
        // In this example, we simply take each servants' getState MBean
        // request result and concatenate them into one long String that
        // displays each servants' state. 
        if (methodName.equals("getState")) {
            StringBuffer stateBuf = new StringBuffer();
            
            for (int i=0; i<servantMBeanResults.length; i++) {
                stateBuf.append("SERVANT #" + i + " state ==|" + 
                                servantMBeanResults[i] + "|== ");
            }
            return stateBuf.toString();
        }
        // If we also had an example method which returns say an int, 
        // getNumberOfMBeans(), it can take the similar approach
        // and to add each servants' getNumberOfMBeans() result together here.
        /* example added for non-existent method: int getNumberOfMBeans()
        else if (methodName.equals("getNumberOfMBeans")) {
            int aggregatedResult = 0;
            
            for (int i=0; i<servantMBeanResults.length; i++) {
                aggregatedResult += (int) servantMBeanResults[i];
            }
            return aggregatedResult;
        }
        */
        
        return methodName + " is NOT handled by " + getClass().getName() + "!";
    }
    
}

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tjmx_extend
文件名:tjmx_extend.html