OC4J 10g (10.1.3) 方法文档:创建简单的 JMX MBean

最初撰稿时间:2005 年 1 月 12 日
更新时间:
日期:2006 年 8 月 8 日 19:53:41 -0700(2006 年 8 月 8 日,星期二)
作者:sbutton

目录

更改跟踪

日期

更改人

更改内容

2005 年 1 月 12 日

sbutton

初始创建和发布 OC4J 10g (10.1.3) 开发人员预览版 3

2005 年 7 月 7 日

sbutton

更新为使用 Application Server Control 中新增的 JMX 应用程序 MBean 浏览器功能。使用新的 ant 任务进行部署。

2006 年 1 月 10 日

sbutton

更新了生产;更改为使用在 ant-oracle.xml 中定义属性的新服务器部署到任何类型的 OC4J 目标上。

大纲

这组方法示例着重介绍在部署到 OC4J 实例的应用程序中如何使用 JMX 和管理 Bean (MBean)。

OC4J 10g (10.1.3) 包含一个 JMX 1.2 服务器实现,从而允许 OC4J 实例化和执行 MBean。

OC4J 10g (10.1.3) 实现了 J2EE 管理规范 (JSR77),该规范为 J2EE 容器定义了一组标准的受管理对象,从而可以用标准的方式表示和管理该容器。

OC4J10g (10.1.3) 实现了 J2EE 部署 API 规范 (JSR88),该规范提供了一个标准的 API,用于在 J2EE 容器上执行部署操作。

在该方法文档中,您将构建一组 MBean 并将它们部署到一个 OC4J 10g (10.1.3) 实例上,并使用 Application Server Control(OC4J 随附的)查看和操作这些 MBean。

该方法示例已经在以前版本的基础上进行了更新,以说明如何使用 Application Server Control 通过应用程序 Bean 浏览器来访问 MBean。

前提条件

要构建、部署并运行这些示例,您的计算机上必须安装有以下软件:

必需

  1. OC4J 10g (10.1.3) 开发人员预览版 4,可以从 http://otn.oracle.com/global/cn/software/products/ias/preview.html 下载
  2. Apache Ant,可以从 http://ant.apache.org/ 下载

OC4J、JMX 和 Application Server Control

OTN 上提供的 OC4J 10g (10.1.3) 开发人员预览版 4 实现了 J2EE 1.4 规范,并且认证为与 J2EE 1.4 兼容。

J2EE 1.4 前景的核心附加内容是一组规范,它们定义了一个标准机制来管理 J2EE 容器实现上的资源和应用程序,还定义了这些应用程序的部署方法。

Java 管理扩展 (JMX) 提供了一个 API 和若干服务,从而允许为 Java 应用程序创建标准的管理接口。应用程序可以使用 JMX 来提供自己的一组管理 Bean (MBean),这些管理 Bean 将公开一组用来控制该应用程序的管理操作,提供对该应用程序的配置设置的访问,甚至还允许该应用程序收集和报告统计信息。随着 JMX 标准的发展,MBeanServer 可以发现、实例化和访问应用程序自带的 MBean。MBeanServer 可以作为一个独立组件运行,也可以作为 J2EE 容器的一个嵌入服务运行。在 J2EE 容器中运行时,MBeanServer 可以有效地向 J2EE 容器的其他组件以及管理员公开 MBean 以执行管理操作。

J2EE 1.4 规范还要求实现 J2EE 管理规范。这将有效地要求 J2EE 供应商针对 J2EE 服务器本身的各个组件实现一组必需的 MBean,从而为 J2EE 容器提供一组标准化管理接口。

MBeanServer 可以通过许多不同的协议经由连接器和协议适配器提供对其 MBean 的访问。通过连接器,管理应用程序可以访问 MBeanServer 中运行的 MBean,并在其上执行操作。

由于引入了 JMX 和 J2EE 管理规范,J2EE 通过 J2EE 容器必须提供的一组标准 MBean 提供了一个非常强大的管理功能。它还允许应用程序通过 MBean 提供自己的管理功能,这些功能可以通过标准的管理机制公开。

OC4J 10g (10.1.3) 自带一个与 JMX 1.2 兼容的服务器实现,可以实现 J2EE 1.4 的 JMX 和 J2EE 管理要求。除了 J2EE 管理规范中列出的核心受管理对象组外,OC4J 已经进行了扩展,可以为该规范中未涵盖的其他方面提供 MBean。

Application Server Control

Application Server Control 是一个基于 Web 的新的管理控制台,现在随 OC4J 10g (10.1.3) 一起提供。

Application Server Control 提供了一个完整的管理接口,您可以使用它在运行的 OC4J 实例上执行管理和部署操作。为了支持 JMX,它提供了:

1. 系统 MBean 浏览器,允许管理员查看和操作 OC4J 本身提供的系统级 MBean。

2. 应用程序注册的 MBean 控制台,允许管理员查看和操作应用程序本身注册的 MBean。

Application Server Control 是一个 Web 应用程序,首次启动时由 OC4J 部署。

