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); } }
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; } }
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...................."); } }