standard MBean适合管理新资源或未知,静态资源。随着资源演进,API会随着每次发布而改变。Dynamic MBean则比较适合处理这种情况,因为它在运行时决定管理接口。Dynamic MBean使用class元数据来描述管理接口。
JMX agent识别Dynamic MBean,是因为它们必须实现javax.management.DynamicMBean接口。
invoke()方法有三个参数:string,方法的名称。Object的数组(方法的参数值)和string数组(参数类型的字符串)
JMX agent识别Dynamic MBean,是因为它们必须实现javax.management.DynamicMBean接口。
package javax.management;
public interface DynamicMBean{
public Object getAttribute( String attribute )
throws AttributeNotFoundException, MBeanException,
ReflectionException;
public void setAttribute( Attribute attribute )
throws AttributeNotFoundException,
InvalidAttributeValueException,MBeanException,
ReflectionException;
public AttributeList getAttributes( String[] attributes );
public AttributeList setAttributes( AttributeList attributes );
public Object invoke( String actionName, Object[] params,String[] signature )
throws MBeanException,ReflectionExceptionn
public MBeanInfo getMBeanInfo();
}
DynamicMBean接口使用MBeanInfo类的方式暴露资源的管理接口。JMX agent使用getMBeanInfo()方法来获取Dynamic MBean管理接口描述。MBeanInfo对象是standard JMX对象(描述MBean管理接口的各部分)的容器。
getAttribute()方法返回类型为Object对象,它简单的封装了暴露的属性实际的值。getAttributes()返回AttributeList类,它是ArrayList的一个子类,是Attribute对象的集合。每个Attribute对象封装了attribute的名称和值。invoke()方法有三个参数:string,方法的名称。Object的数组(方法的参数值)和string数组(参数类型的字符串)
MBeanInfo类是描述MBean管理接口对象容器。MBean的元数据类被Dynamic MBean、MBean server创建。MBean server内部使用这些对象(不用区分MBean类型)。
MBeanFeatureInfo是所有metaclass的基类,MBeanParameterInfo是辅助类。每个MBean元数据类是MBeanFeatureInfo的子类,它包含一个name和description。MBeanParameterInfo提供了构造器或操作的参数描述。
public MBeanConstructorInfo( String description,
java.lang.reflect.Constructor constructor );
public MBeanConstructorInfo( String name, String description,
MBeanParameterInfo[] signature )
public MBeanAttributeInfo( String name, String description,
java.lang.reflect.Method getter,
java.lang.reflect.Method setter)
public MBeanAttributeInfo( String name, String type, String
description, boolean isReadable,
boolean isWritable, boolean isIs)
MBeanAttributeInfo的getType()方法返回属性的类型,isReadable()属性是否可读,isWritable()属性是否可写,isIs()。
public MBeanOperationInfo( String description,
java.lang.reflect.Method method)
public MBeanOperationInfo( String name, String description,
MBeanParameterInfo[] signature,
String type, int impact)
MBeanOperationInfo定义了public static final成员变量,这些值描述了invoke的特定操作的impact。
INFO:操作返回信息
ACTION:操作导致MBean的变更或action。
ACTION_INFO:操作导致INFO和ACTION的影响。
UNKNOW:未知的。
构建Dynamic MBean的MBeanInfo信息:
private void buildDynamicMBean() {
// 设定构造函数
Constructor[] thisconstructors = this.getClass().getConstructors();
constructors[0] = new MBeanConstructorInfo(
"HelloDynamic(): Constructs a HelloDynamic object",
thisconstructors[0]);
// 设定一个属性
attributes[0] = new MBeanAttributeInfo("Name", "java.lang.String",
"Name: name string.", true, true, false);
// operate method 我们的操作方法是print
MBeanParameterInfo[] params = null;// 无参数
operations[0] = new MBeanOperationInfo("print",
"print(): print the name", params, "void",
MBeanOperationInfo.INFO);
mBeanInfo = new MBeanInfo(className, description, attributes,
constructors, operations, mBeanNotificationInfoArray);
}
方法调用:
public Object invoke(String actionName, Object[] params, String[] signature)
throws MBeanException, ReflectionException {
if (actionName.equals("print")) {
// 具体实现我们的操作方法print
System.out.println("Hello, " + name + ", this is HellDynamic!");
dynamicAddOperation();
return null;
} else if (actionName.equals("print1")) {
System.out.println("这是动态增加的一方法print1");
return null;
} else {
// unrecognized operation name:
throw new ReflectionException(
new NoSuchMethodException(actionName),
"Cannot find the operation " + actionName + " in "
+ className);
}
}