可以使用以下 URL 访问 Application Server Control: http://localhost:8888/em

首次访问 Application Server Control 时将看到登录屏幕。首次执行 OC4J 时,输入用户名 oc4jadmin 和针对 oc4jadmin 用户设置的口令。

经过认证之后,您将看到正在管理的 OC4J 实例的 Application Server Control 主页

 

系统 MBean 浏览器

Application Server Control 提供了系统 MBean 浏览器,可以访问 OC4J 中运行的 MBeanServer 并显示一组可用的系统 MBean。您可以单击树控件中的一个 MBean 来查看其属性、操作、通知以及统计信息,也可以发出查询来查找特定的 MBean。

在系统 MBean 浏览器中,可以查看和配置 OC4J 的大量操作方式。

 

应用程序定义的 MBean

在 Application Server Control 中可以访问应用程序在部署时提供的 MBean 以及为特定应用程序提供特定管理功能的 MBean。然而,这些类型的 MBean 在系统 MBean 浏览器中不显示。相反,它们显示在注册了它们的已部署应用程序的上下文中。

 

MBean 示例

该方法文档包含了大量 MBean 示例。所提供的 MBean 并不意味着本质上很复杂,它们实际上只是简单介绍了构建 MBean、配置 MBean 并将其部署到 OC4J 的入门知识,并初步探究了 JMX API 提供的下一级别的服务(如通知)。

MBean 类型

JMX 规范定义了 3 种主要的 MBean 类型:

StandardMBean:这些 MBean 最易于设计和实现,它们的管理接口由它们的方法名描述。

DynamicMBean:这些 MBean 必须实现特定的接口,但是它们在运行时公开自己的管理接口,因此具有最大的灵活性。

ModelMBean:这些也是动态 MBean,在运行时是完全可配置和自描述的;它们提供了一个通用的 MBean 类,具有动态使用资源的默认行为。

有关更多信息,请参阅 http://jcp.org/aboutJava/communityprocess/final/jsr003/index3.html 上的 JMX 1.2 规范

使用 OC4J 简单注册 MBean

要使 OC4J 可以实例化自定义的 MBean,您需要在运行时注册这些 MBean。使用静态或编程方法即可实现。

静态注册

对于静态注册,MBean 由 OC4J orion-application.xml 文件中的 <jmx-mbean> 标记定义。

<jmx-mbean objectname=":name=Simple" class="demo.oc4j.jmx.standard.SimpleStandard">
<description>A simple example of a StandardMBean</description>
</jmx-mbean>

可以在特定应用程序的基础上实现该操作,方法是:将 MBean 类包含在一个 EAR 文件中,创建一个 OC4J orion-application.xml 文件,指定要注册的 MBean,然后将这些包含在应用程序 EAR 文件中。部署应用程序之后,将在应用程序的命名空间中注册这些 MBean。

使用 EAR 文件打包 MBean

对于 OC4J,使用应用程序打包 MBean 的简便方法是将它们放在一个 JAR 文件中。将该 JAR 文件放在 EAR 文件中,然后向 OC4J orion-application.xml 文件添加一个 <library> 标记,这样应用程序就可以加载库。

对于 simplembeans.ear 文件,MBeans 打包在一个名为 simplembeans.jar 的 JAR 文件中,该文件位于 simplembeans.ear EAR 文件的根目录级别。

 

META-INF 目录中的 OC4J orion-application.xml 文件将 simplembeans.jar 指定为应用程序的一个库(并如前所述静态注册 MBean)。

<orion-application>

  <!-- Include the library which contains the MBean classes -->
  <library path="simplembeans.jar"/>

  <!-- Statically register the MBeans for this application -->
  <jmx-mbean objectname=":name=Simple" class="demo.oc4j.jmx.standard.SimpleStandard">
    <description>A simple example of a StandardMBean</description>
  </jmx-mbean>

  ...

</orion-application>

现在,OC4J 将能够找到 MBean 并用 MBeanServer 注册它们。

构建并部署示例 MBean

MBean 示例的源代码包含在该方法文档分发中。附带的 Ant 构建脚本具有多个目标,用于编译、打包和部署应用程序。您可以使用附带的 Ant 构建脚本从头开始重新构建 MBean。MBean 的部署可以通过 Ant 脚本或其他许多方法实现。

所有 MBean 都打包在一个名为 simplembeans.ear 的应用程序中。对该应用程序进行部署,使用 MBeanServer 将 MBean 注册在应用程序的命名空间中。

构建 MBean

