在上一节是用apache的commons-modeler来开发的一个model,只不过commons-modeler帮助我们实现了很多的代码,而我们只需要写描述XML文件就行了。这一节,来一个实打实的Model Bean,不借助任何第三方工具包。例子还是沿用Hello这个类,以便于和以前的实现相比较。
一、Model MBean实例
1、Hello.java还是和以前的一样。这里它没有再加上一个MBean接口了,只是一个很普通的类。
java 代码
- public class Hello{
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void printHello() {
- System.out.println("Hello World, " + name);
- }
- public void printHello(String whoName) {
- System.out.println("Hello , " + whoName);
- }
- }
2、接下来是HelloAgent的写法,和以前就差一句:把“new Hello()”这一句删除了,加上了ModelMbeanUtils.createModlerMbean();
java 代码
- import javax.management.MBeanServer;
- import javax.management.MBeanServerFactory;
- import javax.management.ObjectName;
- import javax.management.modelmbean.RequiredModelMBean;
-
- 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=HelloWorld");
-
- RequiredModelMBean hello = ModelMBeanUtils.createModlerMBean();
- 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、ModelMbeanUtils这个类是要我们自己来实现的,也是写model Bean最麻烦的地方,它主要是返回一个RequiredModelMBean类,此类主要包括了一个ModelMBeanInfo类的信息。在 ModelMBeanInfo中定义了所有对需要管理的属性和方法的描述。具体代码如下:
java 代码
- import javax.management.MBeanParameterInfo;
- 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 ModelMBeanUtils {
- private static final boolean READABLE = true;
- private static final boolean WRITABLE = true;
- private static final boolean BOOLEAN = true;
- private static final String STRING_CLASS = "java.lang.String";
- public static RequiredModelMBean createModlerMBean() {
- RequiredModelMBean model = null;
- try {
- model = new RequiredModelMBean();
- model.setManagedResource(new Hello(), "ObjectReference");
- ModelMBeanInfo info = createModelMBeanInfo();
- model.setModelMBeanInfo(info);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return model;
- }
- private static ModelMBeanInfo createModelMBeanInfo() {
-
-
-
-
- ModelMBeanAttributeInfo nameAttrInfo = new ModelMBeanAttributeInfo(
- "Name",
- STRING_CLASS,
- "people name",
- READABLE, WRITABLE, !BOOLEAN,
- null
- );
-
-
-
-
- ModelMBeanOperationInfo print1Info = new ModelMBeanOperationInfo(
- "printHello",
- null,
- null,
- "void",
- MBeanOperationInfo.INFO,
- null
- );
-
- ModelMBeanOperationInfo print2Info;
- MBeanParameterInfo[] param2 = new MBeanParameterInfo[1];
- param2[0] = new MBeanParameterInfo("whoName", STRING_CLASS, "say hello to who");
- print2Info = new ModelMBeanOperationInfo(
- "printHello",
- null,
- param2,
- "void",
- MBeanOperationInfo.INFO,
- null
- );
-
-
-
-
- ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
- RequiredModelMBean.class.getName(),
- null,
- new ModelMBeanAttributeInfo[] {
- nameAttrInfo },
- null,
- new ModelMBeanOperationInfo[] {
- print1Info,
- print2Info },
- null,
- null
- );
- return mbeanInfo;
- }
- }
-
4、看效果的方法和以前一样,运行HelloAgent,用浏览器打开:http://localhost:8082 。效果图和standard mbean一样,就不再帖出来了,去第一篇去看吧
二、总结
我们发现模型Mbean(Model MBean)要比标准MBean(standard mbean)复杂多了,那有什么理由让我们选择使用模型MBean吗?我认为,最大的理由就是模型MBean可以动态配置。试想一下这个应用场景:由于安全或其他原因,系统要把某个MBean公开的可管理方法隐藏起来。这时,如果你是用标准MBean,这需要修改接口类,然后重新编译发布;如果用 Apache commons-modeler来写的模型MBean,则只需要修改XML文件就行了,不需要重新编译发布(可能要重启一下系统)。这就是模型Mbean 优势之所在了。
细心的人会发现动态MBean和这一节的模型Mbean非常相似,但它们还是有很大不同的:动态MBean没有Hello类,它要自己实现Hello类中的方法逻辑.