Change @ManagedResource objectName dynamically

rayman picture rayman · Jul 16, 2012 · Viewed 8.2k times · Source

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

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

Here's my jmx configuration:

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

Answer

theJC picture theJC · Mar 21, 2013

You can do this by just implementing org.springframework.jmx.export.naming.SelfNaming:

@Component("MyPrototypeScopedBeanName")
@ManagedResource     
public class MyPrototypeScopedBeanName implements SelfNaming
{
    @Override
    public ObjectName getObjectName() throws MalformedObjectNameException {
        return new ObjectName("com.foobar", "name", this.toString());
    }
}