要构建 MBean,请执行以下步骤:

  1. 将 ORACLE_HOME 环境变量设置为 OC4J 在系统上的安装目录。例如:

    >set ORACLE_HOME=d:\java\oc4j-1013
  2. 将 how-to-simplembeans.zip 文件解压缩到一个目录下。

    >jar xf how-to-simplembeans.zip
  3. 执行默认的 ant 目标,编译 MBean。

    >ant

    D:\myprojects\java\how-to-simplembeans>ant
    Buildfile:build.xml

    common:
    [echo] BuildName:how-to-simplembeans
    [echo] BuildHome:D:\myprojects\java\how-to-simplembeans
    [echo] BuildFile:D:\myprojects\java\how-to-simplembeans\build.xml
    [echo] BuildJVM: 1.4

    oracle-env-check:

    java-env-check:

    init:
    [echo] -----> Initializing project properties

    setup:
    [echo] -----> Creating the required sub-directories
    [mkdir] Created dir:D:\myprojects\java\how-to-simplembeans\lib
    [mkdir] Created dir:D:\myprojects\java\how-to-simplembeans\build
    [mkdir] Created dir:D:\myprojects\java\how-to-simplembeans\log
    [mkdir] Created dir:D:\myprojects\java\how-to-simplembeans\build\how-to-simplembeans
    [mkdir] Created dir:D:\myprojects\java\how-to-simplembeans\build\how-to-simplembeans\META-INF
    [mkdir] Created dir:D:\myprojects\java\how-to-simplembeans\build\simplembeans

    compile-mbeans:
    [javac] Compiling 11 source files to D:\myprojects\java\how-to-simplembeans\build\simplembeans

    package-mbeans:
    [jar] Building jar:D:\myprojects\java\how-to-simplembeans\build\how-to-simplembeans\simplembeans.jar

    ear-descriptor:
    [copy] Copying 2 files to D:\myprojects\java\how-to-simplembeans\build\how-to-simplembeans\META-INF

    package-app:
    [ear] Building ear:D:\myprojects\java\how-to-simplembeans\build\how-to-simplembeans.ear
    [copy] Copying 1 file to D:\myprojects\java\how-to-simplembeans\lib

    all:
    [echo] -----> Creates and deploys all deployable units for the application

    BUILD SUCCESSFUL
    Total time:6 seconds

这些步骤成功执行的结果是在 lib 目录中创建一个 simplembeans.ear 文件(包含 MBean)。

lib/simplembeans.ear

simplembeans.ear 文件是一个标准的 J2EE EAR 文件,包含 META-INF/application.xml 和 OC4J meta-inf/orion-application.xml 这两个供应用程序使用的文件。

创建了 simplembeans.ear 之后,就可以将它部署到 OC4J 实例上。实现方式有多种。

使用 Ant 部署 MBean

Ant 脚本中的部署目标将 EAR 文件部署到正在运行的 OC4J 实例。

  1. 验证 ant-oracle.properties 文件中定义的部署属性是否符合您的环境。

    # The HTTP listening port for the target OC4J instance
    # - stand alone mode default is 8888
    # - managed mode default is 7777
    oracleas.http.port=8888

    # The administration listening port for the target OC4J instance
    # This port reprensents:
    # - the ORMI port if you are running a Stand Alone OC4J instance (default:23791)
    # - the OPMN port if you are running OracleAS in a managed mode (default:6003)
    # Note about the OPMN port:
    # if you do not know what is the OPMN port of your OracleAS instance
    # you can get if using the following command:
    # $ORACLE_HOME/opmn/bin/opmnctl status -port
    oracleas.admin.port=23791

    # The administrative login name for the target OracleAS instance
    oracleas.admin.user=oc4jadmin

    # The administrative password for the target OracleAS instance
    oracleas.admin.password=welcome1

    # The web site to which the deployed web modules are bound
    oracleas.binding.module=default-web-site

    # The deployer.uri is the URI that ill be used to deploy the application.
    # The URI varies depending of the type of topology
    # 1. Stand Alone OC4J instance
    # 2. Single Managed Instance (OPMN without clustering)
    # 3. Managed Cluster (OPMN with multiple clustered nodes)
    # Uncomment the URI depending of your targetted configuration
    #
    # 1. Stand alone OC4J
    oracleas.deployer.uri=deployer:oc4j:${oracleas.host}:${oracleas.admin.port}

    # 2. Single managed instance
    #oracleas.deployer.uri=deployer:oc4j:opmn://${oracleas.host}:${oracleas.admin.port}/${oracleas.oc4j.instance}

    # 3. Cluster
    #oracleas.deployer.uri=deployer:cluster:opmn://${oracleas.host}:${oracleas.admin.port}/${oracleas.oc4j.instance}

    # If you are deploying to a Managed instance you must specify the OC4J instance name
    oracleas.oc4j.instance=home

  2. 在 Ant 脚本中执行部署目标。

    >ant deploy

    Buildfile:build.xml
    [taskdef] Could not load definitions from resource oracle/ant/taskdefs/deploy/
    antlib.xml. It could not be found.
    [taskdef] Could not load definitions from resource oracle/antlib.xml. It could
    not be found.

    common:
    [echo] BuildName:how-to-simplembeans
    [echo] BuildHome:D:\temp\1013prodhowto\how-to-simplembeans
    [echo] BuildFile:D:\temp\1013prodhowto\how-to-simplembeans\build.xml
    [echo] BuildJVM: 1.5

    oracle-env-check:

    java-env-check:

    init:
    [echo] -----> Initializing project properties

    setup:
    [echo] -----> Creating the required sub-directories

    compile-mbeans:

    package-mbeans:

    compile-web-classes:
    [echo] -----> Compile Web Classes

    web-descriptor:
    [echo] -----> Add OC4J descriptor

    package-web:

    ear-descriptor:

    package-app:

    deploy-ear:
    [echo] -----> Deploying the application module deployment (ear) file
    [echo] Target: deployer:oc4j:localhost:23791
    [oracle:deploy] Deploying application how-to-simplembeans.
    [oracle:deploy] Application deployer for how-to-simplembeans COMPLETES.

    deploy:
    [echo] -----> Deployed the application components required by OC4J for the
    execution of this application


    这将使用 OC4J 分发提供的部署 Ant 任务将 EAR 文件部署到属性文件中定义的 OC4J 实例。

使用 Application Server Control 进行部署(可选)

Application Server Control 提供了一组部署任务,可用于部署 J2EE 应用程序和模块。

1.

打开一个新的浏览器窗口。

输入以下 URL,启动 Application Server Control:

http://127.0.0.1:8888/em

注意:主机:端口值应该用于特定的 OC4J 实例。

2. 首次启动 OC4J 时,使用用户名 oc4jadmin 和针对 oc4jadmin 用户设置的口令登录。
 
3. 单击 Administrations 选项卡,然后单击 Deploy 按钮启动部署向导。
 
4. 在文件选择对话框中选择 lib/how-to-simplembeans.ear 文件,然后单击 Next 按钮。
 
5. Application Name 域中指定 how-to-simplembeans,然后单击 Next 按钮。
 
6. 单击 Deploy 按钮,继续部署操作。
 
7. 应用程序现在将部署到正在运行的 OC4J 实例上,部署窗口中会显示进度消息。
 
8. 单击 Return 按钮,退出部署向导。

深入了解 MBean

该方法文档提供了五个 MBean。尽管每个 MBean 都很简单,并且只触及了 JMX 功能的表面,但每个 MBean 都有些许不同。它们旨在充当代码试验品 — 它们易于理解,有助于您快速了解 JMX 是什么以及 MBean 的外观。

SimpleStandardMBean — 最简单的 MBean

该 Bean 的源代码位于 src/demo/oc4j/jmx/standard 目录下。

该 MBean 几乎是可创建的最简单的 MBean。如果这是一个 C 语言教程,该 MBean 将为“Hello World”。它是一个标准的 MBean,通过一个 Java 接口 (SimpleStandardMBean.java) 公开其属性和方法,其实现在 SimpleStandard 类中完成。该 MBean 通过 orion-application.xml 方法静态注册。

SimpleStandardMBean 维护一个计数器,提供用于递增、递减和重置计数器值的操作,并提供访问器来设置和获取计数器属性的当前值。

SimpleStandardMBean 可以为 MBean 定义管理接口。接口名必须以后缀“MBean”结尾。

package demo.oc4j.jmx.standard;

public interface SimpleStandardMBean
{
  public int getCount();
  public void setCount(int count);
  public int decrement();
  public int increment();
  public int reset();
}

SimpleStandard 可实现声明的接口,以提供管理属性和操作。它看上去像一个实现特定接口的标准 JavaBean。

package demo.oc4j.jmx.standard;

public class SimpleStandard implements SimpleStandardMBean
{
  private int _count = 0;

  public SimpleStandard()
  {
  }

  public int getCount()
  {
    return _count;
  }

  public void setCount(int count)
  {
    _count=count;
  }

  ...
}

orion-application.xml 包含一个用来静态注册该 SimpleStandard MBean 的条目。<jmx-mbean> 标记包含注册 MBean 时使用的属性,以及 MBean 的完全限定名。

<jmx-mbean objectname=":name=Simple" class="demo.oc4j.jmx.standard.SimpleStandard">
  <description>A simple example of a StandardMBean</description>
</jmx-mbean>

注意,注册代码中指定的是类本身而不是接口。

使用静态注册方法,可以在配置文件中指定 MBean 属性的初始值。这是通过 <jmx-mbean> 定义中的 <attribute><value> 标记组实现的。

<jmx-mbean objectname=":name=Simple" class="demo.oc4j.jmx.standard.SimpleStandard">
  <description>A simple example of a StandardMBean</description>
  <attribute name="Count">
    <value>3336</value>
  </attribute>
</jmx-mbean>

将应用程序部署到 OC4J 之后,Application Server Control 将显示 SimpleStandard MBean 并允许对其进行操作。它还将按照指定的方法设置任意属性的初始值。

SimpleStandard MBean 按照 jmx-mbean 标记中指定的那样使用“Simple”名注册,并部署为“simplembeans”应用程序的一部分。

在 Applications 选项卡上,针对 SimpleMBeans 应用程序单击 Application Defined MBeans 列中的链接。

 

这将在树控件中显示 SimpleMBeans 应用程序注册的所有 MBean。

单击名为 Simple 的 MBean。

