Essentials of JMX API - Standard MBeans

来源:互联网 发布:淘宝男装推荐 编辑:程序博客网 时间:2024/06/07 16:47

This topic introduces the fundamental notion of the java management extensions (JMX) API, namely managed beans, or MBeans.

A standard MBean is defined by writing a java interface called somethingMBean and a java class called something that implements that interface. Every method in the interface defines either an attribute or operation in the MBean. A standard MBean is composed of the MBean interface which lists the methods for all exposed attributes and operations, and the class which implements this interface and provides the functionality of the instrumented resource.

The following sections dissect an example standard MBean, and a simple JMX agent that manages the MBean. The code samples are taken from the URL: http://java.sun.com/javase/6/docs/technotes/guides/jmx/examples/jmx_examples.zip.

MBean Interface

public interface HelloMBean { 
    public void sayHello(); 
    public int add(int x, int y); 
    public String getName(); 
    public int getCacheSize();
    public void setCacheSize(int size);
}

As stated previously, by convention an MBean interface takes the name of the java class that implements it, with the suffix MBean added.

According to the JMX specification, an MBean interface consists of named and typed attributes that are readable and possibly writeable, and named and typed operations that can be invoked by applications that are managed by MBean.

Of the two attributes that are declared by HelloMBean, Name is a a read-only string, and CacheSize is an integer that can be both read and written. Getter and Setter methods are declared, to allow the managed application to access and possibly change the attribute values. As defined in JMX specification, a getter is an public method whose name being with get and which does not return value. A getter enables a manager to read the value of the attribute, whose type is that of the returned type. A setter is any public method whose name being with set and which takes a single parameter, A setter enables a manager to write new a value in the attribute, whose type is the same as that of the parameter.

Managing Resources

As presented in the Java Management Extensions (JMX) Technology Overview, once a resource  has been instrumented by MBeans, the management of that resource is performed by JMX Agent.

The core component of a JMX Agent is the MBean server, a managed object server in which MBeans are registered. a JMX Agent also includes a set of service to manage MBeans.

import java.Lang.management.*;
import javax.management.*; 
public class Main {
    public static void main(String[] args) throws Exception { 
    // Obtain MBeanServer object reference by calling the static getPlatformMBeanServer method of ManagementFactory class. The method getPlatformMBeanServer returns the platform MBeanServer. On the first call to this method, it first creates the platform MBeanServer object by calling the MBeanServerFactory.createMBeanServer method and registers the platform MXBeans in this Platform MBeanServer using the MXBean names defined in class description.
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    // Define an object name for the MBean instance it will create. Each JMX MBean must have an object name. the object name is an instance of class ObjectName, and must conform to syntax defined by JMX specification, namely it must comprise a domain, and a list of key-properties.
    ObjectName name = new ObjectName("com.example.mbeans:type=Hello"); 
    Hello mbean = new Hello(); 
    mbs.registerMBean(mbean, name);  
    System.out.println("Waiting forever..."); 
    Thread.sleep(Long.MAX_VALUE);
   }
}

原创粉丝点击