JMX之Model MBean

Model MBean同样也是动态MBeans,可以理解为这类MBean是基于一个基本的StandardMBean,并且完全可配置的,在运行期间进行自我声明;它们为资源动态工具提供一个一般性的,有默认行为的MBean类。

这个MBean的类不需要继承任何接口

package com.jmx.modelbean.demo;

public class Hello {//don't need implement interface
	
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void printHello() {
		System.out.println("Hello World Model Bean......" + name);
	}
	
	public void printHello(String name) {
		System.out.println("Hello World Model Bean......." + name);
	}
	
	public void setName(String name) {
		this.name = name;
		System.out.println("My value is set to " + name);
	}
}

但是需要自己编写一个产生Model MBean的工具类,Model MBean使用JDK提供的RequiredModelMBean,指定基本bean(Hello),创建好需要的ModelMBeanInfo就可以了

package com.jmx.modelbean.demo;

import javax.management.Descriptor;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.modelmbean.DescriptorSupport;
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 createModelMBean() {   
		RequiredModelMBean model = null;   
		try {
			model = new RequiredModelMBean();
			//set Hello to ModelBean
			model.setManagedResource(new Hello(),"ObjectReference");
			
			//create mbean information
			ModelMBeanInfo info = createModelMBeanInfo();   
			model.setModelMBeanInfo(info);
			
		} catch (Exception e) {   
			e.printStackTrace();   
		}
		return model;   
	}

	private static ModelMBeanInfo createModelMBeanInfo() {

		//process exist attribute and method
		//create attribute of name
		Descriptor portAttrDesc = new DescriptorSupport();
        portAttrDesc.setField("name", "Name");
        portAttrDesc.setField("descriptorType", "attribute");
        portAttrDesc.setField("displayName", "Name");
        portAttrDesc.setField("getMethod", "getName");
        portAttrDesc.setField("setMethod", "setName");
		
        ModelMBeanAttributeInfo nameAttrInfo = new ModelMBeanAttributeInfo(//
                "Name", //attribute name
                STRING_CLASS, //attribute type
                "people name", //attribute desc
                READABLE, WRITABLE, !BOOLEAN,
                portAttrDesc //Descriptor
        );
        
        //getName
        Descriptor getStateDesc = new DescriptorSupport(new String[] {
          "name=getName", "descriptorType=operation", "class=com.jmx.modelbean.demo.Hello",
          "role=operation" });

        ModelMBeanOperationInfo getName = new ModelMBeanOperationInfo(//
          "getName", //
          "get name attribute", //
          null, //
          "java.lang.String", //
          MBeanOperationInfo.ACTION, //
          getStateDesc //
        );

        //setName
        Descriptor setStateDesc = new DescriptorSupport(new String[] {
          "name=setName", "descriptorType=operation", "class=com.jmx.modelbean.demo.Hello",
          "role=operation" });

        MBeanParameterInfo[] setStateParms = new MBeanParameterInfo[] { (new MBeanParameterInfo(
          "name", "java.lang.String", "new name value")) };

        ModelMBeanOperationInfo setName = new ModelMBeanOperationInfo(//
          "setName", //
          "set name attribute", //
          setStateParms, //
          "void", //
          MBeanOperationInfo.ACTION, //
          setStateDesc //
        ); 
        
		//create new method
		ModelMBeanOperationInfo print1Info = new ModelMBeanOperationInfo(
													"printHello",
													null,
													null,
													"void",
													MBeanOperationInfo.INFO,
													null);
		//create new method with params
		ModelMBeanOperationInfo print2Info;   
		MBeanParameterInfo[] param2 = new MBeanParameterInfo[1];   
		param2[0] = new MBeanParameterInfo("whoName", //param name
										STRING_CLASS, //param type
										"say hello to who");//param desc
		print2Info = new ModelMBeanOperationInfo(
							"printHello",
							null,
							param2,
							"void",
							MBeanOperationInfo.INFO,
							null);   
	
		//create ModelMBeanInfo          
		ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(//   
									RequiredModelMBean.class.getName(), //MBean
									null, //desc
									new ModelMBeanAttributeInfo[] { //attributes
										nameAttrInfo},
									null, //constructor
									new ModelMBeanOperationInfo[] { //operation
										getName,
										setName,
										print1Info, 
										print2Info},
									null, //notification
									null);//desc
		return mbeanInfo;   
	}   
}

在注册时没有什么特别之处,只是需要注意下通过工具类获得MBean即可

package com.jmx.modelbean.demo;

import java.lang.management.ManagementFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
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 MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
		
		//create mbean server
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		
		//create object name
		ObjectName helloName = new ObjectName("jmxMBean:name=hello");
		
		//receive ModelMBean by ModelMBeanUtils
		RequiredModelMBean hello = ModelMBeanUtils.createModelMBean();
		
		//create mbean and register mbean
		server.registerMBean(hello, helloName);
		
		//create adaptor, adaptor is just a form as show mbean. It has no relation to specific business mbean.
		HtmlAdaptorServer adaptor  = new HtmlAdaptorServer();
		//create adaptor name
		ObjectName adaptorName = new ObjectName("jmxAaptor:name=adaptor,port=5050");
		//register adaptor and adaptor name
		server.registerMBean(adaptor, adaptorName);
		
		adaptor.setPort(9999);
		adaptor.start();
		System.out.println("....................server start....................");
	}
}

Model MBean可以动态配置。试想一下这个应用场景:由于安全或其他原因,系统要把某个MBean公开的可管理方法隐藏起来。这时,如果你是用标准MBean,这需要修改接口类,然后重新编译发布;如果用 Apache commons-modeler(如果不想总要维护MBean这个借口,那么可以使用Apache的commons-modeler来辅助开发MBean,所有的MBean都装配在XML文件中)来写的模型MBean,则只需要修改XML文件就行了,不需要重新编译发布(可能要重启一下系统)。这就是Model Mbean 优势之所在了。
动态MBean和这一节的模型Mbean非常相似,但它们还是有很大不同的:动态MBean没有Hello类,它要自己实现Hello类中的方法逻辑。

你可能感兴趣的:(J2ee)