Java – Forcing Spring’s MBeanExporter to use a particular MBeanServer

javajbossjmxmbeansspring

I have a web application running on JBoss 4.2.2. In order to monitor performance I have enabled the internal platform JMX server that ships with Java 5. In other words, I added:

-Dcom.sun.management.jmxremote

to JBoss' launch script. This works as expected. However, as a result of this, all MBeans are now registered on the platform MBeanServer. I don't want that, I want them to be registered on JBoss' MBeanServer.

The difficulty lies in the fact that I use Spring to register my managed beans. For this, MBeanExporter is used. Thus, I need to tell my MBeanExporter to use JBoss' MBeanServer when registering beans. However, the only exposed method in MBeanExporter to affect what server is used is setServer(MBeanServer mBeanServer). The problem is that I only know how to get a reference to the correct MBeanServer programmatically, and not in Spring's XML, where the MBeanExporter is declared.

My options appears to be:

  1. Write a subclass to MBeanExporter, overriding certain methods, so the correct MBeanServer is loaded
  2. Write a PostBeanProcessor that finds JBoss' MBeanServer and then calls setServer
  3. JNDI? Only works if the MBeanServer is exposed in JNDI, and I haven't been able to find it.

What is the most idiomatic way? Am I doing something really silly?

Best Answer

You can use the static factory method from the JBoss API to inject the MBeanServer into the MBeanExporter:

<bean class="org.springframework.jmx.export.MBeanExporter">
    <property name="server">
        <bean class="org.jboss.mx.util.MBeanServerLocator" factory-method="locateJBoss"/>
    </property>
    <!-- Add the rest of your MBeanExporter properties here -->
</bean>