3 Replies Latest reply on Oct 19, 2003 7:18 AM by Juha Lindfors

    Recommended way for MBean A to call MBean b

    paulmoore100 Newbie

      2 mbeans living on the same server need to communicate. I know I can define JMX methods and do invoke but I would like to simply get the MBean instance directly so that I can use its plain public methods (as opposed to JMX interface).

      It seems that no there is no official JMX way to do this. server.getObjectInstance doesnt actually return the real object.

      I could use the org.jboss.system.Registry and register during startService. Not sure if this is the intended use though.

      Or I could publish myself into jndi name server - but I dont want to be seen remotely.

      So what should I do?

        • 1. Re: Recommended way for MBean A to call MBean b
          Elias Ross Master


          I usually include an attribute (Instance) and create a getInstance MBean method. Have it return "this". For looser coupling, create an interface with the methods you want to expose.

          The JBoss folk seem to follow this approach.

          You can also have one mbean construct the other, and you can hold a reference in the first. (Method "registerMBean".)

          http://java.sun.com/j2ee/1.4/docs/api/javax/management/MBeanServer.html#registerMBean(java.lang.Object,%20javax.management.ObjectName)

          • 2. Re: Recommended way for MBean A to call MBean b
            Andy Godwin Newbie

            Paul

            You could use org.jboss.mx.util.MBeanProxy which gives you a
            dynamic proxy based instance of the MBean so that you can call the
            MBean methods directly on the proxy object.

            For example, if you have an MBean, say MyService, that implements
            MyServiceMBean and is registered with an MBean ObjectName of, say,
            foo:service=MyService then any other MBean can get a reference
            to this MBean and call methods on it as follows:

            ObjectName fooName = new ObjectName("foo:service=MyService");
            MyServiceMBean myServiceMBean =
            (MyServiceMbean)MBeanProxy.get(MyServiceMBean.class,
            fooName, server);
            myServiceMBean.someMethod();

            The dynamic proxy completely hides the MBeanServer invocation so that
            you can use it as if it were an instance of the MBean object.

            FWIW, there is no "official JMX" way to do this ... one of the points of
            the MBeanServer invoke() method is that the invoker doesn't have to have
            a reference to the java.lang.Class of the object that it's invoking methods
            on ... this avoids lots of problems with ClassLoaders and redeployment.

            Also, genman's comments about using interfaces is wise, especially if you
            want to be able to redeploy one MBean whilst leaving the other one running!

            HTH

            Andy

            • 3. Re: Recommended way for MBean A to call MBean b
              Juha Lindfors Master

              You will not find an "official" way to do this in the JMX spec as the main purpose of the architecture and JMX API design is to hide the actual component references to force a loosely coupled in VM relationships.

              -- Juha