Application Server Control 将公开该 MBean 的详细信息。它将列出该 MBean 的重要详细信息,如名称、类和说明。它还公开该 MBean 的属性(在本示例中为 Count),并列出操作名。您可以在 Application Server Control 中直接操作属性以及执行对 MBean 的操作。

 

显示所有属性(在本示例中为 Count)的当前值。对于那些具有读写操作的属性,在 Value 表单域中填入值并按 apply 按钮,即可设置属性的值。

 

选择 Operations 选项卡并单击所需的操作,即可查看操作列表并调用 MBean 上的操作。

 

在该示例中,单击“Invoke Operation”按钮将显示增量方法详细信息并调用该方法。这将执行 MBean 接口中定义的“increment”方法的底层实现。

 

单击 Return 按钮,返回 operations 选项卡。

现在,查看 Count 属性的值,您将发现该值已经在以前的基础上增加了 1。

 

这样,Application Server Control 就将一个简单的计数器 MBean 部署到 OC4J 上,并向要使用它并与之进行交互的管理员公开。

DynamicRegistration — 动态注册 MBean

该 Bean 的源代码位于 src/demo/oc4j/jmx/dynamicregistration 目录下。

该 MBean 演示了如何在 MBean 世界中以动态方式完成操作。在本示例中,我们将使用一个 MBean (DynamicRegistration) 来动态注册另一个使用管理员提供的名称的 MBean (Simple),然后调用其上的操作。

该示例包含一个标准的 MBean 接口(一个实现类)。它使用前面的示例 SimpleStandard 作为它在用户提供的名称下动态注册的 MBean。

DynamicRegistrationMBean 可以为 MBean 定义管理接口。它公开一个方法,以使用提供的名称来注册另一个 MBean,还公开了其他方法来递增和获取指定 MBean 的计数值。

package demo.oc4j.jmx.dynamic;

public interface DynamicRegistrationMBean
{
  public String registerNewSimpleMBean(String name);
  public void executeIncrement(String name);
  public void executeDecrement(String name);
  public int getCount(String name);
}

DynamicRegistration 实现了管理接口。它提供的实现负责动态注册 SimpleMBean,以及调用已注册 MBean 上的对应方法。

registerNewSimpleMBean 方法将一个新的 SimpleStandard 对象注册为一个 MBean,MBeanServer 将使用提供的名称。用其他名称多次调用该方法,可以注册多个 SimpleStandard MBean 实例。新的 MBean 将注册在默认应用程序的命名空间下,这是因为 Application Server Control 本身(实际上正在注册新的 MBean)作为默认应用程序的一部分运行。

public String registerNewSimpleMBean(String name)
{
  String ret = null;
  String beanName = null;

  beanName = getName(name);

  try
  {
    SimpleStandard bean = new SimpleStandard();
    ObjectName obj = new ObjectName(beanName);

    // dynamically register the SimpleStandard MBean
    ObjectInstance newobj = _mbeanServer.registerMBean(bean,obj);
    ret = "This seems to have worked, my name is " + newobj.getObjectName().getCanonicalName();
  }
  catch(Exception e)
  {
    e.printStackTrace();
  }
}

executeIncrement 方法(使用 MBeanServer 提供的 invoke(...) 操作注册的)在 SimpleStandard MBean 上调用增量方法,该方法有效地充当 SimpleStandard MBean 上的 invoke 方法周围的包装。它使用为方法提供的名称来查找要在其上执行操作的 MBean。如果您有多个在唯一名称下注册的 Bean 示例,则可以分别调用其上的增量方法。

javax.management.MBeanServer.invoke 方法功能强大,通过提供有关方法参数签名的详细信息并提供参数值,可以调用任意给定复杂性的方法。

public void executeIncrement(String name)
{
  try
  {
    ObjectName obj = new ObjectName(getName(name));
    _mbeanServer.invoke(obj,"increment",null,null);
  }
  catch(Exception e)
  {
  e.printStackTrace();
  }
}

getCount 方法使用 MBeanServer.getAttribute 方法获得所提供名称的 MBean 上 Count 属性的值。如果您有多个已注册的 SimpleStandard MBean 实例,可通过提供 Bean 的唯一名称来使用该方法获取 Count 的值。

/**
* Executes the getCount method on the bean that has been dynamically registered, using the
* MBeanServer.invoke(..) method.
* @return int - the count value
*/
public int getCount(String name)
{
  int ret = -999;
  try
  {
    ObjectName obj = new ObjectName(getName(name));
    Integer val =(Integer)_mbeanServer.getAttribute(obj,"Count");
    ret = val.intValue();
  }
  catch(Exception e)
  {
    e.printStackTrace();
  }
  return ret;
}

orion-application.xml 包含一个用来静态注册该 MBean 的条目。<jmx-mbean> 标记包含注册 MBean 时使用的属性,以及 MBean 的完全限定名。

<jmx-mbean objectname=":name=SimpleDynamicRegistration" class="demo.oc4j.jmx.dynamic.SimpleDynamicRegistration">
<description>An example of registering the simple bean dynamically using a supplied name</description>
</jmx-mbean>

