JMX理解与实例

JMX解决的问题:

Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (e. g. printers) and service oriented networks. Those resources are represented by objects called MBeans (for Managed Bean). In the API, classes can be dynamically loaded and instantiated. Managing and monitoring applications can be designed and developed by Java Dynamic Management Kit.

JMX中MBean有DynamicMBean和StandardMBean两类,前者动态组装一个监控对象,较为复杂,但更为灵活,不需要监控类继承*MBean接口,后者简单。

架构图如下:

JMX架构

以下为一简单实例。

先抽象需要监控的对象TestMBean,注意该接口的命名必须是:监控的对象类名+MBean

package loci.jmx;
/**
*
@author yaohw
*
*/
public interface TestBeanMBean {

public Object fun(Object para);

public String getField();

public void setField(String field);

public Object fun();
}

监控的对象TestBean

package loci.jmx;

/**
*
@author yaohw
*
*/
public class TestBean implements TestBeanMBean {

private String field;

TestBean(String field){
this.field = field;
}

public Object fun(){
System.out.println("TestMBean-field:"+field);
return field;
}
public Object fun(Object para){
System.out.println("TestMBean-field:"+field);
System.out.println("TestMBean-para:"+para);
return field;
}
/**
*
@return the field
*/
public String getField() {
System.out.println("get field!"+field);
return field;
}
/**
*
@param field the field to set
*/
public void setField(String field) {
System.out.println("set field!"+field);
this.field = field;
}
}

接着实现代理层

当通过RMI实现监控管理时,引入jmxremoting.jar.实现如下。注意若想在jconsole控制台中监控,则修改实例化的MBeanServer

package loci.jmx;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.comm.RmiConnectorServer;

/**
*
@author yaohw
*
*/
public class RMIAgent {

public static void main(String[] args) {

/**
* normal use RMI in APP
*/
// MBeanServer mbs = MBeanServerFactory.createMBeanServer("RMIAgent");

/**
* 使用Jconsole
*/
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();


RmiConnectorServer connector = new RmiConnectorServer();
ObjectName connectorName = null;
try {
connectorName = new ObjectName("RMIAgent:name=RMIConnector");
mbs.registerMBean(connector, connectorName);
TestBean mbean = new TestBean("hello JMX from server!");
ObjectName testMBeanName = new ObjectName("RMIAgent:name=testMBean");

mbs.registerMBean(mbean, testMBeanName);
connector.start();
} catch (Exception e) {
e.printStackTrace();
}

}
}

监控对象测试代码RMIManager.java为:

package loci.jmx;

import javax.management.Attribute;
import javax.management.ObjectName;
import com.sun.jdmk.comm.RmiConnectorAddress;
import com.sun.jdmk.comm.RmiConnectorClient;

/**
*
@author yaohw
*
*/
public class RMIManager {

public static void main(String[] args) {
RmiConnectorClient client = new RmiConnectorClient();
RmiConnectorAddress address = new RmiConnectorAddress();
try {
client.connect(address);
ObjectName testMBeanName = ObjectName.getInstance("RMIAgent:name=testMBean");
client.invoke(testMBeanName, "fun", null, null);
client.setAttribute(testMBeanName, new Attribute("Field", new String(
"hello JMX from manager!")));
System.out.println(client.invoke(testMBeanName, "fun", null, null));
} catch (Exception e) {
e.printStackTrace();
} finally {

}
}
}



当选择HTTP协议来监控管理对象时,引入jmxtools.jar。实现如下:

package loci.jmx;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

/**
*
@author yaohw
*
*/
public class HTMLAgent {

private MBeanServer mbs = null;

public HTMLAgent() {

// create a MBeanServer
mbs = MBeanServerFactory.createMBeanServer("HTTPAgent");

// create an adapter
HtmlAdaptorServer adapter = new HtmlAdaptorServer();

// create a MBean
TestBean mbean = new TestBean("hello boys!");

ObjectName adapterName = null;
ObjectName helloWorldName = null;

try {
adapterName = new ObjectName("HTTPAgent:name=htmladaffpter,port=9092");
// regisetr the adapter to the MBeanServer
mbs.registerMBean(adapter, adapterName);
// declare the port which the adapter user
adapter.setPort(9092);
// start the adapter
adapter.start();

helloWorldName = new ObjectName("HTTPAgent:name=testMBean");
mbs.registerMBean(mbean, helloWorldName);

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new HTMLAgent();
}
}


此时监控可以直接使用浏览器了。访问:http://localhost:9092/ 即会出现相关管理界面。

附:动态MBean实现时实例代码(From Infinispan)

View Code
 1 public class ResourceDMBean implements DynamicMBean {
2 private static final Class<?>[] primitives = {int.class, byte.class, short.class, long.class,
3 float.class, double.class, boolean.class, char.class};
4
5 private static final Class<?>[] primitiveArrays = {int[].class, byte[].class, short[].class, long[].class,
6 float[].class, double[].class, boolean[].class, char[].class};
7
8 private static final String MBEAN_DESCRITION = "Dynamic MBean Description";
9
10 private static final Log log = LogFactory.getLog(ResourceDMBean.class);
11 private final Object obj;
12 private String description = "";
13
14 private final MBeanAttributeInfo[] attrInfo;
15 private final MBeanOperationInfo[] opInfos;
16
17 private final HashMap<String, AttributeEntry> atts = new HashMap<String, AttributeEntry>();
18 private final List<MBeanOperationInfo> ops = new ArrayList<MBeanOperationInfo>();
19
20 public ResourceDMBean(Object instance) {
21
22 if (instance == null)
23 throw new NullPointerException("Cannot make an MBean wrapper for null instance");
24
25 this.obj = instance;
26 findDescription();
27 findFields();
28 findMethods();
29
30 attrInfo = new MBeanAttributeInfo[atts.size()];
31 int i = 0;
32
33 MBeanAttributeInfo info;
34 for (AttributeEntry entry : atts.values()) {
35 info = entry.getInfo();
36 attrInfo[i++] = info;
37 if (log.isTraceEnabled()) {
38 log.tracef("Attribute %s [r=%b,w=%b,is=%b,type=%s]", info.getName(),
39 info.isReadable(), info.isWritable(), info.isIs(), info.getType());
40 }
41 }
42
43 opInfos = new MBeanOperationInfo[ops.size()];
44 ops.toArray(opInfos);
45
46 if (log.isTraceEnabled()) {
47 if (!ops.isEmpty())
48 log.trace("Operations are:");
49 for (MBeanOperationInfo op : opInfos) {
50 log.tracef("Operation %s %s", op.getReturnType(), op.getName());
51 }
52 }
53 }
54
55 Object getObject() {
56 return obj;
57 }
58 ……




posted @ 2012-02-08 12:43  echx  阅读(3884)  评论(3编辑  收藏  举报