Mbeans registered to mbean server not showing up in jconsole

Prasanna picture Prasanna · Sep 15, 2011 · Viewed 11.5k times · Source

I create a mbean server using MBeanServerFactory.createMBeanServer and register mbeans with it. I can find the mbean server in jconsole but when I connect to it I do not see registered mbeans. Here is the code:

public static void main(String[] args) throws Exception
{
    MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer("example");
    ObjectName objectName = new ObjectName("example:type=simpleMbean");
    Simple simple = new Simple (1, 0);
    mbeanServer.registerMBean(simple, objectName);
    while (true)
    {
    }
}

Instead of creating a mbean server, if I using the platformMBeanServer and register my mbean to it, I can see the mbean in jconsole. Any idea what else do I need to do while doing createMBeanServer?

Answer

thusitha picture thusitha · Mar 8, 2012

I came accross this issue yesterday but managed to sort that one out. I spend some time doing that so I thought this post would be helpfull to save someone else's time.

First you can register you beans in the java code as stated in your post in the main method. But I think it is much more simpler if you use Spring.

Check this link out for more information; http://static.springsource.org/spring/docs/2.0.x/reference/jmx.html

There are some loop holes you need to avoid. Don't start two MBeans servers in your application.

I used this config file:

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=beanName" value-ref="dataSource"/>
</map>
</property>
  <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
</bean>

Use this configuration to attach a bean name to MBeanExporter. Make sure 'lazy-init' is set to false. Please note I am using this configuration in a webapplication. Web application is deployed inside tomcat. So tomcat already has MBean server. So you don't need to provide one explicitly. If you are running it in a standalone mode you need to start a MBean server and configure it accordingly.

Please also note you need add following properties inside tomcat's catalina.bat file. set CATALINA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8088 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.hostname="localhost"

Jmx connector port in this case is 8088 and hostname is 'localhost' Once you have started tomcat you need to start jconsole(I am not going to tell how to start it here) Then click on 'RemoteProcess' and type 'localhost:8088' and connect to the tomcat's MBean server. Then go to MBean tab in jconsole and you should see your MBean there.