Saturday, December 26, 2009

MBean vs MXBean

MBean vs MXBean

Manageable Resources are identified and instrumented as java objects, it could be Managed Bean(MBean) or advanced/extended/simplified Managed Bean(MXBean).

Managed Bean

MBeans can be any of java objects, in which we can store/retrive Serializable/Externalizable objects using methods. Based on the design pattern used in objects, we can differentiate either Standard(static) or Dynamic. Recommended to maintain Interface class name suffixed with MBean
Standard MBeans
Java objects that conform to certain design patterns derived from the JavaBeans component model.
  • fields are declared with access modifier as private, setter and getter methods with access modifier public, is called Attributes
  • Additionally, methods declared with access modifier public to do special operations, is called operations

public interface HelloWorldMBean {
//attributes
public String getName();
public void setName();

//operations
public void clearHelloCount();
public String sayHello(String name);
}
Dynamic MBeans
javax.management.DynamicMBean interface that offers more flexibility at runtime.
  • Object getAttribute(String attribute)
    Obtain the value of a specific attribute of the Dynamic MBean.
  • AttributeList getAttributes(String[] attributes)
    Get the values of several attributes of the Dynamic MBean.
  • MBeanInfo getMBeanInfo()
    Provides the exposed attributes and actions of the Dynamic MBean using an MBeanInfo object.
  • Object invoke(String actionName, Object[] params, String[] signature)
    Allows an action to be invoked on the Dynamic MBean.
  • void setAttribute(Attribute attribute)
    Set the value of a specific attribute of the Dynamic MBean.
  • AttributeList setAttributes(AttributeList attributes)
    Sets the values of several attributes of the Dynamic MBean.
Sample:
//Registering MBean in server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=HelloWorld");
Object mbean = new HelloWorld();
mbs.registerMBean(mbean, name);

//Retrieving MBean from server
HelloWorldMBean hwBean = JMX.newMBeanProxy(mbs, name, HelloWorldMBean.class);
hwBean.sayHello("JMX");

MXBean

MXBeans reference only a pre-defined set of types. Recommended to maintain Interface class name suffixed with MXBean. Implements interface of Dynamic or cusotmized JavaBean. Using MXBean, We can facilitate any client to use MBeans. Additional details required to use MXBean

  • In interface, Class name has to be suffixed with MXBean or annotation @MXBean has to be used.
  • In implementation class, annotation ConstructorProperties used to reconstruct the MXBean in retrievel time. Possible attribute names required to get from MBeanServer for this ObjectName has to be listed in constructor argument.
  • No Model-Class implementation not required to retrive pre-defined types.
  • CompositeData used to reconstruct Complex Objects.

However, in implementation class, annotation used to specify the attribute details in ConstructorProperties. And, In discovering and accessing code has the differences. Instead of directly retrieving the cusomer defined Bean, javax.management.openmbean.CompositeDataSupport has used to retrieve the data.



Sample:

public HelloWorld implements HelloWorldMXBean{
private String name;
@ConstructorProperties({"name"})
public HelloWorld(String name)
{this.name=name;
}
}

...

//Registering MXBean in server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=HelloWorld");
Object mbean = new HelloWorld();
mbs.registerMBean(mbean, name);

//Retrieving MXBean from server
//Predefined type retrieval
 String nameAttribute=mbs.getAttribute(name, "name");
 
//using CompositeData for complex java objects
CompositeData userdefinedBean = (CompositeData) mbs.getAttribute(name, "ComplexJavaObject");
String name = (String) hwBean.get("name");

//using proxy
HelloWorldMXBean proxy = JMX.newMXBeanProxy(mbs, name, HelloWorldMXBean.class);

Refer:
In java.net, What is an MXBean ? blog by Eamonn McManus.

5 comments:

Dishi said...

From where can I download JMX.jar file which gives me JMX.newMXBeanProxy(,,) mehtod. Please provide me the link or mail me the same as soon as possible on dishijain14@gmail.com.

Krishna said...

JMX class comes as part of rt.jar (JDK1.6 onwards)

Anonymous said...

what can be done if I am using dynamic bean to register my class, and need to return object. how can I used MXBean and Dynamic bean, Is it possible?

Anonymous said...

how can I used MXBean as Dynamic bean

Anonymous said...

Please see MXBean section

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com