Mbeans registered to 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 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 cre开发者_如何转开发ating 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?
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.
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
The easiest solution is to use the Platform MBean Server configured via system properties.
So you need to the MBeanServer
instance with
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
and set the following system properties when launching your application:
-Dcom.sun.management.jmxremote.port=1919
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
You need use PlatformMBeanServer
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ps http://www.javalobby.org/java/forums/t49130.html
精彩评论