EasyMBean
From jManage
[edit] IntroductionEasyMBean is a framework that allows developers to create ModelMBeans by simply applying annotations to plain Java classes. They no longer need to extend from xxxMBean interface or write numerous descriptor and meta data classes for creating ModelMBeans. The idea behind EasyMBean is quite similar to JMX support in [[1]] Spring, with one big difference - EasyMBean removes the dependence on Spring container so that it can be used with any container or even without a container. EasyMBean framework provides following features:
[edit] DownloadEasyMBean framework can be downloaded from http://sourceforge.net/projects/easymbean/. [edit] Quick Start for the impatient:To export and register your class as a MBean in a JMX MBeanServer you need to do the following: 1 Annotate your class elements with corresponding annotations -
@ManagedResource can only be used at Class level, @ManagedAttribute for a variable, @ManagedConstructor for a constructor, @ManagedOperation for a method and @ManagedNotification for an object of type Notification. 2 Create an instance of MBeanServer where you want to register your MBeans and create an instance your annotated class. 3 Call the static method getMBean of class EasyMBean passing in instance of your annotated class, the desired ObjectName instance and a reference to MBeanServer as parameters 4 The Method if successful, returns and ObjectInstance of the newly created MBean [edit] Example[edit] Step 1. Annotate the desired class with annotations:
import org.jmanage.easymbean.annotations.ManagedConstructor; import org.jmanage.easymbean.annotations.ManagedNotification; import org.jmanage.easymbean.annotations.ManagedOperation;
@ManagedAttribute(name=”testAttribute” displayName=”Test Attribute”, value=”5”, getMethod=”gettestAttribute”, setMethod=”settestAttribute”) @ManagedNotification(name=”testNotification”, type=”org.jmanage.test”) @ManagedConstructor(name=”TestClass”) @ManagedOperation(name=”testMethod”, targetClass=”TestClass”) public int gettestAttribute() { return testAttribute; } public void settestAttribute(int val) { testAttribute = val; } } [edit] Step 2: Create an instance of MBeanServer and the annotated class:
[edit] Step 3: Call the static method getMBean of class EasyMBeanpassing in instance of your annotated class, the desired ObjectName instance and a reference to MBeanServer as parameters.
[edit] Step 4: Nothing!If all goes well, your annotated class will be registered as ModelMBean in the passed in MBeanServer and an ObjectInstance to same will be returned. [edit] Understanding EasyMBean annotations for the cool headedThe org.jmanage.easymbean.annotation package defines annotations to describe ModelMBean attributes, constructors, operations, notifications and the MBean itself. The package defines following annotations: [edit] 1. @ManagedResource:Meant to be used at class level, this annotation marks the class as a ModelMBean. Any class is to be exported as MBean must be tagged by @ManagedResource annotation. If not tagged, the class will not be exported as MBean. This annotation supports the following descriptor elements (correspond directly to JMX MBean descriptors):
[edit] 2. @ManagedAttribute:This annotations defines what properties of the class are exposed as MBeanAttributes. Any variable that has to be exposed as MBeanAttribute must be tagged with this annotation. If not tagged, it will not be exposed as MBeanAttribute. Those of you who have worked with MBeans in Spring will immediately notice one difference. While in spring you associate variables with getters and setters whereas in EasyMBean you do the reverse - associate getters and setters with variables e.g. instead of doing the following (as in Spring):
you will do the following:
@ManagedAttribute(name=”name”, currencyTimeLimit=”20”, setter=”setName”)
For a detailed discussion of each, see JMX specification. [edit] 3. @ManagedOperation:To expose your methods as operations of a MBean, tag your method with @ManagedOperation annotation. If the method is not tagged by this annotation, it will not be exposed as MBeanOperation. Another important difference between Spring JMX and EasyMBean framework is the way operation parameters are declared. In Spring you will define operation parameters by @ManagedOperationParameter annotation. However, for the sake of simplicity, EasyMBean hides this from the developer. It implicitly generates descriptors by reflectively examining the method. Thus a developer simply needs to mark the method by @MangedOperation annotation and does not need to worry about parameters. EasyMBean does not provide or support user defined ManagedOperationParameters like Spring for sake of simplicity.For example:
Following descriptors are supported:
For explanation and details, refer to JMX specification. [edit] 4. @ManagedConstructor:To expose constructors of a class as JMX managed constructor, simply tag it with @ManagedConstructor annoation. This is same as annotating a method, except that parameters for annotations are different. Following descriptors are supported:
For details of each descriptor, refer to JMX specification [edit] 5. @ManagedNotification:If your class needs to emit notifications, all you need to do to expose your notification is tag them with @ManagedNotification annotation. Please note that at this point in time, EasyMBean simply generates descriptors and registers them with MBeanServer. It does either produce code to implement NotificationEmittor or NotificationBroadcaster interface nor it checks whether the annotated class implements those methods. Though desirable, I’ll add these features when I get more time! To expose a notification, declare an object of type Notification and tag it with @ManagedNotification annotation e.g.
@ManagedNotification(name=”myNotification” type=”org.jmanage.test”) Notification myNotification; Following descriptors are supported for @ManagedNotifications:
[edit] EasyMBean ClassEasyMBean class defined in org.jmanage.easymbean package is a utility class that containes only one static method called as getMBean. The Method is defined as follows: [edit] public static ObjectInstance getMBean(Object obj, ObjectName oname, MBeanServer mbs)Arguments:
Return: The objectinstance for newly created and registered MBean. Thus, once you have annotated your class, create and instance and invoke EasyMBean.getMBean method passing in the above mentioned parameters. If the call succeeds, it returns an ObjectInstance of the newly registered MBean. [edit] Rodents and PestsIf you come across any rodents and pests or feel like contacting me drop a mail at sanketsharma at gmail dot com |
