Spring MBeanExporter - 为 MBean 命名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8137983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:45:08  来源:igfitidea点击:

Spring MBeanExporter - giving name to MBean

springjmx

提问by Ivan Sopov

I'm trying to run a simple application with jmx-exported method. I do it like (spring-context and cglib for "@Configuration" are in classpath):

我正在尝试使用 jmx 导出方法运行一个简单的应用程序。我这样做(“@Configuration”的spring-context和cglib在类路径中):

package com.sopovs.moradanen.jmx;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class SpringJmxTest {
public static void main(String[] args) {
    new AnnotationConfigApplicationContext("com.sopovs.moradanen.jmx");
    while (true) {
        Thread.yield();
    }
}

@Bean
public MBeanExporter createJmxExporter() {
    return new MBeanExporter();
}

public interface FooBarMBean {
    public String hello();
}

@Component
public static class FooBar implements FooBarMBean {
    @Override
    public String hello() {
        return "Hello";
    }
}
}

However when I run it I get:javax.management.MalformedObjectNameException: Key properties cannot be empty. I tried to debug and solved it with:

但是,当我运行它时,我得到:javax.management.MalformedObjectNameException:Key 属性不能为空。我试图调试并解决它:

@Component
public static class FooBar implements FooBarMBean, SelfNaming {
    @Override
    public String hello() {
        return "Hello";
    }

    @Override
    public ObjectName getObjectName() throws MalformedObjectNameException {
        return new ObjectName("fooBar:name=" + getClass().getName());
    }
}

But is there a better way to supply a name for MBean?

但是有没有更好的方法来为 MBean 提供名称?

回答by Donatello

You can use the descriptions annotations provided by Spring Context @Managed* :

您可以使用 Spring Context @Managed* 提供的描述注释:

To do this, you must NOT implements the interface with "MBean" or "MXBean" suffix, neither SelfNaming. Then, the bean will be detected as a standard spring "managed bean" when MBeanExporter will registerBeanInstance(..), and will be converted to a ModelMBean using all spring annotations, including descriptions of attributes, operations, parameters, etc..

为此,您不得实现带有“MBean”或“MXBean”后缀的接口,也不能实现 SelfNaming。然后,当 MBeanExporter 将 registerBeanInstance(..) 时,bean 将被检测为标准 spring “托管 bean”,并将使用所有 spring 注释转换为 ModelMBean,包括属性、操作、参数等的描述。

As a requirement, you should declare in your spring context the MBeanExporterwith AnnotationJmxAttributeSource, MetadataNamingStrategy, and MetadataMBeanInfoAssemblerattributes, which can be simplified like this :

作为一项要求,您应该在 Spring 上下文中声明MBeanExporterAnnotationJmxAttributeSourceMetadataNamingStrategyMetadataMBeanInfoAssembler属性,可以像这样简化:

<bean id="mbeanExporter"
     class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter" />

or

或者

<context:mbean-export />

And your managed bean should look like this :

您的托管 bean 应如下所示:

@Component("myManagedBean")
@ManagedResource(objectName="your.domain.jmx:name=MyMBean",
                 description="My MBean goal")
public class AnnotationTestBean {

    private int age;

    @ManagedAttribute(description="The age attribute", currencyTimeLimit=15)
    public int getAge() {
        return age;
    }

    @ManagedOperation(description = "Check permissions for the given activity")
    @ManagedOperationParameters( {
        @ManagedOperationParameter(name = "activity",
                                   description = "The activity to check")
    })
    public boolean isAllowedTo(final String activity) {
        // impl
    }
}

Remember to not implements an MBean interface, which would be a StandardMBean, and SelfNaming interface, which would bypass Spring naming management !

记住不要实现 MBean 接口,这将是 StandardMBean 和 SelfNaming 接口,这将绕过 Spring 命名管理!

回答by Oleg Estekhin

You can use KeyNamingStrategyto define all JMX-related properties inside XML configuration without adding any compile-time dependencies to Spring into the source code of your MBean:

您可以使用KeyNamingStrategy在 XML 配置中定义所有与 JMX 相关的属性,而无需将任何对 Spring 的编译时依赖项添加到 MBean 的源代码中:

<bean class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
    <property name="namingStrategy" ref="namingStrategy"/>
</bean>
<bean id="namingStrategy"
        class="org.springframework.jmx.export.naming.KeyNamingStrategy">
    <property name="mappings">
        <props>
            <prop key="someSpringBean">desired.packageName:name=desiredBeanName</prop>
        </props>
    </property>
</bean>

If you can live with somewhat arbitrary object names, then you can use the IdentityNamingStrategyas a naming strategy for MBeanExporter and minimize the XML configuration event further:

如果您可以使用一些任意的对象名称,那么您可以使用IdentityNamingStrategy作为 MBeanExporter 的命名策略,并进一步最小化 XML 配置事件:

<bean class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
    <property name="namingStrategy" ref="namingStrategy"/>
</bean>
<bean id="namingStrategy"
        class="org.springframework.jmx.export.naming.IdentityNamingStrategy"/>

回答by Dave G

Check spring documentation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jmx.htmlsection 22.3.2 explains the JDK 5.0 annotations that are available.

检查 spring 文档:http: //static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jmx.html 部分 22.3.2 解释了可用的 JDK 5.0 注释。

Section 22.4 explains mechanisms available for object naming.

第 22.4 节解释了可用于对象命名的机制。