【Spring】Spring Framework Reference Documentation中文版28

来源:互联网 发布:sql 多个结果合并 编辑:程序博客网 时间:2024/06/05 04:39

31. JMX

 

31.1 Introduction

介绍

 

The JMX support in Spring provides you with the features to easily and transparently integrate your Spring application into a JMX infrastructure.

JMX支持在spring中提供给你简单的特性和明确的集成你的spring应用到JMX的架构中。

 

JMX?

 

This chapter is not an introduction to JMX…​ it doesnt try to explain the motivations of why one might want to use JMX (or indeed what the letters JMX actually stand for). If you are new to JMX, check out Section 31.8, Further Resourcesat the end of this chapter.

这个章节不是介绍JMX,不是尝试解释为什么可以使用JMX(或JMX实际是做什么的)。如果你是一个JMX新手,可以查看章节31.8,“更多资源”在本章节的末尾。

 

Specifically, Springs JMX support provides four core features:

特定的springJMX支持提供了核心的特性:

 

    The automatic registration of any Spring bean as a JMX MBean

自动注册springbeanJMXbean

    A flexible mechanism for controlling the management interface of your beans

方便的策略用于控制管理bean的接口。

    The declarative exposure of MBeans over remote, JSR-160 connectors

声明远程暴露的MBeansJSR-160连接。

    The simple proxying of both local and remote MBean resources

简单的本地代理和远程的MBean资源。

 

These features are designed to work without coupling your application components to either Spring or JMX interfaces and classes. Indeed, for the most part your application classes need not be aware of either Spring or JMX in order to take advantage of the Spring JMX features.

这些特性被设计不需要耦合你应用的组件不管是对于spring还是JMX接口和类,对于大部分你的应用类不需要知道springJMX就可以使用springJMX的特性。

 

31.2 Exporting your beans to JMX

暴露你的beanJMX

 

The core class in Springs JMX framework is the MBeanExporter. This class is responsible for taking your Spring beans and registering them with a JMX MBeanServer. For example, consider the following class:

springJMX框架中核心类是MBeanExporter。这个类代表使用你的springbean并且注册在JMXMBeanServer中。例如,考虑下面的类:

 

package org.springframework.jmx;

 

public class JmxTestBean implements IJmxTestBean {

 

    private String name;

    private int age;

    private boolean isSuperman;

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getName() {

        return name;

    }

 

    public int add(int x, int y) {

        return x + y;

    }

 

    public void dontExposeMe() {

        throw new RuntimeException();

    }

}

 

To expose the properties and methods of this bean as attributes and operations of an MBean you simply configure an instance of the MBeanExporter class in your configuration file and pass in the bean as shown below:

为了暴露属性和方法对于这个bean作为属性和操作对于MBean,你简单的配置代替MBeanExporter类在你的配置文件中并且传递在bean中如下:

 

<beans>

    <!-- this bean must not be lazily initialized if the exporting is to happen -->

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean"/>

            </map>

        </property>

    </bean>

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

</beans>

 

The pertinent bean definition from the above configuration snippet is the exporter bean. The beans property tells the MBeanExporter exactly which of your beans must be exported to the JMX MBeanServer. In the default configuration, the key of each entry in the beans Map is used as the ObjectName for the bean referenced by the corresponding entry value. This behavior can be changed as described in Section 31.4,Controlling the ObjectNames for your beans.

相关的bean的定义来自上面的配置片段是暴露的bean。这个bean的属性告诉MBeanExporter有关你的bean必须暴露为JMXMBeanServer。对于默认的配置,每个实体的关键在beanmap中被使用作为一个ObjectName用于bean的引用对于相关联的实体值。这个行为可以被改变描述在章节31.4中,“控制ObjectName对于你的bean

 

With this configuration the testBean bean is exposed as an MBean under the ObjectName bean:name=testBean1. By default, all public properties of the bean are exposed as attributes and all public methods (bar those inherited from the Object class) are exposed as operations.

使用这样的配置testBeanbean被暴露作为一个MBeanObjectName bean:name=testBean1。默认的,所有的bean的公共属性被暴露作为一个属性并且所有的公共方法(这些继承自Object类)被暴露作为操作。

 

[Note]

注意

 

MBeanExporter is a Lifecycle bean (see the section called Startup and shutdown callbacks) and MBeans are exported as late as possible during the application lifecycle by default. It is possible to configure the phase at which the export happens or disable automatic registration by setting the autoStartup flag.

MBeanExporter是一个生命周期的bean(见章节“启动和关闭回调”)和MBeans被暴露在应用的默认的周期中。可以配置周期有关暴露或关闭自动注册通过设置autoStartup的标志位。

 

31.2.1 Creating an MBeanServer

创建一个MBeanServer

 

The above configuration assumes that the application is running in an environment that has one (and only one) MBeanServer already running. In this case, Spring will attempt to locate the running MBeanServer and register your beans with that server (if any). This behavior is useful when your application is running inside a container such as Tomcat or IBM WebSphere that has its own MBeanServer.

上面的配置假设应用正在运行在一个环境有一个(且只有一个)MBeanServer正在运行。在这个例子中,spring将尝试定位运行的MBeanServer并且注册你的bean对于服务器(任何服务器)。这个行为是有用的当你的应用运行在一个容器中例如TomcatIBMWebSphere有其自己的MBeanServer

 

