Class JMXBeanWrapper

  • All Implemented Interfaces:
    javax.management.DynamicMBean, javax.management.NotificationBroadcaster, javax.management.NotificationEmitter

    public class JMXBeanWrapper
    extends javax.management.NotificationBroadcasterSupport
    implements javax.management.DynamicMBean

    This is a wrapper class that wraps a normal Java object into a JMX dynamic bean by using Java annotations. It supports free naming and description of the bean, bean attributes, operations and operation parameters. To create a dynamic JMX bean, simply annotate your normal Java class and pass an instance of it to the constructor. This wrapper class will act as a proxy between the JMX system and your logic. Simply register the wrapped bean with JMX and you can access it through JCOnsole, JVisualVM and other JMX clients. Four annotation types are used to describe the bean:

    • JMXBean : Marks and describes a class to be used as a dynamic JMX bean.
    • JMXBeanKey : Marks the getter method used to set the key for the bean.
    • JMXBeanAttribute : Marks and describes methods (setter/getter) in a JMXBean to be used as a JMX attribute.
    • JMXBeanOperation : Marks and describes a method to be used as a JMX operation.
    • JMXBeanParameter : Describes a method parameter for JMX operation parameters

    Here is an example of an annotated class:

     @JMXBean(description = "My first JMX bean test")
     public class MyBean {
            private int level = 0;
     
            @JMXBeanKey
            public String getBeanKey() {
                    return "bean.key:name=mybean,level=" + level;
            }
     
            @JMXBeanAttribute(name = "Floor Level", description = "The current floor level")
            public int getLevel() {
                    return level;
            }
     
            @JMXBeanAttribute
            public void setLevel(int newLevel) {
                    level = newLevel;
            }
     
            @JMXBeanOperation(name = "Echo Test", description = "Echoes the parameter back to you")
            public String myMethod(
                            @JMXBeanParameter(name = "Input", description = "String of what to echo") String param) {
                    return "You said " + param;
            }
     }
     

    How to use:

     MyBean myBean = new MyBean();
     MBeanServerManager mBeanServerManager = MBeanServerManagerFactory.newInstance(dataSource.getMBeanServer(), "Demo-0", logger);
     mBeanServerManager.registerMBean(myBean);
     
    • Constructor Summary

      Constructors 
      Constructor Description
      JMXBeanWrapper​(java.lang.Object bean)
      Creates a new dynamic JMX bean on the basis of an annotated class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addRelationshipAttribute​(javax.management.Attribute attribute, java.lang.String description)
      Add relationship attribute to this bean
      java.lang.Object getAttribute​(java.lang.String attribute)  
      javax.management.AttributeList getAttributes​(java.lang.String[] attributes)  
      java.lang.Object getInnerObject()
      Access to the wrapped bean
      javax.management.MBeanInfo getMBeanInfo()  
      java.lang.Object invoke​(java.lang.String actionName, java.lang.Object[] params, java.lang.String[] signature)  
      void setAttribute​(javax.management.Attribute attribute)  
      javax.management.AttributeList setAttributes​(javax.management.AttributeList attributes)  
      java.lang.String toString()
      Joins the toString() method on the inner object and appends the toString() method of MBeanInfo
      • Methods inherited from class javax.management.NotificationBroadcasterSupport

        addNotificationListener, getNotificationInfo, handleNotification, removeNotificationListener, removeNotificationListener, sendNotification
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • JMXBeanWrapper

        public JMXBeanWrapper​(java.lang.Object bean)
                       throws java.lang.SecurityException,
                              javax.management.IntrospectionException
        Creates a new dynamic JMX bean on the basis of an annotated class.
        Parameters:
        bean - The bean object which acts as a proxy target.
        Throws:
        java.lang.SecurityException
        javax.management.IntrospectionException
    • Method Detail

      • addRelationshipAttribute

        public void addRelationshipAttribute​(javax.management.Attribute attribute,
                                             java.lang.String description)
        Add relationship attribute to this bean
        Parameters:
        attribute - Attribute key value pair of name and object it links to
        description - of this attribute that will appear in a JMX Console
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String attribute)
                                      throws javax.management.AttributeNotFoundException,
                                             javax.management.MBeanException,
                                             javax.management.ReflectionException
        Specified by:
        getAttribute in interface javax.management.DynamicMBean
        Throws:
        javax.management.AttributeNotFoundException
        javax.management.MBeanException
        javax.management.ReflectionException
      • setAttribute

        public void setAttribute​(javax.management.Attribute attribute)
                          throws javax.management.AttributeNotFoundException,
                                 javax.management.InvalidAttributeValueException,
                                 javax.management.MBeanException,
                                 javax.management.ReflectionException
        Specified by:
        setAttribute in interface javax.management.DynamicMBean
        Throws:
        javax.management.AttributeNotFoundException
        javax.management.InvalidAttributeValueException
        javax.management.MBeanException
        javax.management.ReflectionException
      • getAttributes

        public javax.management.AttributeList getAttributes​(java.lang.String[] attributes)
        Specified by:
        getAttributes in interface javax.management.DynamicMBean
      • setAttributes

        public javax.management.AttributeList setAttributes​(javax.management.AttributeList attributes)
        Specified by:
        setAttributes in interface javax.management.DynamicMBean
      • invoke

        public java.lang.Object invoke​(java.lang.String actionName,
                                       java.lang.Object[] params,
                                       java.lang.String[] signature)
                                throws javax.management.MBeanException,
                                       javax.management.ReflectionException
        Specified by:
        invoke in interface javax.management.DynamicMBean
        Throws:
        javax.management.MBeanException
        javax.management.ReflectionException
      • getMBeanInfo

        public javax.management.MBeanInfo getMBeanInfo()
        Specified by:
        getMBeanInfo in interface javax.management.DynamicMBean
      • getInnerObject

        public java.lang.Object getInnerObject()
        Access to the wrapped bean
        Returns:
        object this class wraps
      • toString

        public java.lang.String toString()
        Joins the toString() method on the inner object and appends the toString() method of MBeanInfo
        Overrides:
        toString in class java.lang.Object
        Returns:
        Joint string of wrapped object and bean info