Search code examples
jmxmbeans

multiple mbean objects implement same interface


I want to create 2 mbeans implementing same interface. Is that legal? Say I have an interface:

interface HelloMBean
{
   void setVar();
   int getVar();
}

And I need 2 mbean with different implementation.

class Hello implements HelloMBean
{
   ...
}

class HelloAnother implements HelloMBean
{
  ...
}

Solution

  • It is not only legal, but intended to work that way.

    The interface describes what operations and attributes are available on the MBean. The implementation does the functionality.

    When you want to register the MBean in the MBeanServer, you pass the ObjectName and the implementation to the server.

    This way you can either register both Hello and HelloAnother under different ObjectNames in the MBeanServer in parallel or swap out the implementation of HelloMBean in the running server. The later is effectively what JBossAS 3+4 did for all their hot-deploying.

    The Clients of your MBean only see the "methods" from the interface and are talking to the MBeanServer which is then relaying the calls to the respective implementation.