jmx动态MBean示例
- 2016-06-29 20:57:00
- admin
- 原创 2055
一、jmx动态MBean示例
代码下载:SimpleDynamic.java
import java.util.Iterator;
import javax.management.*;
public class SimpleDynamic implements DynamicMBean {
private MBeanAttributeInfo[] attrInfoArr;
private MBeanConstructorInfo[] initInfoArr;
private MBeanOperationInfo[] operInfoArr;
private MBeanInfo beanInfo;
private String className = getClass().getName();
private String classDesc = "class description.";
private static final String attr_name_State = "State";
private static final String attr_name_ChangeTimes = "ChangeTimes";
private String state = "initial state";
private int changeTimes = 0;
private int resetTimes = 0;
private void buildDynamicMBeanInfo() {
attrInfoArr = new MBeanAttributeInfo[2];
attrInfoArr[0] = new MBeanAttributeInfo(attr_name_State,
String.class.getName(), "description", true, true, false);
attrInfoArr[1] = new MBeanAttributeInfo(attr_name_ChangeTimes,
int.class.getName(), "description", true, true, false);
initInfoArr = new MBeanConstructorInfo[1];
initInfoArr[0] = new MBeanConstructorInfo(
"description", getClass().getConstructors()[0]);
operInfoArr = new MBeanOperationInfo[1];
operInfoArr[0] = new MBeanOperationInfo("reset",
"description", null, "void", MBeanOperationInfo.ACTION);
beanInfo = new MBeanInfo(className, classDesc,
attrInfoArr, initInfoArr, operInfoArr, null);
}
public SimpleDynamic() {
buildDynamicMBeanInfo();
}
public MBeanInfo getMBeanInfo() {
return beanInfo;
}
public Object getAttribute(String attribute_name)
throws AttributeNotFoundException, MBeanException, ReflectionException {
if (attribute_name.equals(attr_name_State)) {
return getState();
}
if (attribute_name.equals(attr_name_ChangeTimes)) {
return getChangeTimes();
}
throw new AttributeNotFoundException(
"Cannot find " + attribute_name + " attribute in " + className);
}
public void setAttribute(Attribute attribute)
throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
String name = attribute.getName();
Object value = attribute.getValue();
System.out.println("value: " + value);
if (name.equals(attr_name_State)) {
setState((String) value);
} else if (name.equals(attr_name_ChangeTimes)) {
// do nothing
} else {
throw new AttributeNotFoundException(
"Attribute " + name + " not found in " + className);
}
}
public AttributeList getAttributes(String[] attributeNames) {
AttributeList resultList = new AttributeList();
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) {
AttributeList resultList = new AttributeList();
for (Iterator<Object> iter = attributes.iterator(); iter.hasNext();) {
Attribute attr = (Attribute) iter.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 {
if (operationName.equals("reset")) {
reset();
return null;
} else {
throw new ReflectionException(new NoSuchMethodException(operationName),
"Cannot find the operation " + operationName + " in " + className);
}
}
public String getState() {
return state;
}
public void setState(String s) {
state = s;
changeTimes++;
}
public int getChangeTimes() {
return changeTimes;
}
public void reset() {
state = "initial state";
changeTimes = 0;
resetTimes++;
}
public int getResetTimes() {
return resetTimes;
}
}