本文介绍了动态更改@ManagedResource objectName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式/动态创建原型bean。我想在启动后将这些bean放在jmx控制台中。我怎么能区分它们?我正在使用anotations将我的bean添加到jmx并且我有

I am creating prototype beans programatically/dynamically. I want those beans after initiation to be in the jmx console. How I can distinguish between them? I am using anotations in order to add my beans to the jmx and I have

@ManagedResource(objectName="bean:name=MybBean")

我需要动态注入objectName。我知道怎么办呢?

I need to inject the objectName dynamically. Any idea how could I do it?

这是我的jmx配置:

<context:mbean-export server="mbeanServer" />

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" />

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
        lazy-init="false">

        <property name="beans">
            <map>
                <entry key="Server:name=HttpAdaptor">
                    <bean class="mx4j.tools.adaptor.http.HttpAdaptor">
                        <property name="port" value="8000" />
                        <property name="host" value="0.0.0.0" />
                        <property name="processor">
                            <bean class="mx4j.tools.adaptor.http.XSLTProcessor" />
                        </property>

                    </bean>
                </entry>                
            </map>
        </property>
        <property name="listeners">
            <list>
                <!--

                -->
                <bean class="com.fixgw.jmx.HttpAdaptorMgr">
                    <property name="mbeanServer" ref="mbeanServer" />
                </bean>
            </list>
        </property>
    </bean>

   <bean id="sessionMDB" class="com.fixgw.mdb.SessionMDB"
        scope="prototype" lazy-init="true">
        <constructor-arg ref="0" />
        <constructor-arg ref="0" />
    </bean>


推荐答案

您可以使用JMX命名策略来执行此操作。在工作中我们使用一个接口:

You can use a a JMX naming strategy to do this. At work we use an interface:

public interface RuntimeJmxNames {
    /** this is the name= part of the object name */
    public String getJmxName();
    /** this sets the folders as 00=FirstFolder,01=Second */
    public String[] getJmxPath();
}

我发布了实现。

然后类似下面的Spring bean:

And then something like the following Spring beans:

<bean id="jmxAttributeSource"
    class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />

<bean id="jmxAssembler"
    class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="jmxAttributeSource" />
</bean>

<bean id="jmxNamingStrategy" class="com.j256.jmx.RuntimeMetadataNamingStrategy">
    <property name="attributeSource" ref="jmxAttributeSource" />
</bean>

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="autodetect" value="true" />
    <property name="assembler" ref="jmxAssembler" />
    <property name="namingStrategy" ref="jmxNamingStrategy" />
    <property name="ensureUniqueRuntimeObjectNames" value="false" />
    <property name="excludedBeans" ref="excludedJmxBeans" />
</bean>

在您的代码中,您执行以下操作:

In your code you do something like:

@ManagedResource(objectName = "foo.com:name=replaced", description = "...")
public class Foo implements RuntimeJmxNames {
    ...
    public String getJmxName() {
        // here's where you can make the name be dynamic
        return toString();
    }
    @Override
    public String[] getJmxPath() {
        return new String[] { "folder" };
    }
}

这是关于虽然我不是100%肯定它涵盖了自定义命名的东西。

Here's the Spring documentation on JMX naming although I'm not 100% sure it covers the custom naming stuff.

此外,我的执行了虽然它的Spring支持没有很好的记录,但很多都是这样。

Also, my SimpleJMX package does a lot of this as well although its Spring support is not well documented.

这篇关于动态更改@ManagedResource objectName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 16:40