However, this approach is of no use in a standalone environment, or when running inside a container that does not provide an MBeanServer. To address this you can create an MBeanServer instance declaratively by adding an instance of the org.springframework.jmx.support.MBeanServerFactoryBean class to your configuration. You can also ensure that a specific MBeanServer is used by setting the value of the MBeanExporters `server property to the MBeanServer value returned by an MBeanServerFactoryBean; for example:

然而,这种行为是没有用的在一个标准的环境中或当运行在容器的内部不需要提供一个MBeanServer。为了表示你可以创建一个MBeanServer实例通过添加一个org.springframework.jmx.support.MBeanServerFactoryBean类的实例到你的配置中。你可以可以指定MBeanServer通过使用设置MBeanExporterserver属性为MBeanServer的值在例子中是通过MBeanServerFactoryBean来返回的。

 

<beans>

 

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>

 

    <!--

    this bean needs to be eagerly pre-instantiated in order for the exporting to occur;

    this means that it must not be marked as lazily initialized

    -->

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean"/>

            </map>

        </property>

        <property name="server" ref="mbeanServer"/>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

</beans>

 

Here an instance of MBeanServer is created by the MBeanServerFactoryBean and is supplied to the MBeanExporter via the server property. When you supply your own MBeanServer instance, the MBeanExporter will not attempt to locate a running MBeanServer and will use the supplied MBeanServer instance. For this to work correctly, you must (of course) have a JMX implementation on your classpath.

这里是一个MBeanServer的实例创建通过MBeanServerFactoryBean并且提供了MBeanExporter通过server属性。当你自己的MBeanServer实例,MBeanExporter将不会尝试定位一个运行的MBeanServer并且将使用支持的MBeanServer实例。对此可以正常运行,你必须(当然)有一个JMX实现在你的classpath中。

 

31.2.2 Reusing an existing MBeanServer

重用一个已有的MBeanServer

 

If no server is specified, the MBeanExporter tries to automatically detect a running MBeanServer. This works in most environment where only one MBeanServer instance is used, however when multiple instances exist, the exporter might pick the wrong server. In such cases, one should use the MBeanServer agentId to indicate which instance to be used:

如果没有指定服务器,MBeanExporter将试图自动探测一个正在运行的MBeanServer。这在大部分环境中适用当只有一个MBeanServer接口被使用的时候,然而当有多个实例存在时,exporter可能选择错误的服务器。在这种情况,应当使用MBeanServeragentId来指定应当使用的实例:

 

<beans>

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">

        <!-- indicate to first look for a server -->

        <property name="locateExistingServerIfPossible" value="true"/>

        <!-- search for the MBeanServer instance with the given agentId -->

        <property name="agentId" value="MBeanServer_instance_agentId>"/>

    </bean>

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="server" ref="mbeanServer"/>

        ...

    </bean>

</beans>

 

For platforms/cases where the existing MBeanServer has a dynamic (or unknown) agentId which is retrieved through lookup methods, one should use factory-method:

对于平台/示例当已有的MBeanServer有一个动态的(或未知的)agentId被接收通过lookup方法,则应当使用factory-method

 

<beans>

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="server">

            <!-- Custom MBeanServerLocator -->

            <bean class="platform.package.MBeanServerLocator" factory-method="locateMBeanServer"/>

        </property>

    </bean>

 

    <!-- other beans here -->

 

</beans>

 

31.2.3 Lazy-initialized MBeans

延迟初始化的MBean

 

If you configure a bean with the MBeanExporter that is also configured for lazy initialization, then the MBeanExporter will not break this contract and will avoid instantiating the bean. Instead, it will register a proxy with the MBeanServer and will defer obtaining the bean from the container until the first invocation on the proxy occurs.

如果你配置一个bean使用了MBeanExporter被配置为延迟加载,则MBeanExporter将不会被连接并且将不会初始化bean。作为替代,他将被注册使用MBeanServer并且将获取bean来自容器知道首先调用代理。

 

31.2.4 Automatic registration of MBeans

自动注册MBeans

 

Any beans that are exported through the MBeanExporter and are already valid MBeans are registered as-is with the MBeanExporter without further intervention from Spring. MBeans can be automatically detected by the MBeanExporter by setting the autodetect property to true:

任何bean可以被暴露通过MBeanExporter并且已经验证MBean是注册作在MBeanExporter不需要更多spring的设置。MBeans可以被自动探测通过MBeanExporter设置autodetect属性为true

 

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

    <property name="autodetect" value="true"/>

</bean>

 

<bean name="spring:mbean=true" class="org.springframework.jmx.export.TestDynamicMBean"/>

 

Here, the bean called spring:mbean=true is already a valid JMX MBean and will be automatically registered by Spring. By default, beans that are autodetected for JMX registration have their bean name used as the ObjectName. This behavior can be overridden as detailed in Section 31.4, Controlling the ObjectNames for your beans.

这里,bean调用spring:mbean=true已经是一个验证JMXMBean并且将自动被spring注册。默认,bean被自动探测对于JMX注册有他们的名字使用通过ObjectName。这个行为可以被覆盖其细节描述在章节31.4中,“处理ObjectName对于你的bean”。

 

31.2.5 Controlling the registration behavior

控制注册的行为

 

Consider the scenario where a Spring MBeanExporter attempts to register an MBean with an MBeanServer using the ObjectName 'bean:name=testBean1'. If an MBean instance has already been registered under that same ObjectName, the default behavior is to fail (and throw an InstanceAlreadyExistsException).

考虑这个场景一个springMBeanExporter尝试注册一个MBean使用MBeanServer使用的ObjectName'bean:name=testBean1'。如果一个MBean的实例已经被注册在相同的ObjectName下,默认的行为会失败(并且抛出一个InstanceAlreadyExistsException异常)。

 

It is possible to control the behavior of exactly what happens when an MBean is registered with an MBeanServer. Springs JMX support allows for three different registration behaviors to control the registration behavior when the registration process finds that an MBean has already been registered under the same ObjectName; these registration behaviors are summarized on the following table:

可以控制明确的行为发生当一个MBean被注册使用了一个MBeanServerspringJMX支持允许用于三个不同的注册行为来控制注册行为当注册程序找到一个MBean已经被注册在相同的ObjectName下;这些注册行为被总结到如下的表格中:

 

Table 31.1. Registration Behaviors

注册行为

Registration behavior

注册行为

Explanation

说明

REGISTRATION_FAIL_ON_EXISTING

This is the default registration behavior. If an MBean instance has already been registered under the same ObjectName, the MBean that is being registered will not be registered and anInstanceAlreadyExistsException will be thrown. The existing MBean is unaffected.

这是默认的注册行为。如果一个MBean实例已经被注册在相同的ObjectName下,MBean被注册将不会被注册并且一个InstanceAlreadyExistsException将被抛出。已有的MBean将失效。

REGISTRATION_IGNORE_EXISTING

If an MBean instance has already been registered under the same ObjectName, the MBean that is being registered will not be registered. The existing MBean is unaffected, and no Exception will be thrown. This is useful in settings where multiple applications want to share a common MBean in a shared MBeanServer.

如果一个MBean的实例已经被注册在相同的ObjectName下,MBean被注册将不会执行。已有的MBean将不会受到影响,并且没有异常会被抛出。当多个应用希望共享相同的MBean在同一个MBeanServer的情况下是十分有意义的。

REGISTRATION_REPLACE_EXISTING

If an MBean instance has already been registered under the same ObjectName, the existing MBean that was previously registered will be unregistered and the new MBean will be registered in its place (the new MBean effectively replaces the previous instance).

如果一个MBean的实例已经被注册在相同ObjectName下,已有的MBean会被取消注册并且新的MBean将会被注册代替他的位置(新的MBean将替代之前的实例)。

The above values are defined as constants on the MBeanRegistrationSupport class (the MBeanExporter class derives from this superclass). If you want to change the default registration behavior, you simply need to set the value of the registrationBehaviorName property on your MBeanExporter definition to one of those values.

上面被定义的值作为MBeanRegistrationSupport类的常量(MBeanExporter类来自这个超类)。如果你希望改变默认的注册行为,你需要简单的设置registrationBehaviorName属性的值在MBeanExporter的定义中对于其中一个值。

 

The following example illustrates how to effect a change from the default registration behavior to the REGISTRATION_REPLACE_EXISTING behavior:

下面的例子展示了如何影响改变默认的注册行为对于REGISTRATION_REPLACE_EXISTING的行为:

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean"/>

            </map>

        </property>

        <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

</beans>

 

31.3 Controlling the management interface of your beans

控制你的bean的管理接口

 

In the previous example, you had little control over the management interface of your bean; all of the public properties and methods of each exported bean was exposed as JMX attributes and operations respectively. To exercise finer-grained control over exactly which properties and methods of your exported beans are actually exposed as JMX attributes and operations, Spring JMX provides a comprehensive and extensible mechanism for controlling the management interfaces of your beans.

在之前的例子中,你需要控制管理接口对于你的bean;所有的公共属性和每个暴露的bean的方法作为JMX属性和相应的操作。为了执行全部的控制对于已有的属性和你暴露的bean的方法被暴露作为JMX的属性和操作。springJMX提供了一个丰富的额外的策略用于控制你的bean的管理接口。

 

31.3.1 the MBeanInfoAssembler Interface

MBeanInfoAssembler接口

 

Behind the scenes, the MBeanExporter delegates to an implementation of the org.springframework.jmx.export.assembler.MBeanInfoAssembler interface which is responsible for defining the management interface of each bean that is being exposed. The default implementation, org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler, simply defines a management interface that exposes all public properties and methods (as you saw in the previous examples). Spring provides two additional implementations of the MBeanInfoAssembler interface that allow you to control the generated management interface using either source-level metadata or any arbitrary interface.

在这个场景中,MBeanExporter为了一个实现对于org.springframework.jmx.export.assembler.MBeanInfoAssembler接口可以代表定义管理每个bean的暴露的接口。默认的实现是org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler简单定义了一个管理接口暴露了所有的公共属性和方法(你在之前的例子中看到的)。spring提供了两个额外的实现对于MBeanInfoAssembler接口允许你来控制生成管理接口使用源码级别的元数据或任何接口。

 

31.3.2 Using Source-Level Metadata (Java annotations)

使用源码级别的元数据(Java注解)

 

Using the MetadataMBeanInfoAssembler you can define the management interfaces for your beans using source level metadata. The reading of metadata is encapsulated by the org.springframework.jmx.export.metadata.JmxAttributeSource interface. Spring JMX provides a default implementation which uses Java annotations, namely org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource. The MetadataMBeanInfoAssembler must be configured with an implementation instance of the JmxAttributeSource interface for it to function correctly (there is no default).

使用MetadataMBeanInfoAssembler你可以定义管理接口用于你的bean使用源码级别的元数据。读取元数据是被保护通过org.springframework.jmx.export.metadata.JmxAttributeSource接口。springJMX提供了一个默认的实现通过使用Java注解,名字为org.springframework.jmx.export.annotation.AnnotationJmxAttributeSourceMetadataMBeanInfoAssembler必须被配置使用一个实现JmxAttributeSource接口的实例用于正确的功能(没有默认值)。

 

To mark a bean for export to JMX, you should annotate the bean class with the ManagedResource annotation. Each method you wish to expose as an operation must be marked with the ManagedOperation annotation and each property you wish to expose must be marked with the ManagedAttribute annotation. When marking properties you can omit either the annotation of the getter or the setter to create a write-only or read-only attribute respectively.

为了标记一个bean用于暴露给JMX,你需要注解bean的类使用ManagedResource注解。你希望暴露的每个方法作为一个操作必须被标记使用ManagedOperation注解并且每个你希望的属性暴露必须标记使用ManagedAttribute注解。当标记属性你可以省略gettersetter方法来创建一个只读或只写的相应的属性。

 

[Note]

注意

 

A ManagedResource annotated bean must be public as well as the methods exposing an operation or an attribute.

一个ManagedResource注解的bean必须是public因为方法暴露了一个操作或一个属性。

 

The example below shows the annotated version of the JmxTestBean class that you saw earlier:

下面的例子展示了注解版本对于JmxTestBean类你之前看到的:

 

package org.springframework.jmx;

 

import org.springframework.jmx.export.annotation.ManagedResource;

import org.springframework.jmx.export.annotation.ManagedOperation;

import org.springframework.jmx.export.annotation.ManagedAttribute;

 

@ManagedResource(

        objectName="bean:name=testBean4",

        description="My Managed Bean",

        log=true,

        logFile="jmx.log",

        currencyTimeLimit=15,

        persistPolicy="OnUpdate",

        persistPeriod=200,

        persistLocation="foo",

        persistName="bar")

public class AnnotationTestBean implements IJmxTestBean {

 

    private String name;

    private int age;

 

    @ManagedAttribute(description="The Age Attribute", currencyTimeLimit=15)

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    @ManagedAttribute(description="The Name Attribute",

            currencyTimeLimit=20,

            defaultValue="bar",

            persistPolicy="OnUpdate")

    public void setName(String name) {

        this.name = name;

    }

 

    @ManagedAttribute(defaultValue="foo", persistPeriod=300)

    public String getName() {

        return name;

    }

 

    @ManagedOperation(description="Add two numbers")

    @ManagedOperationParameters({

        @ManagedOperationParameter(name = "x", description = "The first number"),

        @ManagedOperationParameter(name = "y", description = "The second number")})

    public int add(int x, int y) {

        return x + y;

    }

 

    public void dontExposeMe() {

        throw new RuntimeException();

    }

 

}

 

Here you can see that the JmxTestBean class is marked with the ManagedResource annotation and that this ManagedResource annotation is configured with a set of properties. These properties can be used to configure various aspects of the MBean that is generated by the MBeanExporter, and are explained in greater detail later in section entitled Section 31.3.3,Source-Level Metadata Types.

这里你可以看到JmxTestBean类被标记使用了ManagedResource注解并且这个ManagedResource的注解被配置使用了一个属性set。这些属性可以被使用来配置不同的方面的MBean被生成通过MBeanExporter并且在后续会解释斜街在整个章节31.3.3,“源码级别的媒体类型”。

 

You will also notice that both the age and name properties are annotated with the ManagedAttribute annotation, but in the case of the age property, only the getter is marked. This will cause both of these properties to be included in the management interface as attributes, but the age attribute will be read-only.

你也将注意到agename属性被注解使用了ManagedAttribute注解,但是由于age属性,只有getter属性被标记。这将导致这些属性被包括在管理接口作为属性,但是age属性将是只读。

 

Finally, you will notice that the add(int, int) method is marked with the ManagedOperation attribute whereas the dontExposeMe() method is not. This will cause the management interface to contain only one operation, add(int, int), when using the MetadataMBeanInfoAssembler.

最后,你将注意到add方法被标记使用了ManagedOperation属性但是dontExposeMe方法则没有。这将导致管理接口将包含一个操作,add,当使用MetadataMBeanInfoAssembler的时候。

 

The configuration below shows how you configure the MBeanExporter to use the MetadataMBeanInfoAssembler:

下面的配置展示你如何配置MBeanExporter来使用MetadataMBeanInfoAssembler

 

<beans>

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="assembler" ref="assembler"/>

        <property name="namingStrategy" ref="namingStrategy"/>

        <property name="autodetect" value="true"/>

    </bean>

 

    <bean id="jmxAttributeSource"

            class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

 

    <!-- will create management interface using annotation metadata -->

    <bean id="assembler"

            class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">

        <property name="attributeSource" ref="jmxAttributeSource"/>

    </bean>

 

    <!-- will pick up the ObjectName from the annotation -->

    <bean id="namingStrategy"

            class="org.springframework.jmx.export.naming.MetadataNamingStrategy">

        <property name="attributeSource" ref="jmxAttributeSource"/>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.AnnotationTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

</beans>

 

Here you can see that an MetadataMBeanInfoAssembler bean has been configured with an instance of the AnnotationJmxAttributeSource class and passed to the MBeanExporter through the assembler property. This is all that is required to take advantage of metadata-driven management interfaces for your Spring-exposed MBeans.

这里你可以看到MetadataMBeanInfoAssemblerbean配置使用了AnnotationJmxAttributeSource类的实例并且传递给MBeanExporter通过assembler属性。这是所有需要被考虑基于元数据的管理接口用于你spring暴露的MBean

 

31.3.3 Source-Level Metadata Types

源码级别的元数据类型

 

The following source level metadata types are available for use in Spring JMX:

下面的源码级别的元数据类型适用使用springJMX

 

Table 31.2. Source-Level Metadata Types

源码级别的元数据类型

Purpose

目的

Annotation

注解

Annotation Type

注解类型

Mark all instances of a Class as JMX managed resources

标记所有的类实例作为JMX管理的资源

@ManagedResource

Class

Mark a method as a JMX operation

标记一个方法为JMX操作

@ManagedOperation

Method

Mark a getter or setter as one half of a JMX attribute

标记一个getset方法作为一个JMX属性

@ManagedAttribute

Method (only getters and setters)

Define descriptions for operation parameters

为操作参数定义描述

@ManagedOperationParameter and @ManagedOperationParameters

Method

The following configuration parameters are available for use on these source-level metadata types:

下面的配置参数可用于源码级别的元数据类型:

 

Table 31.3. Source-Level Metadata Parameters

源码级别的元数据参数

Parameter

参数

Description

描述

Applies to

应用于

ObjectName

Used by MetadataNamingStrategy to determine the ObjectName of a managed resource

MetadataNamingStrategy使用来决定管理资源的ObjectName

ManagedResource

description

Sets the friendly description of the resource, attribute or operation

对于资源、属性或操作设置友好的描述信息

ManagedResource, ManagedAttribute, ManagedOperation, ManagedOperationParameter

currencyTimeLimit

Sets the value of the currencyTimeLimit descriptor field

设置currencyTimeLimit描述域的值

ManagedResource, ManagedAttribute

defaultValue

Sets the value of the defaultValue descriptor field

设置defaultValue描述域的值

ManagedAttribute

log

Sets the value of the log descriptor field

设置log描述域的值

ManagedResource

logFile

Sets the value of the logFile descriptor field

设置logFile描述域的值

ManagedResource

persistPolicy

Sets the value of the persistPolicy descriptor field

设置persistPolicy描述域的值

ManagedResource

persistPeriod

Sets the value of the persistPeriod descriptor field

设置persistPeriod描述域的值

ManagedResource

persistLocation

Sets the value of the persistLocation descriptor field

设置persistLocation描述域的值

ManagedResource

persistName

Sets the value of the persistName descriptor field

设置persistName描述域的值

ManagedResource

name

Sets the display name of an operation parameter

设置一个操作参数显示的名字

ManagedOperationParameter

index

Sets the index of an operation parameter

设置一个操作参数的下标

ManagedOperationParameter

 

31.3.4 the AutodetectCapableMBeanInfoAssembler interface

AutodetectCapableMBeanInfoAssembler接口

 

To simplify configuration even further, Spring introduces the AutodetectCapableMBeanInfoAssembler interface which extends the MBeanInfoAssembler interface to add support for autodetection of MBean resources. If you configure the MBeanExporter with an instance of AutodetectCapableMBeanInfoAssembler then it is allowed to "vote" on the inclusion of beans for exposure to JMX.

为了简化配置,spring引入了AutodetectCapableMBeanInfoAssembler接口继承了MBeanInfoAssembler接口来添加额外的支持对于MBean资源的自动探测。如果你配置MBeanExporter使用了AutodetectCapableMBeanInfoAssembler则会允许对于包含的bean投票用于暴露给JMX

 

Out of the box, the only implementation of the AutodetectCapableMBeanInfo interface is the MetadataMBeanInfoAssembler which will vote to include any bean which is marked with the ManagedResource attribute. The default approach in this case is to use the bean name as the ObjectName which results in a configuration like this:

之外,对于AutodetectCapableMBeanInfo接口唯一的实现是MetadataMBeanInfoAssembler将包括任何bean标记了ManagedResource属性。默认的方式是使用bean的名字作为ObjectName代表配置如下:

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <!-- notice how no 'beans' are explicitly configured here -->

        <property name="autodetect" value="true"/>

        <property name="assembler" ref="assembler"/>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

    <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">

        <property name="attributeSource">

            <bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

        </property>

    </bean>

 

</beans>

 

Notice that in this configuration no beans are passed to the MBeanExporter; however, the JmxTestBean will still be registered since it is marked with the ManagedResource attribute and the MetadataMBeanInfoAssembler detects this and votes to include it. The only problem with this approach is that the name of the JmxTestBean now has business meaning. You can address this issue by changing the default behavior for ObjectName creation as defined in Section 31.4,Controlling the ObjectNames for your beans.

注意在这个配置中没有bean被传递给MBeanExporter;然而JmxTestBean依然可以被注册因此他使用了ManagedResource属性并且MetadataMBeanInfoAssembler探测到并且包括他。这个方法唯一的问题是JmxTestBean的名字有了业务的含义。你可以标志这个问题通过改变ObjectName创建描述在章节31.4中,“控制你的beanObjectName”。

 

31.3.5 Defining management interfaces using Java interfaces

使用Java接口定义管理接口

 

In addition to the MetadataMBeanInfoAssembler, Spring also includes the InterfaceBasedMBeanInfoAssembler which allows you to constrain the methods and properties that are exposed based on the set of methods defined in a collection of interfaces.

此外对于MetadataMBeanInfoAssemblerspring也包括InterfaceBasedMBeanInfoAssembler允许你来强制方法和属性被暴露基于方法定义的集合在接口的集合中。

 

Although the standard mechanism for exposing MBeans is to use interfaces and a simple naming scheme, the InterfaceBasedMBeanInfoAssembler extends this functionality by removing the need for naming conventions, allowing you to use more than one interface and removing the need for your beans to implement the MBean interfaces.

由于标准的策略用于暴露MBean是使用接口和一个简单的命名schemeInterfaceBasedMBeanInfoAssembler继承了这个功能通过取掉了对于命名规范的需要,允许你使用一个或多个接口并且取消了你的bean来实现MBean接口。

 

Consider this interface that is used to define a management interface for the JmxTestBean class that you saw earlier:

考虑这个实例被使用来定义管理接口用于你之前看到的JmxTestBean类:

 

public interface IJmxTestBean {

 

    public int add(int x, int y);

 

    public long myOperation();

 

    public int getAge();

 

    public void setAge(int age);

 

    public void setName(String name);

 

    public String getName();

 

}

 

This interface defines the methods and properties that will be exposed as operations and attributes on the JMX MBean. The code below shows how to configure Spring JMX to use this interface as the definition for the management interface:

这个接口定义了方法和属性将被暴露作为操作和属性对于JMXMBean。下面的代码展示了如何配置springJMX来使用这个接口作为定义用于管理接口:

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean5" value-ref="testBean"/>

            </map>

        </property>

        <property name="assembler">

            <bean class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">

                <property name="managedInterfaces">

                    <value>org.springframework.jmx.IJmxTestBean</value>

                </property>

            </bean>

        </property>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

      </bean>

 

</beans>

 

Here you can see that the InterfaceBasedMBeanInfoAssembler is configured to use the IJmxTestBean interface when constructing the management interface for any bean. It is important to understand that beans processed by the InterfaceBasedMBeanInfoAssembler are not required to implement the interface used to generate the JMX management interface.

这里你可以看到InterfaceBasedMBeanInfoAssembler被配置来使用IJmxTestBean接口当创建一个管理接口用于任意的bean。这是重要的有关理解bean暴露通过InterfaceBasedMBeanInfoAssembler不是必须的来实现接口用于生成JMX的管理接口。

 

In the case above, the IJmxTestBean interface is used to construct all management interfaces for all beans. In many cases this is not the desired behavior and you may want to use different interfaces for different beans. In this case, you can pass InterfaceBasedMBeanInfoAssembler a Properties instance via the interfaceMappings property, where the key of each entry is the bean name and the value of each entry is a comma-separated list of interface names to use for that bean.

在上面的例子中,IJmxTestBean接口被用于构建所有管理接口对于所有的bean。在大部分情况下没有设计行为并且你希望使用不同的接口用于不同的bean。在这个例子中,你可以传递InterfaceBasedMBeanInfoAssembler一个属性实例通过interfaceMappings属性,关键是对于每个实例的bean的名字和每个实体的值是一个逗号分隔的接口名列表用于这个bean

 

If no management interface is specified through either the managedInterfaces or interfaceMappings properties, then the InterfaceBasedMBeanInfoAssembler will reflect on the bean and use all of the interfaces implemented by that bean to create the management interface.

如果没有管理接口被定义通过managedInterfacesinterfaceMappings属性,然后InterfaceBasedMBeanInfoAssembler将会影响bean并且使用所有的借口实现来创建管理接口。

 

31.3.6 Using MethodNameBasedMBeanInfoAssembler

使用MethodNameBasedMBeanInfoAssembler

 

The MethodNameBasedMBeanInfoAssembler allows you to specify a list of method names that will be exposed to JMX as attributes and operations. The code below shows a sample configuration for this:

MethodNameBasedMBeanInfoAssembler允许你来指定一个方法名的列表将会暴露为JMX作为属性和操作。下面的代码展示了一个简单的配置:

 

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

    <property name="beans">

        <map>

            <entry key="bean:name=testBean5" value-ref="testBean"/>

        </map>

    </property>

    <property name="assembler">

        <bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">

            <property name="managedMethods">

                <value>add,myOperation,getName,setName,getAge</value>

            </property>

        </bean>

    </property>

</bean>

 

Here you can see that the methods add and myOperation will be exposed as JMX operations and getName(), setName(String) and getAge() will be exposed as the appropriate half of a JMX attribute. In the code above, the method mappings apply to beans that are exposed to JMX. To control method exposure on a bean-by-bean basis, use the methodMappings property of MethodNameMBeanInfoAssembler to map bean names to lists of method names.

这里你可以看到方法addmyOperation将被暴露作为JMX操作和getNamesetNamegetAge将被暴露作为JMX属性的一半。在上面的代码中,方法匹配应用于beans暴露给JMX。为了控制方法的暴露对于一个bean-by-bean的基础,使用MethodNameMBeanInfoAssemblermethodMappings属性来匹配bean的名字对于方法名。

 

31.4 Controlling the ObjectNames for your beans

为你的bean控制ObjectName

 

Behind the scenes, the MBeanExporter delegates to an implementation of the ObjectNamingStrategy to obtain ObjectNames for each of the beans it is registering. The default implementation, KeyNamingStrategy, will, by default, use the key of the beans Map as the ObjectName. In addition, the KeyNamingStrategy can map the key of the beans Map to an entry in a Properties file (or files) to resolve the ObjectName. In addition to the KeyNamingStrategy, Spring provides two additional ObjectNamingStrategy implementations: the IdentityNamingStrategy that builds an ObjectName based on the JVM identity of the bean and the MetadataNamingStrategy that uses source level metadata to obtain the ObjectName.

下面的场景中,MBeanExporter使用了ObjectNamingStrategy的实现来获取ObjectName用于每个注册的bean。默认的实现KeyNamingStrategy默认使用bean的关键字的beanmap作为ObjectName。此外,KeyNamingStrategy可以匹配beanMap的关键字对于Properties文件(或多个文件)的实体来解析ObjectName。此外对于KeyNamingStrategyspring提供了两个额外的ObjectNamingStrategy实现:IdentityNamingStrategy构建ObjectName基于JVM对于bean的定义和MetadataNamingStrategy使用了源码级别的元数据来获取ObjectName

 

31.4.1 Reading ObjectNames from Properties

从属性中读取ObjectName

 

You can configure your own KeyNamingStrategy instance and configure it to read ObjectNames from a Properties instance rather than use bean key. The KeyNamingStrategy will attempt to locate an entry in the Properties with a key corresponding to the bean key. If no entry is found or if the Properties instance is null then the bean key itself is used.

你可以配置你自己的KeyNamingStrategy实例并且配置他来读取ObjectName来自Properties实例而不是使用beankeyKeyNamingStrategy将会尝试定位Properties中的实体使用一个关键字想关于beankey。如果没有找到实体或如果属性实例是nullbeankey本身是可以使用的。

 

The code below shows a sample configuration for the KeyNamingStrategy:

下面的代码展示了一个样例的配置用于KeyNamingStrategy

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="testBean" value-ref="testBean"/>

            </map>

        </property>

        <property name="namingStrategy" ref="namingStrategy"/>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

    <bean id="namingStrategy" class="org.springframework.jmx.export.naming.KeyNamingStrategy">

        <property name="mappings">

            <props>

                <prop key="testBean">bean:name=testBean1</prop>

            </props>

        </property>

        <property name="mappingLocations">

            <value>names1.properties,names2.properties</value>

        </property>

    </bean>

 

</beans>

 

Here an instance of KeyNamingStrategy is configured with a Properties instance that is merged from the Properties instance defined by the mapping property and the properties files located in the paths defined by the mappings property. In this configuration, the testBean bean will be given the ObjectName bean:name=testBean1 since this is the entry in the Properties instance that has a key corresponding to the bean key.

这里是一个KeyNamingStrategy的实例被配置使用了Properties实例组合了Properties实例定义通过匹配属性和加载了路径中的属性文件通过mapping属性。在配置中,testBean将被给予bjectName bean:name=testBean1由于这是在Properties实例中的实体有一个关键字相对于beankey

 

If no entry in the Properties instance can be found then the bean key name is used as the ObjectName.

如果没有实体在Properties实例中可以被找到则beankey名字将被使用作为ObjectName

 

31.4.2 Using the MetadataNamingStrategy

使用MetadataNamingStrategy

 

The MetadataNamingStrategy uses the objectName property of the ManagedResource attribute on each bean to create the ObjectName. The code below shows the configuration for the MetadataNamingStrategy:

MetadataNamingStrategy使用ManagedResource属性的objectName属性对于每个bean来创建ObjectName。下面的代码展示了对于MetadataNamingStrategy的配置:

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="testBean" value-ref="testBean"/>

            </map>

        </property>

        <property name="namingStrategy" ref="namingStrategy"/>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

    <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">

        <property name="attributeSource" ref="attributeSource"/>

    </bean>

 

    <bean id="attributeSource"

            class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

 

</beans>

 

If no objectName has been provided for the ManagedResource attribute, then an ObjectName will be created with the following format:[fully-qualified-package-name]:type=[short-classname],name=[bean-name]. For example, the generated ObjectName for the following bean would be: com.foo:type=MyClass,name=myBean.

如果没有提供objectName对于ManagedResource属性,则会创建一个ObjectName使用下面的格式:[fully-qualified-package-name]:type=[short-classname],name=[bean-name]。例如,生成的ObjectName对于下面的bean将会是:com.foo:type=MyClass,name=myBean

 

<bean id="myBean" class="com.foo.MyClass"/>

 

31.4.3 Configuring annotation based MBean export

配置注解基于MBean导出

 

If you prefer using the annotation based approach to define your management interfaces, then a convenience subclass of MBeanExporter is available: AnnotationMBeanExporter. When defining an instance of this subclass, the namingStrategy, assembler, and attributeSource configuration is no longer needed, since it will always use standard Java annotation-based metadata (autodetection is always enabled as well). In fact, rather than defining an MBeanExporter bean, an even simpler syntax is supported by the @EnableMBeanExport @Configuration annotation.

如果你倾向于使用基于注解的方式来定义你的管理接口,MBeanExporter的一个方便的子类是可用的:AnnotationMBeanExporter。当定义子类的实例,namingStrategyassemblerattributeSource配置将不再被需要,因此他们使用标准的Java注解的元数据(自动探测是默认被开启的)。实际上对于定义一个MBeanExporterbean,一个更加简单的语法是支持通过@EnableMBeanExport@Configuration注解。

 

@Configuration

@EnableMBeanExport

public class AppConfig {

 

}

 

If you prefer XML based configuration the 'context:mbean-export' element serves the same purpose.

如果你倾向于基于xml的配置则'context:mbean-export'元素实现的效果是一样的。

 

<context:mbean-export/>

 

You can provide a reference to a particular MBean server if necessary, and the defaultDomain attribute (a property of AnnotationMBeanExporter) accepts an alternate value for the generated MBean `ObjectNamesdomains. This would be used in place of the fully qualified package name as described in the previous section on MetadataNamingStrategy.

