JMX的Dynamic MBean不需要自定义MBean接口,只需要实现JDK提供的DynamicMBean接口即可。Dynamic MBean没有任何明显写在代码里的属性和方法,所有的属性和方法都是通过反射结合JMX提供的辅助元数据,从而动态生成的。
Dynamic MBean:
package com.jmx.dynamic.demo;
import java.lang.reflect.Constructor;
import java.util.Iterator;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.DynamicMBean;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException;
public class HelloDynamic implements DynamicMBean {
//attributes
private String name;
private MBeanInfo mBeanInfo = null;
private String className;
private String description;
private MBeanAttributeInfo[] attributes;
private MBeanConstructorInfo[] constructors;
private MBeanOperationInfo[] operations;
MBeanNotificationInfo[] mBeanNotificationInfoArray;
public HelloDynamic() {
init();
buildDynamicMBean();
}
private void init() {
className = this.getClass().getName();
description = "Simple implementation of a MBean.";
//initial attributes
attributes = new MBeanAttributeInfo[1];
//initial constructors
constructors = new MBeanConstructorInfo[1];
//initial method
operations = new MBeanOperationInfo[1];
mBeanNotificationInfoArray = new MBeanNotificationInfo[0];
}
private void buildDynamicMBean() {
//create constructor
Co