How can I get a reference to the MBeanServer?
So the question is how can we get a reference to the JBoss MBeanServer
from code within the same JVM. One thing to note here is that
we can have more than one instantiated MBeanServers inside a JVM
but we are mostly interested in getting a reference to the "jboss"
MBeanServer.
There are 2 variations to this, depending on whether or not you
want to get the reference from an MBean, or not.
Getting the reference from an MBean
If you extend ServiceMBeanSupport, then you already have a cached
reference to the MBeanServer:
public class ServiceMBeanSupport ... public MBeanServer getServer(); ...
If you don't extend ServiceMBeanSupport, you can implement the
preRegister() method of the javax.management.MBeanRegistration
interface in order to cache a reference to the MBeanServer, e.g.
(actual code taken from ServiceMBeanSupport):
... public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { this.server = server; this.serviceName = getObjectName(server, name); return this.serviceName; } ...
A third alternative, if you are wrapping your MBean or your simple POJO
implementation as an XMBean, is to provide a setter in your POJO
like:
... void setMBeanServer(MBeanServer server) { this.server = server; } ...
Then let the XMBean machinery inject to your POJO at configuration
time a reference to the MBeanServer by calling your setter method,
using an xmbean configuration that looks like:
<mbean> ... <descriptors> ... <injection id="MBeanServerType" setMethod="setMBeanServer"></injection> ... </descriptor> ... </mbean>
Getting the reference from a non-MBean
Getting the reference from a non-MBean (e.g. from an EJB or a Servlet
running inside the same JVM) is easy using the MBeanServerLocator:
import org.jboss.mx.util.MBeanServerLocator; ... // find the local MBeanServer MBeanServer server = MBeanServerLocator.locateJBoss();
Related:
HowDoIGetRemoteAccessToMyMBean
Referenced by:
Comments