[JMX一步步來(lái)] 4、動(dòng)態(tài)MBean:DynamicMBean
文/陳剛 from www.chengang.com.cn at 2005-12-4
一、前言
動(dòng)態(tài)MBean是在運(yùn)行期才定義它的屬性和方法,也就是說(shuō)它有什么屬性和方法是可以動(dòng)態(tài)改變的。動(dòng)態(tài)MBean主要利用一些輔助類(lèi)(構(gòu)造函數(shù)類(lèi)MBeanConstructorInfo、屬性類(lèi)MBeanAttributeInfo、方法類(lèi)MBeanOperationInfo)來(lái)完成這個(gè)功能,所有的動(dòng)態(tài)MBean必須實(shí)現(xiàn)DynamicMBean接口。DynamicMBean寫(xiě)好后,使用方法和第一篇文章中普通的MBean一樣。
給出一個(gè)動(dòng)態(tài)MBean的實(shí)例,這個(gè)實(shí)例最初動(dòng)態(tài)構(gòu)了一個(gè)Name屬性及一個(gè)print方法,當(dāng)我們執(zhí)行它的print方法之后,又給此MBean新增了一個(gè)print1方法。實(shí)例的代碼如下:
二、實(shí)例
1、HelloDynamic類(lèi)
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;
/**
* @author Sunny Peng
* @author change by Chen.Gang, add a feature for dynamic add operation
* @version 1.0
*/
public class HelloDynamic implements DynamicMBean {
//這是我們的屬性名稱(chēng)
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 dynamic MBean.";
attributes = new MBeanAttributeInfo[1];
constructors = new MBeanConstructorInfo[1];
operations = new MBeanOperationInfo[1];
mBeanNotificationInfoArray = new MBeanNotificationInfo[0];
}
private void buildDynamicMBean() {
//設(shè)定構(gòu)造函數(shù)
Constructor[] thisconstructors = this.getClass().getConstructors();
constructors[0] = new MBeanConstructorInfo("HelloDynamic(): Constructs a HelloDynamic object", thisconstructors[0]);
//設(shè)定一個(gè)屬性
attributes[0] = new MBeanAttributeInfo("Name", "java.lang.String", "Name: name string.", true, true, false);
//operate method 我們的操作方法是print
MBeanParameterInfo[] params = null;//無(wú)參數(shù)
operations[0] = new MBeanOperationInfo("print", "print(): print the name", params, "void", MBeanOperationInfo.INFO);
mBeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, mBeanNotificationInfoArray);
}
//動(dòng)態(tài)增加一個(gè)print1方法
private void dynamicAddOperation() {
init();
operations = new MBeanOperationInfo[2];//設(shè)定數(shù)組為兩個(gè)
buildDynamicMBean();
operations[1] = new MBeanOperationInfo("print1", "print1(): print the name", null, "void", MBeanOperationInfo.INFO);
mBeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, mBeanNotificationInfoArray);
}
public Object getAttribute(String attribute_name) {
if (attribute_name != null)
return null;
if (attribute_name.equals("Name"))
return name;
return null;
}
public void setAttribute(Attribute attribute) {
if (attribute == null)
return;
String Name = attribute.getName();
Object value = attribute.getValue();
try {
if (Name.equals("Name")) {
// if null value, try and see if the setter returns any exception
if (value == null) {
name = null;
// if non null value, make sure it is assignable to the attribute
} else if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) {
name = (String) value;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public AttributeList getAttributes(String[] attributeNames) {
if (attributeNames == null)
return null;
AttributeList resultList = new AttributeList();
// if attributeNames is empty, return an empty result list
if (attributeNames.length == 0)
return resultList;
for (int i = 0; i < attributeNames.length; i++) {
try {
Object value = getAttribute(attributeNames[i]);
resultList.add(new Attribute(attributeNames[i], value));
} catch (Exception e) {
e.printStackTrace();
}
}
return resultList;
}
public AttributeList setAttributes(AttributeList attributes) {
if (attributes == null)
return null;
AttributeList resultList = new AttributeList();
// if attributeNames is empty, nothing more to do
if (attributes.isEmpty())
return resultList;
// for each attribute, try to set it and add to the result list if successfull
for (Iterator i = attributes.iterator(); i.hasNext();) {
Attribute attr = (Attribute) i.next();
try {
setAttribute(attr);
String name = attr.getName();
Object value = getAttribute(name);
resultList.add(new Attribute(name, value));
} catch (Exception e) {
e.printStackTrace();
}
}
return resultList;
}
public Object invoke(String operationName, Object params[], String signature[]) throws MBeanException, ReflectionException {
// Check for a recognized operation name and call the corresponding operation
if (operationName.equals("print")) {
//具體實(shí)現(xiàn)我們的操作方法print
System.out.println("Hello, " + name + ", this is HellDynamic!");
dynamicAddOperation();
return null;
} else if (operationName.equals("print1")) {
System.out.println("這是動(dòng)態(tài)增加的一方法print1");
return null;
} else {
// unrecognized operation name:
throw new ReflectionException(new NoSuchMethodException(operationName), "Cannot find the operation " + operationName + " in " + className);
}
}
public MBeanInfo getMBeanInfo() {
return mBeanInfo;
}
}
說(shuō)明:
- 實(shí)現(xiàn)于接口DynamicMBean
- 借助于各種輔助類(lèi)完成一個(gè)類(lèi)的構(gòu)造。構(gòu)造函數(shù)類(lèi)MBeanConstructorInfo、屬性類(lèi)MBeanAttributeInfo、方法類(lèi)MBeanOperationInfo
- 這里所有public方法是實(shí)現(xiàn)于DynamicMBean的。主要提供:setAttribute設(shè)置屬性、getAttribute取得屬性、setAttributes設(shè)置一組屬性、getAttributes取得一組屬性、invoke方法調(diào)用、getMBeanInfo MBeanServer由這個(gè)方法得到關(guān)鍵的MBean類(lèi)的構(gòu)造信息。
2、HelloAgent類(lèi)
前面說(shuō)了HelloDynamic和普通MBean的使用方法是一樣的,因此HelloAgent和第一篇的HelloAgent基本一樣,就是把Hello改成HelloDynamic而已。為了實(shí)例完整,也一并帖出來(lái)吧。
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class HelloAgent {
public static void main(String[] args) throws Exception {
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName helloName = new ObjectName("chengang:name=HelloDynamic");
HelloDynamic hello = new HelloDynamic();
server.registerMBean(hello, helloName);
ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
server.registerMBean(adapter, adapterName);
adapter.start();
System.out.println("start.....");
}
}
3、運(yùn)行
先運(yùn)行HelloAgent。再打開(kāi)瀏覽器,輸入網(wǎng)址:http://localhost:8082/。單擊進(jìn)入“name=HelloDynamic ”項(xiàng),執(zhí)行print方法后再回到上一頁(yè)面你會(huì)發(fā)現(xiàn)又多了一個(gè)print1方法。
4、總結(jié)
動(dòng)態(tài)MBean的代碼稍顯復(fù)雜,但對(duì)于一些特殊需求的情況,它將顯示出強(qiáng)大威力。而且它還是模型MBeans(Model MBeans)的基礎(chǔ)。不過(guò)在一般的項(xiàng)目中,動(dòng)態(tài)MBean還是用得比較少,所謂利器深藏之而不用,非常時(shí)方現(xiàn)光芒。
作者簡(jiǎn)介
陳剛,廣西桂林人,著作有《Eclipse從入門(mén)到精通》
您可以通過(guò)其博客了解更多信息和文章:http://www.chenGang.com.cn
posted on 2006-03-07 15:03 陳剛 閱讀(5999) 評(píng)論(1) 編輯 收藏 所屬分類(lèi): JMX