Spring 的 JMX 框架中有效 Bean 的含义以及创建用于自动检测 MBeanExporter 的有效 Bean 的方法



我是spring的新手,目前正在学习spring提供的jmx支持。我知道MBeanExporter是spring的JMX框架的核心类之一。所以我试着玩它。(我遵循这里提供的教程(

我正在尝试对MBeanExporterautodetect性质进行实验。但我真的不知道我是否理解正确。

链接中的文档显示

如果启用了autodetect,那么spring将自动注册有效的JMX bean。

现在我不明白什么是有效的jmx-bean。我知道每个jmx-bean都应该有一个对象名称,并且它应该实现一个接口,该接口的名称应该是以"MBean"为后缀的类名。我还缺少其他限制吗?

当我满足这两个限制时,MBeanExporter的自动检测功能运行良好。但我觉得使用spring必须有一些其他方法来构造有效的jmx-bean,我对此一无所知。你能给我指一下吗?

以下是代码:

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mBenaServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true"/>
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="server" ref="mBenaServer"/>
<!--<property name="beans">-->
<!--<map>-->
<!--<entry key="com.mybean:name=testBean1" value-ref="personBean"/>-->
<!--</map>-->
<!--</property>-->
<property name="autodetect" value="true"/>
</bean>
<bean id="personBean" class="com.jmx.trial.Person" lazy-init="true">
<property name="name" value="Lavish"/>
<property name="age" value="25"/>
</bean>
</beans>

PersonMBean.java

package com.jmx.trial;
public interface PersonMBean {
void setName(String name);
void setAge(int age);
String getName();
int getAge();
}

Person.java

package com.jmx.trial;
import org.springframework.jmx.export.naming.SelfNaming;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
public class Person implements PersonMBean, SelfNaming {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public ObjectName getObjectName() throws MalformedObjectNameException {
return new ObjectName("custom.bean:name=testbean");
}
}

Main.java

package com.jmx.trial;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
private static Logger logger = Logger.getLogger(Main.class);
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
Person p = (Person) context.getBean("personBean");
System.out.println(p.getName());
System.out.println(p.getAge());
logger.debug("Started, now waiting");
Thread.sleep(Long.MAX_VALUE);
}
}

我正在寻找是否有可能以上述代码之外的任何方式创建一个有效的jmx-bean。

我不知道它是否与ManagedResource有关。如果是的话,那么我希望有详细的解释。我试着在spring/docs上读到它,但对我来说并不顺利。

阅读spring文档。

Spring在传统的JMX之上添加了一层。

对于传统的JMX,MBean是一个bean,比如Foo,具有公开的属性和操作的接口FooMBean

在此上下文中,autDetect只是指在应用程序上下文中声明的任何bean上自动检测此类接口,并对其进行注册。

Spring允许任何bean作为MBean公开(它不需要接口(。相反,您可以从几种机制中选择一种来选择哪些属性/操作是公开的,但通常,您必须告诉导出器您想要公开哪些bean。您可能永远不希望应用程序上下文中的每个bean都被公开。

带注释的模型可能是最简单的(@ManagedResource@ManagedAttribute@ManagedOperation(。

框架提供的AutodetectCapableMBeanInfoAssembler实现检测这些注释。

为了进一步简化配置,Spring包含了AutodetectCapableMBeanInfoAssembler接口,它扩展了MBeanInfoAssemblyler接口,增加了对MBean资源自动检测的支持。如果使用AutodetectCapableMBeanInfoAssembler的实例配置MBeanExporter,则允许它"投票"是否包含用于向JMX公开的bean。

AutodetectCapableMBeanInfo接口的唯一实现是MetadataMBeanInfoAssembler,它投票包括任何标记有ManagedResource属性的bean。。。

但是如果你想要更多的自动检测,你可以自己写。

最新更新