views:

625

answers:

2

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?

A: 

The problem is that I only know how to get a reference to the correct MBeanServer programmatically

If you can get the reference programmatically, why can't you wire it in through the MBeanExporter#server property?

Kevin
Yes, but I need to do it before afterPropertiesSet is called on MBeanExporter. I can accomplish this with a PostBeanProcessor, which is the solution I am leaning towards... but I feel like I am doing something that should be really, really simple in a complicated way.
waxwing
Why can't you just set the "server" property with the reference to the proper MBeanServer?
Kevin
Well, if I do it programmatically, I need some appropriate place to put the call to setServer. And that must be after Spring creates the bean, but before Spring calls afterPropertiesSet on the bean (because that's when the MBeanServer is used). Ideally I would set it in the XML, but I couldn't find a good way to do that until skaffman showed me how.
waxwing
+3  A: 

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>
skaffman
Great! This is exactly what I was looking for. I have used factory-method before, but I didn't see that it was perfect for this usage.
waxwing