你可以提供一个引用对于特定的MBean服务器如果有必要的并且defaultDomain属性(AnnotationMBeanExporter的一个属性)接收生成的MBean的值“ObjectName域”。这将被使用由于全限定包名作为描述在之前的部分对于MetadataNamingStrategy

 

@EnableMBeanExport(server="myMBeanServer", defaultDomain="myDomain")

@Configuration

ContextConfiguration {

 

}

 

<context:mbean-export server="myMBeanServer" default-domain="myDomain"/>

 

[Note]

注意

 

Do not use interface-based AOP proxies in combination with autodetection of JMX annotations in your bean classes. Interface-based proxies 'hide' the target class, which also hides the JMX managed resource annotations. Hence, use target-class proxies in that case: through setting the 'proxy-target-class' flag on <aop:config/>, <tx:annotation-driven/>, etc. Otherwise, your JMX beans might be silently ignored at startup…​

不要使用基于接口的AOP代理来合并自动探测的JMX注解在你的bean的类中。基于接口的代理隐藏了目标,也隐藏了JMX管理的资源注解。因此,使用目标类代理在例子中:因此设置<aop:config/>, <tx:annotation-driven/>中的'proxy-target-class'标志位等等。你的JMXbean可以忽略在启动时。

 

31.5 JSR-160 Connectors

JSR-160连接器

 

For remote access, Spring JMX module offers two FactoryBean implementations inside the org.springframework.jmx.support package for creating both server- and client-side connectors.

对于远程访问,springJMX模块提供了两个FactoryBean实现在org.springframework.jmx.support包中用于创建服务端和客户端的连接器。

 

31.5.1 Server-side Connectors

服务端的连接器

 

To have Spring JMX create, start and expose a JSR-160 JMXConnectorServer use the following configuration:

为了有springJMX创建、开始和暴露一个JSR-160连接器使用了下面的配置:

 

<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"/>

 

By default ConnectorServerFactoryBean creates a JMXConnectorServer bound to "service:jmx:jmxmp://localhost:9875". The serverConnector bean thus exposes the local MBeanServer to clients through the JMXMP protocol on localhost, port 9875. Note that the JMXMP protocol is marked as optional by the JSR 160 specification: currently, the main open-source JMX implementation, MX4J, and the one provided with the JDK do not support JMXMP.

默认的ConnectorServerFactoryBean创建了一个JMXConnectorServer绑定到"service:jmx:jmxmp://localhost:9875"serverConnectorbean暴露了本地的MBeanServer给客户端通过JMXMP协议对于本地端口是9875。注意JMXMP协议被标记作为一个可选组件通过JSR160的规范:目前,主要的开源的JMX实现是MX4JJDK提供但是不支持JMXMP

 

