Spring MBeanExporter - giving name to MBean

陌路散爱 提交于 2019-11-30 10:15:56

You can use the descriptions annotations provided by 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..

As a requirement, you should declare in your spring context the MBeanExporter with AnnotationJmxAttributeSource, MetadataNamingStrategy, and MetadataMBeanInfoAssembler attributes, which can be simplified like this :

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

or

<context:mbean-export />

And your managed bean should look like this :

@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 !

You can use KeyNamingStrategy to define all JMX-related properties inside XML configuration without adding any compile-time dependencies to Spring into the source code of your 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 IdentityNamingStrategy as a naming strategy for MBeanExporter and minimize the XML configuration event further:

<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"/>

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

Section 22.4 explains mechanisms available for object naming.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!