此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
控制 Bean 的 Management Interface
在上一节的示例中,
您对 Bean 的 Management Interface 几乎没有控制权。所有public
每个导出的 bean 的属性和方法都公开为 JMX 属性和
作。要对具体内容进行更精细的控制
导出的 bean 的属性和方法实际上作为 JMX 属性公开
和作,Spring JMX 为
控制 bean 的 Management 接口。
使用MBeanInfoAssembler
应用程序接口
在幕后,MBeanExporter
delegates 的org.springframework.jmx.export.assembler.MBeanInfoAssembler
API,即
负责定义每个公开的 bean 的 Management 接口。
默认实现org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler
,
定义公开所有公共属性和方法的管理接口
(如您在前面部分的示例中看到的那样)。Spring 提供两个
其他MBeanInfoAssembler
界面,让您
使用源级元数据控制生成的管理界面
或任何任意接口。
使用源级元数据:Java 标注
通过使用MetadataMBeanInfoAssembler
中,您可以定义
使用 source level metadata 的 bean。元数据的读取由org.springframework.jmx.export.metadata.JmxAttributeSource
接口。Spring JMX
提供了一个使用 Java 注解的默认实现,即org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource
.您必须
配置MetadataMBeanInfoAssembler
使用JmxAttributeSource
接口使其正常运行,因为没有默认值。
要将 Bean 标记为导出到 JMX,您应该使用@ManagedResource
注解。您必须将您希望公开的每个方法注释为
作与@ManagedOperation
注释并注释您希望的每个属性
expose 替换为@ManagedAttribute
注解。在注释属性时,您可以省略
getter 或 setter 的注解,用于创建 write-only 或 read-only
属性。
一个@ManagedResource -annotated bean 必须是 public,公开
作或属性。 |
以下示例显示了JmxTestBean
类,我们
用于创建 MBeanServer。
package org.springframework.jmx;
@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 {
private int age;
private String name;
public void setAge(int age) {
this.age = age;
}
@ManagedAttribute(description="The Age Attribute", currencyTimeLimit=15)
public int getAge() {
return this.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 this.name;
}
@ManagedOperation(description="Add two numbers")
@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();
}
}
在前面的示例中,您可以看到AnnotationTestBean
class 被注解
跟@ManagedResource
而这个@ManagedResource
配置了 annotation
具有一组属性。这些属性可用于配置各个方面
由MBeanExporter
并在 Greater
detail 在 Spring JMX 注释中。
这age
和name
属性使用@ManagedAttribute
,
但是,在age
property,则仅对 getter 方法进行注释。
这会导致这两个属性都包含在管理界面中
作为托管属性,但age
属性是只读的。
最后,add(int, int)
method 的@ManagedOperation
,
而dontExposeMe()
method 不是。这会导致管理界面
仅包含一个作 (add(int, int)
) 时,使用MetadataMBeanInfoAssembler
.
这AnnotationTestBean class 不需要实现任何 Java 接口,
因为 JMX 管理接口仅从 Comments 派生。 |
以下配置显示了如何配置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>
<!-- 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="jmxAttributeSource"
class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="testBean" class="org.springframework.jmx.AnnotationTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
</beans>
在前面的示例中,MetadataMBeanInfoAssembler
bean 已配置了
实例的AnnotationJmxAttributeSource
类并传递给MBeanExporter
通过 assembler 属性。这就是利用所需的全部内容
用于 Spring 公开的 MBean 的 Comments 驱动的管理界面。
Spring JMX 注释
下表描述了可在 Spring JMX 中使用的 Comments:
注解 | 适用于 | 描述 |
---|---|---|
|
类 |
标记 |
|
类 |
指示托管资源发出的 JMX 通知。 |
|
方法(仅限 getter 和 setter) |
将 getter 或 setter 标记为 JMX 属性的一半。 |
|
方法(仅限 getter) |
将 getter 标记为 JMX 属性,并添加 descriptor 属性以指示它是一个量度。 |
|
方法 |
将方法标记为 JMX作。 |
|
方法 |
定义作参数的描述。 |
下表描述了一些可用于 这些注释。有关更多详细信息,请参阅每个注释的 Javadoc。
属性 | 适用于 | 描述 |
---|---|---|
|
|
使用者 |
|
|
设置资源、通知、属性、指标或作的描述。 |
|
|
设置 |
|
|
设置 |
|
|
设置 |
|
|
设置 |
|
|
设置 |
|
|
设置 |
|
|
设置 |
|
|
设置 |
|
|
设置作参数的显示名称。 |
|
|
设置作参数的索引。 |
使用AutodetectCapableMBeanInfoAssembler
接口
为了进一步简化配置,Spring 包括AutodetectCapableMBeanInfoAssembler
接口,该接口扩展了MBeanInfoAssembler
接口添加对 MBean 资源自动检测的支持。如果配置MBeanExporter
实例为AutodetectCapableMBeanInfoAssembler
是的
允许 “投票” 包含用于 JMX 的 bean。
唯一的AutodetectCapableMBeanInfo
interface 是
这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="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource">
<bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
</property>
</bean>
<bean id="testBean" class="org.springframework.jmx.AnnotationTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
</beans>
请注意,在前面的配置中,没有 bean 传递给MBeanExporter
.
但是,AnnotationTestBean
仍然已注册,因为它使用@ManagedResource
和MetadataMBeanInfoAssembler
检测到此情况并投票包括
它。这种方法的唯一缺点是AnnotationTestBean
现在
具有商业意义。您可以通过配置ObjectNamingStrategy
如控制ObjectName
的实例
你的 Bean.您还可以看到一个使用MetadataNamingStrategy
在“使用源代码级元数据:Java 注释”中。
使用 Java 接口定义管理接口
除了MetadataMBeanInfoAssembler
,Spring 还包括InterfaceBasedMBeanInfoAssembler
,它允许您约束 methods 和
属性,这些属性根据
接口。
尽管公开 MBean 的标准机制是使用接口和简单的
命名方案 /InterfaceBasedMBeanInfoAssembler
通过以下方式扩展此功能
无需命名约定,让您使用多个接口
以及消除 bean 实现 MBean 接口的需要。
请考虑以下接口,该接口用于定义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();
}
此接口定义作为作公开的方法和属性,并且 属性。以下代码显示了如何配置 Spring JMX 以使用 此接口作为 Management Interface 的定义:
<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>
在前面的示例中,InterfaceBasedMBeanInfoAssembler
配置为使用IJmxTestBean
接口。是的
请务必了解InterfaceBasedMBeanInfoAssembler
不需要实现用于生成 JMX 管理的接口
接口。
在上述情况下,IJmxTestBean
interface 用于构建所有管理
所有 bean 的接口。在许多情况下,这不是所需的行为,您可能会
希望对不同的 bean 使用不同的接口。在这种情况下,您可以将InterfaceBasedMBeanInfoAssembler
一个Properties
实例通过interfaceMappings
属性,其中每个条目的键是 Bean 名称,每个条目的值是一个
用于该 bean 的接口名称的逗号分隔列表。
如果未通过managedInterfaces
或interfaceMappings
properties 中,InterfaceBasedMBeanInfoAssembler
反映
在 Bean 上,并使用该 Bean 实现的所有接口来创建
管理界面。
用MethodNameBasedMBeanInfoAssembler
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>
在前面的示例中,您可以看到add
和myOperation
方法作为 JMX 公开
作和getName()
,setName(String)
和getAge()
公开为
适当的 JMX 属性的一半。在上面的代码中,方法映射适用于
向 JMX 公开的 bean。要逐个 bean 控制方法公开,可以使用
这methodMappings
的属性MethodNameMBeanInfoAssembler
将 Bean 名称映射到
方法名称列表。