To specify another URL and register the JMXConnectorServer itself with the MBeanServer use the serviceUrl and ObjectName properties respectively:

为了指定另一个URL并且注册了JMXConnectorServer本身使用了MBeanServer明确使用了serviceUrlObjectName属性。

 

<bean id="serverConnector"

        class="org.springframework.jmx.support.ConnectorServerFactoryBean">

    <property name="objectName" value="connector:name=rmi"/>

    <property name="serviceUrl"

            value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector"/>

</bean>

 

If the ObjectName property is set Spring will automatically register your connector with the MBeanServer under that ObjectName. The example below shows the full set of parameters which you can pass to the ConnectorServerFactoryBean when creating a JMXConnector:

如果ObjectName属性设置spring将自动注册你的连接器对于MBeanServerObjectName之下。下面的例子展示了全部的参数设置你可能传递给ConnectorServerFactoryBean当创建一个JMXConnector的时候:

 

<bean id="serverConnector"

        class="org.springframework.jmx.support.ConnectorServerFactoryBean">

    <property name="objectName" value="connector:name=iiop"/>

    <property name="serviceUrl"

        value="service:jmx:iiop://localhost/jndi/iiop://localhost:900/myconnector"/>

    <property name="threaded" value="true"/>

    <property name="daemon" value="true"/>

    <property name="environment">

        <map>

            <entry key="someKey" value="someValue"/>

        </map>

    </property>

