Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mbeans registered to mbean server not showing up in jconsole

Tags:

java

jmx

mbeans

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?

like image 969
Prasanna Avatar asked Sep 14 '11 23:09

Prasanna


People also ask

What is MBeans in Jconsole?

An MBean is a managed Java object, similar to a JavaBeans component, that follows the design patterns set forth in the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed.

What is MBean server?

An MBean server is a repository of MBeans that provides management applications access to MBeans.

What is Spring boot MBean?

The automatic registration of any Spring bean as a JMX MBean. A flexible mechanism for controlling the management interface of your beans. The declarative exposure of MBeans over remote, JSR-160 connectors.


2 Answers

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.

like image 145
thusitha Avatar answered Sep 30 '22 08:09

thusitha


If you want to see your MBeans in the JConsole, you will have to use RMI. Basically, do the following

Registry registry = LocateRegistry.createRegistry(RMI_REGISTRY_PORT);
//... create your MBean Server here and add your MBeans...
Map<String, Object> env = new HashMap<String, Object>(); //Add authenticators and stuff to the environment.    

//Create a URL from which your beans will be accessible.    
JMXServiceURL jmxServiceURL = new JMXServiceURL("rmi", 
                                                "localhost", 
                                                CONNECTOR_SERVER_PORT, 
                                                "/jndi/rmi://localhost:" + RMI_REGISTRY_PORT + "myApp");

//Start the connector server
JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, env, server);
System.out.println(jmxServiceURL); //Use this URL to connect through JConsole, instead of selecting Local Process, just select the Remote process
like image 22
r00k Avatar answered Sep 30 '22 09:09

r00k