Mbeans registered on mbean server not showing up in jconsole

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 mbeans registered. 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 use the MBeanServer platform and register my mbean, I can see mbean in jconsole. Any idea what else I need to do when creating createMBeanServer?

+4
source share
4 answers

Yesterday I came across this question, but I managed to figure it out. I spent some time on this, so I thought this post would be useful for saving another time.

First you can register you beans in java code, as indicated in the message in the main method. But I think it is much easier if you use Spring.

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

There are some holes in the loop that you need to avoid. Do not start two MBeans servers in your application.

I used this configuration 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 the bean name to MBeanExporter. Make sure the lazy-init parameter is set to false. Please note that I am using this configuration in a web application. The web application is deployed inside tomcat. So, tomcat already has an MBean server. Therefore, you do not need to explicitly specify. If you run it offline, you need to start the MBean server and configure it accordingly.

Also note that you need to add the following properties inside the tomcat 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 = "local"

The Jmx connector port in this case is 8088, and the host name is "localhost". After you start tomcat, you need to start jconsole (I'm not going to tell you how to start it here). Then click “RemoteProcess” and enter “localhost: 8088” and connect to the tomcat MBean server. Then go to the MBean tab in jconsole and you will see your MBean.

+2
source

The simplest solution is to use the MBean Server platform configured using the system properties .

So you need to create an MBeanServer instance with

 MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); 

and when starting the application, set the following system properties:

 -Dcom.sun.management.jmxremote.port=1919 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false 
+1
source

If you want to see your MBeans in JConsole, you will have to use RMI. Basically follow these steps

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

You need to use PlatformMBeanServer

 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 

ps http://www.javalobby.org/java/forums/t49130.html

0
source

All Articles