</bean>

 

Note that when using a RMI-based connector you need the lookup service (tnameserv or rmiregistry) to be started in order for the name registration to complete. If you are using Spring to export remote services for you via RMI, then Spring will already have constructed an RMI registry. If not, you can easily start a registry using the following snippet of configuration:

注意当使用基于RMI的连接器时你需要查找服务(tnameservrmiregistry)来启动用于命名注册来完成。如果你使用spring来暴露远程服务对于你通过RMI,则spring将连接RMIregistry。如果不是你可以指定启动一个registry使用下面的配置片段:

 

<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">

    <property name="port" value="1099"/>

</bean>

 

31.5.2 Client-side Connectors

客户端连接器

 

To create an MBeanServerConnection to a remote JSR-160 enabled MBeanServer use the MBeanServerConnectionFactoryBean as shown below:

为了创建MBeanServerConnection对于远程的JSR-160MBeanServer使用MBeanServerConnectionFactoryBean展示如下:

 

<bean id="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">

    <property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi"/>

</bean>

 

31.5.3 JMX over Burlap/Hessian/SOAP

基于Burlap/Hessian/SOAPJMX

 

JSR-160 permits extensions to the way in which communication is done between the client and the server. The examples above are using the mandatory RMI-based implementation required by the JSR-160 specification (IIOP and JRMP) and the (optional) JMXMP. By using other providers or JMX implementations (such as MX4J) you can take advantage of protocols like SOAP, Hessian, Burlap over simple HTTP or SSL and others:

JSR-160允许扩展对于通讯在客户端和服务端。上面的例子使用了强制的基于RMI的实现符合JSR-160规范(IIOPJRMP)和(可选的)JXMP。通过使用其他提供者或JMX实现(例如MX4J)你可以使用类似于SOAPHessianBurlap的优点基于简单的HTTPSSL等。

 

<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">

    <property name="objectName" value="connector:name=burlap"/>

    <property name="serviceUrl" value="service:jmx:burlap://localhost:9874"/>

</bean>

 

In the case of the above example, MX4J 3.0.0 was used; see the official MX4J documentation for more information.

在上面的例子中,使用了MX4J3.0.0;见官方的MX4J的文档来了解更多信息。

 

31.6 Accessing MBeans via Proxies

通过代理访问MBeans

 