该示例演示了 MBean 世界的动态功能,允许动态注册 MBean 并与之交互。

使用 Application Sever Control,您可以与 SimpleDynamicRegistration MBean 交互。

 

您可以注册一个新的 MBean(它最终是 SimpleStandard 类型),方法是:调用 registerNewSimpleMBean 方法,为新 MBean 提供名称,然后单击 Invoke Operation 按钮。

该操作的返回值将显示用于注册新的 MBean 对象的名称。

 

应用程序 MBean 浏览器现在将显示使用用户提供的名称注册的新 MBean。

 

使用 SimpleDynamicRegistration MBean 公开的方法,您可以获得 Count 属性的当前值并执行 increment/decrement 方法以更改其值。您必须传入动态创建的 MBean 的名称,这样 SimpleDynamicMBean 才能找到您注册的 MBean。

 

有趣的是,由于动态注册的 MBean 仍然是一个 MBean,因此仍然可以直接使用应用程序 MBean 浏览器与它进行交互。

 

SimpleNotification — 从 MBean 发出通知

该 Bean 的源代码位于 src/demo/oc4j/jmx/notification 目录下。

JMX 规范的一个非常有用的功能是通知的概念。该功能使得在一个 MBean 自身内发生事件时,该 MBean 可以通知其他感兴趣的各方。基于其他示例中使用的 Counter 示例,该 MBean 在调用操作(递增、递减和重置)时发送通知事件。

通过对您的 MBean 实现类中的 javax.managemement.NotificationBroadcasterSupport 类划分子类,可以轻松地添加对通知的支持。NotificationBroadcasterSupport 类实现 javax.management.NotificationBroadcaster 接口,可以添加和删除通知监听器方法以及 sendNotification 方法,从而减少了您的工作量。

该 Bean 的主要方面包括:

  1. MBean 如何通知 MBeanServer 它想广播的通知。这可以通过实现 getNotificationInfo 方法来实现。该方法将返回一个数组,为该 MBean 可能发送的每个通知指出了通知的 Java 类的名称和通知类型。
  2. 使用 sendNotification 方法发送通知。

SimpleNotifier.java

public class SimpleNotifier
       extends NotificationBroadcasterSupport
       implements SimpleNotifierMBean
{
  /**
   * The notification set.
   */
  private String notifications[] =
  {
    "increment", "decrement", "reset"
  };

  public MBeanNotificationInfo[] getNotificationInfo()
  {
    MBeanNotificationInfo[] info = {
      new MBeanNotificationInfo(notifications,
                                "javax.management.Notification",
                                "Notification set for SimpleNotifier")
    };
    return info;
  }

  public void increment()
  {
    //perform the increment task
    sendNotification(Notification(notifications[0],this,System.currentTimeMillis(),"Count=" + getCount()));

  }

  ...
}

orion-application.xml 包含一个用来静态注册该 MBean 的条目。<jmx-mbean> 标记包含注册 MBean 时使用的属性,以及 MBean 的完全限定名。

<jmx-mbean objectname=":name=SimpleNotifier" class="demo.oc4j.jmx.notification.SimpleNotifier">
<description>An example of the Simple MBean but now with Notifications</description>
</jmx-mbean>

MBeanServer 通过调用 getNotificationInfo 方法来发现通知。当某个 MBean 的通知出现在 Notifications 选项卡上时,Application Server Control 会列出它们。

单击 SimpleNotifier MBean 链接以访问它。

 

选择 Notifications 选项卡,查看通知列表。

 

列出 SimpleNotifier MBean 的通知。此处显示的值是由 MBeanServer 在 MBean 上调用 getNotificationInfo 方法而获得的,然后返回该 MBean 支持的通知列表 — 递增,递减和重置。

Application Server Control 可以接收来自 MBean 的通知。要订阅通知,请选择感兴趣的通知旁边的 Subscribed 复选框。

在我们的 SimpleNotification 示例中,在执行操作(递增、递减和重置)后发送一个通知。使用 Application Server Control 多次调用这些操作。

当 Application Server Control 接收到通知后, 主页上会显示一个指示器(显示通知数量)。

 

单击该链接将显示已接收的所有通知。在下面的示例中,您可以看到递增操作被调用了三次,然后是一个递减操作,然后进行重置。

 

单击特定通知将下钻到该通知的特定详细信息。

 

SimpleExtended — 提供 StandardMBean 的元数据

该 Bean 的源代码位于 src/demo/oc4j/jmx/extendstandard 目录下。

通过使用 StandardMBean,为应用程序提供管理接口成为一个比较直接的机制。由于 MBean 服务器使用反射类型方法,因此它可以发现 MBean 的属性(基于 set/get JavaBean 术语)以及操作,然后在 JMX 代理层公开。

此方法的一个小缺点是,可在 Application Server Control 中显示的方法不提供有用的描述。例如,Application Server Control 中显示了当前 SimpleStandard 示例 MBean,如下所示。其显示的内容包括通用整体描述,以及其操作的每个参数的通用描述。

 

