java提供了JMX技术来管理java类。让我们可以动态管理运行中的java程序。既然是管理则应该有:管理者和被管理者之分。jmx技术中分为:管理者类MBean(也叫设备层),管理者容器(也叫管理者服务器或代理层),被管理者类。
下面是基础初探:
1.定义被管理者类:
package ex20.pyrmont.modelmbeantest;
public class Car {
private String color = "red";
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void drive() {
System.out.println("Baby you can drive my car.");
}
}
2.定义管理者类MBean
package ex20.pyrmont.modelmbeantest;
import javax.management.Attribute;
import javax.management.Descriptor;
import javax.management.MBeanException;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.RuntimeOperationsException;
import javax.management.modelmbean.DescriptorSupport;
import javax.management.modelmbean.ModelMBean;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.ModelMBeanInfoSupport;
import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.management.modelmbean.RequiredModelMBean;
public class ModelAgent {
private String MANAGED_CLASS_NAME = "ex20,pyrmont.modelmbeantest.Car";
private MBeanServer mBeanServer = null;
public ModelAgent() {
mBeanServer = MBeanServerFactory.createMBeanServer();
}
public MBeanServer getMBeanServer() {
return mBeanServer;
}
private ObjectName createObjectName(String name) {
ObjectName objectName = null;
try {
objectName = new ObjectName(name);
} catch (MalformedObjectNameException e) {
e.printStackTrace();
}
return objectName;
}
private ModelMBean createMBean(ObjectName objectName, String mbeanName) {
ModelMBeanInfo mBeanInfo = createModelMbeanInfo(objectName, mbeanName);
RequiredModelMBean modelMBean = null;
try {
modelMBean = new RequiredModelMBean(mBeanInfo);
} catch (RuntimeOperationsException e) {
e.printStackTrace();
} catch (MBeanException e) {
e.printStackTrace();
}
return modelMBean;
}
private ModelMBeanInfo createModelMbeanInfo(ObjectName objectName, String mbeanName) {
ModelMBeanInfo mBeanInfo = null;
ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[1];
ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[3];
attributes[0] = new ModelMBeanAttributeInfo("Color", "java.lang.String", "the color.", true, true, false, null);
operations[0] = new ModelMBeanOperationInfo("drive", "the drive method", null, "java.lang.String",
MBeanOperationInfo.ACTION, null);
operations[1] = new ModelMBeanOperationInfo("getColor", "get color method", null, "java.lang.String",
MBeanOperationInfo.ACTION, null);
Descriptor setColorDesc = new DescriptorSupport(new String[] { "name=setColor", "descriptorType=operation",
"class=" + MANAGED_CLASS_NAME, "role=operation" });
MBeanParameterInfo[] setColorParams = new MBeanParameterInfo[] {
(new MBeanParameterInfo("new Color", "java.lang.String", "new Color value")) };
operations[2] = new ModelMBeanOperationInfo("setColor", "set color method", setColorParams, "void",
MBeanOperationInfo.ACTION, setColorDesc);
mBeanInfo = new ModelMBeanInfoSupport(MANAGED_CLASS_NAME, null, attributes, null, operations, null);
return mBeanInfo;
}
public static void main(String[] args) {
ModelAgent modelAgent = new ModelAgent();
MBeanServer mBeanServer = modelAgent.getMBeanServer();
Car car = new Car();
String domain = mBeanServer.getDefaultDomain();
ObjectName objectName = modelAgent.createObjectName(domain + ":type=MyCar");
String mBeanName = "myMBean";
ModelMBean modelMBean = modelAgent.createMBean(objectName, mBeanName);
try {
modelMBean.setManagedResource(car, "ObjectReference");
mBeanServer.registerMBean(modelMBean, objectName);
} catch (Exception e) {
e.printStackTrace();
}
try {
Attribute attribute = new Attribute("Color", "green");
mBeanServer.setAttribute(objectName, attribute);
String color = (String) mBeanServer.getAttribute(objectName, "Color");
System.out.println("Color:" + color);
attribute = new Attribute("Color", "blue");
mBeanServer.setAttribute(objectName, attribute);
color = (String) mBeanServer.getAttribute(objectName, "Color");
System.out.println("Color:" + color);
mBeanServer.invoke(objectName, "drive", null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在管理者的Main函数中使用了JAVA提供的默认的管理者容器ModelAgent的实例,并将MBean实例注册到了ModelAgent中,注意MBean实例在ModelAgent的唯一标识为ObjectName。
运行结果:
Color:green
Color:blue
Baby you can drive my car.