Version 5

    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"



    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

    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


       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:

          <injection id="MBeanServerType" setMethod="setMBeanServer"></injection>


    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:

       // find the local MBeanServer
       MBeanServer server = MBeanServerLocator.locateJBoss();








    Referenced by: