Class JMXBeanWrapper

  • All Implemented Interfaces:,,

    public class JMXBeanWrapper

    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;
            public String getBeanKey() {
                    return "bean.key:name=mybean,level=" + level;
            @JMXBeanAttribute(name = "Floor Level", description = "The current floor level")
            public int getLevel() {
                    return level;
            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);
    • Constructor Summary

      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​( attribute, java.lang.String description)
      Add relationship attribute to this bean
      java.lang.Object getAttribute​(java.lang.String attribute) getAttributes​(java.lang.String[] attributes)  
      java.lang.Object getInnerObject()
      Access to the wrapped bean getMBeanInfo()  
      java.lang.Object invoke​(java.lang.String actionName, java.lang.Object[] params, java.lang.String[] signature)  
      void setAttribute​( attribute) setAttributes​( attributes)  
      java.lang.String toString()
      Joins the toString() method on the inner object and appends the toString() method of MBeanInfo
      • Methods inherited from class

        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,
        Creates a new dynamic JMX bean on the basis of an annotated class.
        bean - The bean object which acts as a proxy target.
    • Method Detail

      • addRelationshipAttribute

        public void addRelationshipAttribute​( attribute,
                                             java.lang.String description)
        Add relationship attribute to this bean
        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)
        Specified by:
        getAttribute in interface
      • setAttribute

        public void setAttribute​( attribute)
        Specified by:
        setAttribute in interface
      • getAttributes

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

        public setAttributes​( attributes)
        Specified by:
        setAttributes in interface
      • invoke

        public java.lang.Object invoke​(java.lang.String actionName,
                                       java.lang.Object[] params,
                                       java.lang.String[] signature)
        Specified by:
        invoke in interface
      • getMBeanInfo

        public getMBeanInfo()
        Specified by:
        getMBeanInfo in interface
      • getInnerObject

        public java.lang.Object getInnerObject()
        Access to the wrapped bean
        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
        toString in class java.lang.Object
        Joint string of wrapped object and bean info