使用动态和模型 MBean,可以轻松地提供元数据来描述 MBean,从而提供有关属性和操作的更多详细信息。通过直接对 javax.management.StandardMBean 类划分子类并覆盖 getMBeanInfo 方法,可以使用 StandardMBean 来实现相同的操作。MBeanServer 调用该方法以获得有关 MBean 本身的信息。

结合使用最简单的 MBean 方法和 StandardMBean 时,MBean 本身并不直接实现该方法,这意味着所提供的信息只是最佳猜测,通常在本质上非常普通。

该 MBean 示例基于相同的简单 Counter 示例。已经对其进行了扩展,可以提供它自己的元数据来描述其属性和操作。

该 MBean 的主要方面包括:

  1. 它直接划分 javax.management.StandardMBean 类的子类。这要求 MBean 提供自己的构造函数,调用父对象的 构造函数,并向该构造函数传递 MBean 接口。
  2. 覆盖 getMBeanInfo 方法。该方法实现将构建一个新的 MBeanInfo 对象,并用有关该 MBean 的信息(属性、操作)填充它,从而允许提供有意义的描述。

SimpleStandardExtend.java:

public class SimpleStandardExtend
       extends StandardMBean
       implements SimpleStandardExtendMBean
{

  // Note that the constructor calls the super class and passes in a parameter
  // which specifies the interface of this MBean.
  public SimpleStandardExtend() throws NotCompliantMBeanException
  {
    super(SimpleStandardExtendMBean.class);
  }

  public MBeanInfo getMBeanInfo()
  {
    // Create meta-data for the Attributes of this bean, we have only one in this case.
    MBeanAttributeInfo[] matts =
    {
      new MBeanAttributeInfo("Count","int","The counter",true,true,false),
    };

    // Create meta-data for each of the operations of this bean
    MBeanOperationInfo[] mops =
    {
      new MBeanOperationInfo("increment","increments the count by 1",null,"void",MBeanOperationInfo.ACTION_INFO),
      new MBeanOperationInfo("decrement","decrements the count by 1",null,"void",MBeanOperationInfo.ACTION_INFO),
      new MBeanOperationInfo("reset","Resets the counter to 0",null,"void",MBeanOperationInfo.ACTION_INFO)
    };

    // Create meta-data for the constructor, use reflection to get the constructor details in this case
    Class c = this.getClass();
    Constructor[] theConstructors = c.getConstructors();
    MBeanConstructorInfo[] mcons =
    {
      new MBeanConstructorInfo("The default constructor",theConstructors[0])
    };

    // Now create the actual MBeanInfo object, using our new meta-data objects
    // The null parameter is for the notifications -- this MBean doesn't have any
    MBeanInfo myInfo = new MBeanInfo(
      this.getClass().getName(),
      "A somewhat silly example of providing meta-data for a Standard MBean, but you get the idea",
      matts, mcons, mops, null);

  return myInfo;
}

orion-application.xml 包含一个用来静态注册该 MBean 的条目。<jmx-mbean> 标记包含注册 MBean 时使用的属性,以及 MBean 的完全限定名。

<jmx-mbean objectname=":name=SimpleExtends" class="demo.oc4j.jmx.extendstandard.SimpleStandardExtend">
<description>A simple example of an MBean that extends StandardMBean to provide its own MBeanInfo</description>
</jmx-mbean>

该 MBean 示例在 Application Server Control 中显示为 SimpleExtends。

现在,当 SimpleExtends MBean 在应用程序 MBean 浏览器中显示时,MBeanInfo 对象中提供的描述就会显示在 MBean 的 Description 域中,而且操作也会包含有用的描述。

 

使用 StandardMBean 的该方法使 MBean 可以告诉管理员更多有关它正在进行的操作的内容,而不是通过使用反射模型获得的简单描述。唯一需要注意的是,getMBeanInfo 方法需要与 MBean 的管理接口完全同步。

SimpleTimer — 使用 TimerMBean

该 Bean 的源代码位于 src/demo/oc4j/jmx/timer 目录下。

JMX 规范通过 javax.management.timer.TimerBean 提供了一个标准的计时器机制。TimerMBean 可以通过 MBeanServer 进行注册,并配置为在特定日期和时间广播通知。Java 对象注册到 TimerMBean,并指定它们对 TimerMBean 广播的哪些通知感兴趣。监听器对象实现 javax.management.NotificationListener 接口。

SimpleTimer MBean 示例将使用“MyTimerBean”名称动态注册一个 TimerMBean。然后,使用该 TimerMBean 注册一个 SimpleTimerListener 对象实例来接收通知。SimpleTimerMBean 公开了一个操作,以允许将新通知添加到计时器。一个通知定义一个要发生的事件。在本示例中,我们通过为事件指定一些数据来定义通知,例如,指定事件的首次触发时间、两个事件之间的间隔以及事件发生的总次数。

该 MBean 示例还使用 SimpleExtended MBean 中描述的方法来提供有关 MBean 的元数据。这是该方法的一个很好的用例,因为 addNotification 操作需要提供大量参数。如果没有其他描述性信息,使用 MBean 操作将非常困难。

该示例的监听器对象是 SimpleTimerListener 类。它实现 NotificationListener 接口,并将接收到的通知的详细信息输出到控制台。

该示例的主要方面包括:

  1. 注册 TimerMBean
  2. 向 TimerMBean 添加 SimpleTimerListener 对象
  3. 向 TimerMBean 添加新的通知事件

SimpleTimer.java

public class SimpleTimer
       extends StandardMBean
       implements SimpleTimerMBean
{

  public SimpleTimer() throws NotCompliantMBeanException
  {
    super(SimpleTimerMBean.class);
    try
    {
      // Create the new Timer object and start it
      _timer = new Timer();
      _timer.start();
  
      // Register the timer object with the MBeanServer if it is not already registered.
      if(!_mbeanServer.isRegistered(new ObjectName(_timerName)))
      {
        _mbeanServer.registerMBean(_timer,new ObjectName(_timerName));
      }

      // Add a notification listener to the newly registered timer
      _mbeanServer.addNotificationListener(
        new ObjectName(_timerName),new SimpleTimerListener(),null,null);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Registers a new notification using the supplied information with the
   * Timer MBean.The notification listener will receive messages based on
   * the timeoffset, the period and the ntimes settings.
   *
   * @param type - the message type, any string will do
   * @param message - the message text, any string will do
   * @param timeoffset - the amount of time (sec) before the first notification
   * @param period - the interval (sec) between notifications
   * @param ntimes - how many notifications to send
   * @return the ID of the registered Notification
   */
  public Integer addNotification(String type, String message, int timeoffset, int period, int ntimes)
  {
    Integer result = null;
    try
    {
      // Add a notification to the timer object we have registered
      result = _timer.addNotification(type, message, null,
       (new Date(System.currentTimeMillis()+(timeoffset*Timer.ONE_SECOND))),
       (period*Timer.ONE_SECOND),ntimes,true);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return result;
  }

  ...
}

SimpleTimerListener.java

public class SimpleTimerListener implements NotificationListener
{

  /**
   * Method called when a notification is received.
   * @param notification - the notification event
   * @param src - the callback object
   */
  public void handleNotification(Notification notification, Object src)
  {
    // Just dump out the details of the notification to stdout
    System.out.println("\n----------------------------");
    System.out.println("\nNew Message Received:");
    System.out.println("\tDate:" + (new Date(notification.getTimeStamp())).toString());
    System.out.println("\tSeq#:" + notification.getSequenceNumber());
    System.out.println("\tType:" + notification.getType());
    System.out.println("\tMessage:" + notification.getMessage());
    System.out.println("" );
    System.out.flush();
  }
}

orion-application.xml 包含一个用来静态注册该 MBean 的条目。<jmx-mbean> 标记包含注册 MBean 时使用的属性,以及 MBean 的完全限定名。

<jmx-mbean objectname=":name=SimpleTimer" class="demo.oc4j.jmx.timer.SimpleTimer">
<description>An example of using an MBean Timer to receive notification events</description>
</jmx-mbean>

使用 Application Server Control,可以为动态注册的 TimerMBean 设置新的通知事件。MBean 是用 SimpleTimer 名称注册的。

 

SimpleTimer MBean 公开一个操作,该操作允许管理员向 MyTimerMBean 添加通知。

addNotification 操作接受许多用于定义新通知的参数。例如,通知类型和消息、通知开始前的时间间隔、发送通知的时间间隔、用于定义通知的已发送通知数量等。

在本示例中,向 MyTimerMBean 添加一个消息,以提醒管理员每隔五分钟有一杯咖啡(这是一个联网的管理员)。

 

当通知从 MyTimerMBean 中发出后,利用 MyTimerMBean 注册的 SimpleTimerListener 会接收通知并在控制台上进行显示。控制台输出会显示这些通知的发送时间以及通知的内容。下面显示的内容显示了 SimpleTimerListener 对象接收到的两个连续通知,这两个通知间隔 5 分钟(300 秒),并按递增顺序发出。

 

在 Application Server Control 中的默认命名空间下可以看到利用该 MBean 示例动态注册的实际 TimerMBean。

 

由于它只是一个 MBean,Application Server Control 可以与 TimerMBean 本身进行交互,并显示其属性和操作。

调用 MyTimerMBean 的 getNotificationMessage 操作, 并向其传递 Coffee 通知的 ID,MBean 将返回该通知消息的值。注意,该值与控制台上通过 SimpleTimerListener 显示的值相同。

 

使用 TimerMBean 可以方便地计划 JMX 环境中将要发生的事件。

总结

该方法示例演示提供了许多 JMX MBean(它们演示了 JMX API 和可用服务的各个基本方面),并演示了 Application Server Control 如何允许管理员操作系统级别和应用程序级别的 MBean。

 

Left Curve
热门下载
Right Curve