Showing posts with label MBean. Show all posts
Showing posts with label MBean. Show all posts

Wednesday, December 30, 2009

MBean Descriptors

JMX MBean/MXBean get accessed by different clients(Ex: JConsole, VisualVM, and etc.,), where it is our responsibility that to explain/descirbe the details of the Bean, it includes

  • MBean author, version and etc.,
  • Attribute display name, type, minValue, maxValue, defaultValue and recommendedValue and its behaviour
  • Operation display name, and its argument details

Java SE 6 has come up integrated way of annotation approach for all types of MBean, where we can specify the additional details, is called Descriptors.

Author.java
package com.example.mxbeans; 
 
import java.lang.annotation.Documented; 
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
import javax.management.DescriptorKey; 
 
@Documented 
@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface Author { 
    @DescriptorKey("Author") 
    String value(); 
} 

HelloWorldMXBean.java

package com.example.mxbeans; 
 
@Author("Krishna")
public interface HelloWorldMXBean { 
    @DisplayName("GETTER: Name") 
    public QueueSample getName(); 
    @DisplayName("OPERATION: clearHelloCount") 
    public void clearCount(); 
} 

In runtime these metadata/annotation converts in to JAVA object, for most constructors in the classes MBean*Info (MBeanInfo, MBeanAttributeInfo, and so on), a parallel constructor exists with the same parameters plus an additional javax.management.Descriptor parameter. The same is true for OpenMBean*InfoSupport. The MBean*Info and OpenMBean*InfoSupport classes contain a getDescriptor() method.

Read: MBean Descriptors Tutorial

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.

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