首页 文章

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

提问于
浏览
1

我'm new with spring and I'目前正在学习spring提供的jmx支持 . 我知道 MBeanExporter 是Spring 's JMX framework. So I was trying to play around with it. (I' m的核心类之一,遵循提供的教程here

我正在尝试使用 MBeanExporterautodetect 属性 . 但我真的不知道我是否正确理解它 .

这里的链接中的文档说明了这一点

如果启用了自动检测,则spring将自动注册有效的JMX-beans .

现在我不明白有效-jmx bean究竟是什么意思 . 我知道每个jmx-bean应该有一个object name,它应该实现一个接口,其名称应该是以"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 有关 . 如果是,那么我就顺利地为我做好准备 .

1 回答

  • 1

    阅读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实现检测这些注释 .

    为了进一步简化配置,Spring包含AutodetectCapableMBeanInfoAssembler接口,该接口扩展了MBeanInfoAssembler接口,以添加对MBean资源自动检测的支持 . 如果使用AutodetectCapableMBeanInfoAssembler的实例配置MBeanExporter,则允许对包含Bean以“暴露”到JMX进行“投票” . AutodetectCapableMBeanInfo接口的唯一实现是MetadataMBeanInfoAssembler,它投票包含任何标记有ManagedResource属性的bean . ...

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

相关问题