Spring JMX allows you to create proxies that re-route calls to MBeans registered in a local or remote MBeanServer. These proxies provide you with a standard Java interface through which you can interact with your MBeans. The code below shows how to configure a proxy for an MBean running in a local MBeanServer:

springJMX允许你来创建代理来重新路由对于MBean的调用在本地或远程的MBeanServer中。这些代理提供了标准的Java接口因此你可以连接你的MBeans。下面的代码展示了如何配置一个代理用于MBean来运行一个本地的MBeanServer

 

<bean id="proxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean">

    <property name="objectName" value="bean:name=testBean"/>

    <property name="proxyInterface" value="org.springframework.jmx.IJmxTestBean"/>

</bean>

 

Here you can see that a proxy is created for the MBean registered under the ObjectName: bean:name=testBean. The set of interfaces that the proxy will implement is controlled by the proxyInterfaces property and the rules for mapping methods and properties on these interfaces to operations and attributes on the MBean are the same rules used by the InterfaceBasedMBeanInfoAssembler.

这里你可以看到一个代理被创建用于MBean的注册基于ObjectName: bean:name=testBean。代理实现了接口的集合被proxyInterfaces属性来控制并且方法匹配的规则和对于这些接口的属性来操作和MBean上的属性是相同的通过使用InterfaceBasedMBeanInfoAssembler

 

The MBeanProxyFactoryBean can create a proxy to any MBean that is accessible via an MBeanServerConnection. By default, the local MBeanServer is located and used, but you can override this and provide an MBeanServerConnection pointing to a remote MBeanServer to cater for proxies pointing to remote MBeans:

MBeanProxyFactoryBean可以创建一个代理对于任何的MBean如果可以通过MBeanServerConnection来访问的话。默认的本地的MBeanServer被定位并且使用,但是你可以覆盖并且提供一个MBeanServerConnection来指向远程的MBeanServer用于代理来指向远程的MBeans

 

<bean id="clientConnector"

        class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">

    <property name="serviceUrl" value="service:jmx:rmi://remotehost:9875"/>

</bean>

 

<bean id="proxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean">

    <property name="objectName" value="bean:name=testBean"/>

    <property name="proxyInterface" value="org.springframework.jmx.IJmxTestBean"/>

    <property name="server" ref="clientConnector"/>

</bean>

 

Here you can see that we create an MBeanServerConnection pointing to a remote machine using the MBeanServerConnectionFactoryBean. This MBeanServerConnection is then passed to the MBeanProxyFactoryBean via the server property. The proxy that is created will forward all invocations to the MBeanServer via this MBeanServerConnection.

这里你可以看到我们创建了一个MBeanServerConnection指向了一个远程的机器通过使用MBeanServerConnectionFactoryBeanMBeanServerConnection被传递给MBeanProxyFactoryBean通过server属性。代理被创建将转发所有的调用给MBeanServer通过这个MBeanServerConnection

 

31.7 Notifications

通知

 

Springs JMX offering includes comprehensive support for JMX notifications.

springJMX提供包括了对于JMX通知的完善的支持。

 

31.7.1 Registering Listeners for Notifications

对于通过注册监听器

 

Springs JMX support makes it very easy to register any number of NotificationListeners with any number of MBeans (this includes MBeans exported by Springs MBeanExporter and MBeans registered via some other mechanism). By way of an example, consider the scenario where one would like to be informed (via a Notification) each and every time an attribute of a target MBean changes.

springJMX支持使得注册任何数量的NotificationListeners配合任意数量的MBean变得非常简单(包括通过springMBeanExporter暴露的MBeans和通过其他策略注册的MBeans)。考虑一个场景需要被通知(通过一个Notification)每一个参与者和一个目标MBean的属性的改变。

 

package com.example;

 

import javax.management.AttributeChangeNotification;

import javax.management.Notification;

import javax.management.NotificationFilter;

import javax.management.NotificationListener;

 

public class ConsoleLoggingNotificationListener

        implements NotificationListener, NotificationFilter {

 

    public void handleNotification(Notification notification, Object handback) {

        System.out.println(notification);

        System.out.println(handback);

    }

 

    public boolean isNotificationEnabled(Notification notification) {

        return AttributeChangeNotification.class.isAssignableFrom(notification.getClass());

    }

 

}

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean"/>

            </map>

        </property>

        <property name="notificationListenerMappings">

            <map>

                <entry key="bean:name=testBean1">

                    <bean class="com.example.ConsoleLoggingNotificationListener"/>

                </entry>

            </map>

        </property>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

</beans>

 

With the above configuration in place, every time a JMX Notification is broadcast from the target MBean ( bean:name=testBean1), the ConsoleLoggingNotificationListener bean that was registered as a listener via the notificationListenerMappings property will be notified. The ConsoleLoggingNotificationListener bean can then take whatever action it deems appropriate in response to the Notification.

对于上面的配置,每次一个JMX的通知来自目标的MBeanbean:name=testBean1),ConsoleLoggingNotificationListenerbean将被注册作为一个监听器通过notificationListenerMappings属性将被通知到。ConsoleLoggingNotificationListenerbean可以采取行动并且对通知做出响应。

 

You can also use straight bean names as the link between exported beans and listeners:

你也可以直接使用bean的名字作为连接在暴露的bean和监听器之间:

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean"/>

            </map>

        </property>

        <property name="notificationListenerMappings">

            <map>

                <entry key="testBean">

                    <bean class="com.example.ConsoleLoggingNotificationListener"/>

                </entry>

            </map>

        </property>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

</beans>

 

If one wants to register a single NotificationListener instance for all of the beans that the enclosing MBeanExporter is exporting, one can use the special wildcard '*' (sans quotes) as the key for an entry in the notificationListenerMappings property map; for example:

如果希望注册一个简单的NotificationListener实例对于所有的bean对于暴露的MBeanExporter,可以使用指定的通配符*(不需要引号)作为关键字用于在notificationListenerMappings属性map中的实体;例如:

 

<property name="notificationListenerMappings">

    <map>

        <entry key="*">

            <bean class="com.example.ConsoleLoggingNotificationListener"/>

        </entry>

    </map>

</property>

 

If one needs to do the inverse (that is, register a number of distinct listeners against an MBean), then one has to use the notificationListeners list property instead (and in preference to the notificationListenerMappings property). This time, instead of configuring simply a NotificationListener for a single MBean, one configures NotificationListenerBean instances…​a NotificationListenerBean encapsulates a NotificationListener and the ObjectName (or ObjectNames) that it is to be registered against in an MBeanServer. The NotificationListenerBean also encapsulates a number of other properties such as a NotificationFilter and an arbitrary handback object that can be used in advanced JMX notification scenarios.

如果需要反转(就是注册一些独特的监听器用于MBean),使用notificationListeners的列表属性代替(优先于notificationListenerMappings属性)。这次,代替配置简单的NotificationListener用于单一的MBean,配置NotificationListenerBean实例,NotificationListenerBean压缩NotificationListenerObjectName(或ObjectNames)用于被注册在一个MBeanServer中。NotificationListenerBean也压缩一些其他属性例如NotificationFilter和任意的handbackobject可以用于JMX的通知场景。

 

The configuration when using NotificationListenerBean instances is not wildly different to what was presented previously:

当使用NotificationListenerBean实例的配置是不同于之前的表现:

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean"/>

            </map>

        </property>

        <property name="notificationListeners">

            <list>

                <bean class="org.springframework.jmx.export.NotificationListenerBean">

                    <constructor-arg>

                        <bean class="com.example.ConsoleLoggingNotificationListener"/>

                    </constructor-arg>

                    <property name="mappedObjectNames">

                        <list>

                            <value>bean:name=testBean1</value>

                        </list>

                    </property>

                </bean>

            </list>

        </property>

    </bean>

 

    <bean id="testBean" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

</beans>

 

The above example is equivalent to the first notification example. Lets assume then that we want to be given a handback object every time a Notification is raised, and that additionally we want to filter out extraneous Notifications by supplying a NotificationFilter. (For a full discussion of just what a handback object is, and indeed what a NotificationFilter is, please do consult that section of the JMX specification (1.2) entitled 'The JMX Notification Model'.)

上面的例子等同于第一个通知的例子。让我们假设我们希望每次被给予handbackobject的通知,并且此外我们希望过滤外来的通知通过应用一个NotificationFilter。(对于一个handbackobject的讨论是,就是什么是NotificationFilter,请参考JMX定义中的内容名字为‘JMX的通知模型’。)

 

<beans>

 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">

        <property name="beans">

            <map>

                <entry key="bean:name=testBean1" value-ref="testBean1"/>

                <entry key="bean:name=testBean2" value-ref="testBean2"/>

            </map>

        </property>

        <property name="notificationListeners">

            <list>

                <bean class="org.springframework.jmx.export.NotificationListenerBean">

                    <constructor-arg ref="customerNotificationListener"/>

                    <property name="mappedObjectNames">

                        <list>

                            <!-- handles notifications from two distinct MBeans -->

                            <value>bean:name=testBean1</value>

                            <value>bean:name=testBean2</value>

                        </list>

                    </property>

                    <property name="handback">

                        <bean class="java.lang.String">

                            <constructor-arg value="This could be anything..."/>

                        </bean>

                    </property>

                    <property name="notificationFilter" ref="customerNotificationListener"/>

                </bean>

            </list>

        </property>

    </bean>

 

    <!-- implements both the NotificationListener and NotificationFilter interfaces -->

    <bean id="customerNotificationListener" class="com.example.ConsoleLoggingNotificationListener"/>

 

    <bean id="testBean1" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="TEST"/>

        <property name="age" value="100"/>

    </bean>

 

    <bean id="testBean2" class="org.springframework.jmx.JmxTestBean">

        <property name="name" value="ANOTHER TEST"/>

        <property name="age" value="200"/>

    </bean>

 

</beans>

 

31.7.2 Publishing Notifications

发布通知

 

Spring provides support not just for registering to receive Notifications, but also for publishing Notifications.

spring提供了支持不只是注册来接收通知,也包括发布通知。

 

[Note]

通知

 

Please note that this section is really only relevant to Spring managed beans that have been exposed as MBeans via an MBeanExporter; any existing, user-defined MBeans should use the standard JMX APIs for notification publication.

请注意这一章节只相关于spring管理bean并且暴露作为MBean通过MBeanExporter;任何已有的用户定义的MBean应当使用标准的JMXAPI用于通知发布。

 

The key interface in Springs JMX notification publication support is the NotificationPublisher interface (defined in the org.springframework.jmx.export.notification package). Any bean that is going to be exported as an MBean via an MBeanExporter instance can implement the related NotificationPublisherAware interface to gain access to a NotificationPublisher instance. The NotificationPublisherAware interface simply supplies an instance of a NotificationPublisher to the implementing bean via a simple setter method, which the bean can then use to publish Notifications.

springJMX通知中的关键接口发布通知支持是NotificationPublisher接口(定义在org.springframework.jmx.export.notification包中)。任何的bean可以暴露作为一个MBean通过一个MBeanExporter实例可以实现相关的NotificationPublisherAware接口来获取访问NotificationPublisher实例。NotificationPublisherAware接口简单的支持NotificationPublisher的实例来实现bean通过一个简单的设置方法,bean可以使用他来发布通知。

 

As stated in the javadocs of the NotificationPublisher class, managed beans that are publishing events via the NotificationPublisher mechanism are not responsible for the state management of any notification listeners and the like…​ Springs JMX support will take care of handling all the JMX infrastructure issues. All one need do as an application developer is implement the NotificationPublisherAware interface and start publishing events using the supplied NotificationPublisher instance. Note that the NotificationPublisher will be set after the managed bean has been registered with an MBeanServer.

就像NotificationPublisher类开始部分的javadocs中描述,管理bean可以发布事件通过NotificationPublisher测量是不会用于状态管理对于任何通知监听器例如springJMX支持将处理所有的JMX的问题。需要作为一个应用开发者是实现NotificationPublisherAware接口并且开始发布事件使用来支持NotificationPublisher实例。注意NotificationPublisher将被设置在管理bean注册之后对于MBeanServer

 

Using a NotificationPublisher instance is quite straightforward…​one simply creates a JMX Notification instance (or an instance of an appropriate Notification subclass), populates the notification with the data pertinent to the event that is to be published, and one then invokes the sendNotification(Notification) on the NotificationPublisher instance, passing in the Notification.

使用NotificationPublisher实例是很直接的,可以简单的创建一个JMX通知实例(或一个应用通知的子类),暴露通知使用与数据相关的事件被发布,并且可以调用NotificationPublisher实例上的sendNotification(Notification)来传递通知。

 

Find below a simple example…​ in this scenario, exported instances of the JmxTestBean are going to publish a NotificationEvent every time the add(int, int) operation is invoked.

下面一个简单的例子,在这个场景中,暴露JmxTestBean的实例被用于每次发布通知通过add(int, int)操作。

 

package org.springframework.jmx;

 

import org.springframework.jmx.export.notification.NotificationPublisherAware;

import org.springframework.jmx.export.notification.NotificationPublisher;

import javax.management.Notification;

 

public class JmxTestBean implements IJmxTestBean, NotificationPublisherAware {

 

    private String name;

    private int age;

    private boolean isSuperman;

    private NotificationPublisher publisher;

 

    // other getters and setters omitted for clarity

 

    public int add(int x, int y) {

        int answer = x + y;

        this.publisher.sendNotification(new Notification("add", this, 0));

        return answer;

    }

 

    public void dontExposeMe() {

        throw new RuntimeException();

    }

 

    public void setNotificationPublisher(NotificationPublisher notificationPublisher) {

        this.publisher = notificationPublisher;

    }

 

}

 

The NotificationPublisher interface and the machinery to get it all working is one of the nicer features of Springs JMX support. It does however come with the price tag of coupling your classes to both Spring and JMX; as always, the advice here is to be pragmatic…​if you need the functionality offered by the NotificationPublisher and you can accept the coupling to both Spring and JMX, then do so.

NotificationPublisher接口和获取的测试是spring对于JMX支持的一个很好的特性。他使用价格标签来耦合你的类对于springJMX;并且,在这里通知是实际的,如果你需要NotificationPublisher提供的功能你可以访问springJMX

 

31.8 Further Resources

更多的资源

 

This section contains links to further resources about JMX.

这个章节关联的更多有关JMX的资源。

 

    The JMX homepage at Oracle

Oracle中的JMX主页

    The JMX specification (JSR-000003)

JMX的规范

    The JMX Remote API specification (JSR-000160)

JMX的远程API定义

    The MX4J homepage (an Open Source implementation of various JMX specs)

MX4J的主页(对于不同JMX的开源的实现)

 

阅读全文
0 0
原创粉丝点击