The Mystery Of Multiple MBeanServers
By daniel on Nov 25, 2008
JConsole and VisualVM have made it very simple to access MBeans in a remote target VM. There's also a lot of blogs and documents that explain how to access remote MBeans programmatically.
But how do you access local MBeans registered in your own JVM?
To access local MBeans registered in a local MBeanServer in your own JVM you need a direct reference to that MBeanServer.
A JVM can contain any number of MBeanServers, which are usually created by the MBeanServerFactory . If an MBeanServer was created using one of the MBeanServerFactory.createMBeanServer() method it can be later retrieved using MBeanServerFactory.findMBeanServer().
Some other MBeanServers might have been been created using one of the MBeanServerFactory.newMBeanServer() and in that case, the only way to get a reference of those is to obtain it from the code that created them.
Among all the MBeanServers living in a JVM, there is a special one, called the platform MBeanServer. This is the MBeanServer to which JConsole and VisualVM connect when they attach to a local process. The special thing about the platform MBeanServer is that it contains all the platform MBeans defined by the java.lang.management API. It is also the only MBeanServer which can be exposed using the out-of-the-box JMX agent.
If you want to expose another MBeanServer to a remote application, you will need to create and start your own JMXConnectorServer to expose it.
Usually, a JVM will contain a single MBeanServer, and this will be the platform MBeanServer. If you want to add your own MBeans to that MBeanServer it is very simple: you can get a direct reference to the platform MBeanServer of your JVM by calling ManagementFactory.getPlatformMBeanServer(). If you register your own MBeans in the platform MBeanServer of your JVM, they will become automatically visible to applications like JConsole and VisualVM which connect to that MBeanServer.
Some applications however may use and expose more than one MBeanServer. If you use a direct JMXServiceURL to connect to such an application with JConsole or VisualVM, you might get connected to that other MBeanServer. If you don't see any of the java.lang.management API MXBeans or if some (or all) of the usual tabs are unavailable, it's probably because you are connecting to another MBeanServer than the platform MBeanServer.
Hope this helps!