Search code examples
javajmxmbeans

JMX MBeanInfo and Descriptors


I am struggling to understand how to take advantage/use the MBeanInfo (and related MBeanAttributeInfo, ModelMBeanInfo etc.) and related to it Descriptors.

As far as I understand MBean*Info is simply like a Class object containing information on available methods/attributes/constructors etc. Also, as far as I see it, this information is immutable and generated for us in the process of MBeanServer.registerMBean or using StandardMBean class (maybe the MBeanServer actually uses the StandardMbean class?) which uses reflection to generate this information. Is this more or less correct?

Also, I guess there exist a concept of a ModelMBean, to which we are able to pass our own MBeanInfo programmatically. What is the point of doing so if the same can be achieved via a StandardMbean with reflection?

Finally, Descriptors were put in place to allow users to extend information on the mbeans by providing (name,value) pairs in the form of descriptor. This can be attached to any of the MBean*Info classes (constructor, attribute, operation etc.) However, it can be attached during the construction of the Info object, as it is immutable. Therefore, using StandardMbean, that generates this info on its own, there is no way to inject extra information, as the descriptor we get from the MBeanInfo is immutable too... Am I missing something here? What would be the easy way to add extra information to descriptors of contructors/fields/methods on a MBean?


Solution

  • ... this information is immutable and generated for us in the process of MBeanServer.registerMBean or using StandardMBean class ... which uses reflection to generate this information. Is this more or less correct?

    Yes. The information is built about the bean and then registered with the server to it can be published to the clients. Not sure if the MBeanServer creates a StandardMBean under the covers.

    What is the point of doing so if the same can be achieved via a StandardMbean with reflection?

    The MBean*Info classes allow you to programmatically publish a JMX mapping to a concrete type without the need of the interface/impl. This allows external packages such as Spring to be able to detect and publish JMX beans that call through to discovered beans with special @ManagedResource attributes.

    This is also how my SimpleJMX package is able to publish beans with its annotations. All you need to do is add the following to one of your classes and SimpleJMX programmatically creates MbeanInfo instances which describe the class. You do not need to define a JMX interface/impl yourself.

    @JmxResource(description = "Lookup cache", domainName = "j256")
    public class LookupCache {
    
        @JmxAttributeField(description = "Number of hits in the cache")
        private int hitCount;
        ...
    

    Internally, SimpleJMX creates a instance of its class that implements javax.management.DynamicMBean. That interface is what exposes the get, set, invoke action methods. It also returns the MBeanInfo which describes the class for JMX publishing.

    Am I missing something here? What would be the easy way to add extra information to descriptors of contructors/fields/methods on a MBean?

    I don't use Descriptors with SimpleJMX so I don't know how they are used. They look to be meta information about a method or parameter such as deprecated, defaultValue, etc.. This seems to be static information so immutable as well.