1 Reply Latest reply on Jan 15, 2004 4:23 PM by John Emerson

    How do I create an XMBean whose resource is also an XMBean?

    John Emerson Newbie

      I want to dynamically create an XMBean whose resource object is in fact another XMBean. I started with this unit test in 3.2.3:

      jmx/src/main/test/implementation/modelmbean/XMBeanTest

      If I change the User class such that User extends XMBean, the unit test fails. How could I write a fourth test to add to XMBeanTest with a line like:

      d.setField(RESOURCE_REFERENCE, new User1());

      where User1 extends XMBean?

      Thanks in advance,
      jj

        • 1. Re: How do I create an XMBean whose resource is also an XMBe
          John Emerson Newbie

          I dug deeper and found that my question is really the same as the one described in this post:
          http://www.jboss.org/index.html?module=bb&op=viewtopic&t=43148
          The post mentions that code was sumitted to sourceforge. Juha closed the issue with this comment:

          Why are you extending RequiredModelMBean ?

          If you do that you need to ensure that ModelMBeanInfo is set
          before registering the MBean. But you're invoking the
          no-args constructor from superclass that won't set the metadata.

          The programming idiom to use model MBeans is to create an
          instance of the MMB and then configure it with a reference
          to your POJO and the metadata that describes your interface:

          ModelMBean mmb = new RequiredModelMBean();
          mmb.setModelMBeanInfo(mymetadata);
          mmb.setManagedResource("ObjectReference", mypojo);
          server.registerMBean(objectName, mmb);

          Or for XMBean:

          ModelMBean mmb = new
          XMBean("http://url.to.my.descriptor");
          server.registerMBean(objectName, mmb);

          I'll see if there's a reasonable default failure exception
          that should be thrown when you use MMB incorrectly.


          Juha asked "Why are you extending RequiredModelMBean?". I did this since my resource is composed of a class 'foo' (that extends XMBean to receive attribute change notifications) and its subclass, 'bar' that together implement the management inferface. Foo implements NoticationListener so it hears bar's attribute changes.

          I was able to get around the null pointer exception by overloading preRegister in foo:
          public ObjectName preRegister(MBeanServer ms, ObjectName objName) {
           return objName;
           }
          


          This seems to work. I get persistence, I get attribute change notifications, etc.. However, I'm concerned that this solution may be problematic in some way that I haven't seen. I'd appreciate any comment such as '"Sure, that works!" or, "No, way. You've got to reimplement that!".

          Thanks in advance,
          jj