spring 的 JMX 框架中有效 bean 的含义以及为自动检测 MBeanExporter 创建有效 bean 的方法

meaning of valid bean in spring's JMX framework and ways to create a valid bean for autodetect of MBeanExporter

我是 spring 的新手,目前正在学习 spring 提供的 jmx 支持。我了解到 MBeanExporter 是 spring 的 JMX 框架的核心 classes 之一。所以我试着玩弄它。 (我正在按照提供的教程进行操作 here

我正在尝试使用 MBeanExporterautodetect 属性。但是我真的不知道我理解的对不对。

link 这里的文档说

If autodetect is enabled, then valid JMX-beans will automatically be registered by spring.

现在我不明白 valid-jmx bean 的实际含义。我知道每个 jmx-bean 都应该有一个 object name 并且它应该实现一个接口,其名称应该是 class 的名称加上 "MBean" 后缀。我还缺少任何其他限制吗?

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

代码如下:

应用-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 上阅读它,但对我来说并不顺利。

阅读the spring documentation

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

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

autDetect 在此上下文中只是意味着自动检测在应用程序上下文中声明的任何 bean 上的此类接口,并注册它们。

Spring 允许 any bean 作为 MBean 公开(它不需要接口)。相反,您选择 several mechanisms to select which attributes/operations are exposed 之一,但通常,您必须告诉导出器您想要公开哪些 bean。您可能永远不想公开应用程序上下文中的每个 bean。

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

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

To simplify configuration even further, Spring includes 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, it is allowed to “vote” on the inclusion of beans for exposure to JMX.

The only implementation of the AutodetectCapableMBeanInfo interface is the MetadataMBeanInfoAssembler, which votes to include any bean that is marked with the ManagedResource attribute. ...

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