动态MBean

一共有四种MBean:

  1. 标准MBeans(Standard MBeans)设计和实现是最简单的,这类MBean使用自己的方法名作为管理接口;——在前一篇中的Hello、HelloMBean就是一个标准MBeans(Standard MBeans)
  2. 动态MBeans(Dynamic MBeans)必须实现一个指定的接口,由于动态MBeans在运行期间暴露它们的管理接口,因此更为灵活;
  3. 开放MBeans(Open MBeans)属于动态MBeans,这类MBean依靠基础数据类型来实现通用管理,并为友情用户进行自我声明;
  4. 模型MBeans(Model MBeans)同样也是动态MBeans,这类MBeans是完全可配置的,在运行期间进行自我声明;它们为资源动态工具提供一个一般性的,有默认行为的MBeans类。

动态MBean是在运行期才定义它的属性和方法,也就是说它有什么属性和方法是可以动态改变的。

动态MBean主要利用一些辅助类(构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类MBeanOperationInfo)来完成这个功能,所有的动态MBean必须实现DynamicMBean接口。

DynamicMBean写好后,使用方法和第一篇文章中普通的MBean一样。


1)编写动态MBean

[java]  view plain  copy
  1. public class HelloDynamic implements DynamicMBean {     
  2.     //这是我们的属性名称     
  3.     private String name;   
  4.       
  5.     private MBeanInfo mBeanInfo = null;     
  6.     private String className;     
  7.     private String description;     
  8.     private MBeanAttributeInfo[] attributes;     
  9.     private MBeanConstructorInfo[] constructors;     
  10.     private MBeanOperationInfo[] operations;     
  11.     MBeanNotificationInfo[] mBeanNotificationInfoArray;     
  12.     
  13.     public HelloDynamic() {     
  14.         init();     
  15.         buildDynamicMBean();     
  16.     }     
  17.     
  18.     private void init() {     
  19.         className = this.getClass().getName();     
  20.         description = "Simple implementation of a dynamic MBean.";     
  21.         attributes = new MBeanAttributeInfo[1];     
  22.         constructors = new MBeanConstructorInfo[1];     
  23.         operations = new MBeanOperationInfo[1];     
  24.         mBeanNotificationInfoArray = new MBeanNotificationInfo[0];     
  25.     }     
  26.     
  27.     private void buildDynamicMBean() {     
  28.         //设定构造函数     
  29.         Constructor[] thisconstructors = this.getClass().getConstructors();     
  30.         constructors[0] = new MBeanConstructorInfo(  
  31.                 "HelloDynamic(): Constructs a HelloDynamic object",   
  32.                 thisconstructors[0]);   
  33.           
  34.         //设定一个属性     
  35.         attributes[0] = new MBeanAttributeInfo(  
  36.                 "Name",   
  37.                 "java.lang.String",   
  38.                 "Name: name string."//String description,  
  39.                 truetruefalse);   //boolean isReadable,boolean isWritable,boolean isIs  
  40.   
  41.         //operate method 我们的操作方法是print     
  42.         MBeanParameterInfo[] params = null;//无参数     
  43.         operations[0] = new MBeanOperationInfo(  
  44.                 "print",   
  45.                 "print(): print the name"//String description,  
  46.                 params,   
  47.                 "void",   
  48.                 MBeanOperationInfo.INFO);     
  49.           
  50.         mBeanInfo = new MBeanInfo(  
  51.                 className,   
  52.                 description,   
  53.                 attributes,   
  54.                 constructors,   
  55.                 operations,   
  56.                 mBeanNotificationInfoArray);     
  57.     }     
  58.     
  59.     //动态增加一个print1方法     
  60.     private void dynamicAddOperation() {     
  61.         init();     
  62.         operations = new MBeanOperationInfo[2];//设定数组为两个     
  63.         buildDynamicMBean();     
  64.         operations[1] = new MBeanOperationInfo(  
  65.                 "print1",   
  66.                 "print1(): print the name",   
  67.                 null,   
  68.                 "void",  
  69.                 MBeanOperationInfo.INFO);     
  70.         mBeanInfo = new MBeanInfo(  
  71.                 className,   
  72.                 description,   
  73.                 attributes,   
  74.                 constructors,   
  75.                 operations,   
  76.                 mBeanNotificationInfoArray);     
  77.     }     
  78.     
  79.     @Override  
  80.     public Object getAttribute(String attribute_name) {     
  81.         if (attribute_name != null)     
  82.             return null;     
  83.         if (attribute_name.equals("Name"))     
  84.             return name;     
  85.         return null;     
  86.     }     
  87.     @Override  
  88.     public void setAttribute(Attribute attribute) {     
  89.         if (attribute == null)     
  90.             return;     
  91.         String Name = attribute.getName();     
  92.         Object value = attribute.getValue();     
  93.         try {     
  94.             if (Name.equals("Name")) {     
  95.                 // if null value, try and see if the setter returns any exception     
  96.                 if (value == null) {     
  97.                     name = null;     
  98.                     // if non null value, make sure it is assignable to the attribute     
  99.                 } else if ((Class.forName("java.lang.String"))  
  100.                             .isAssignableFrom(value.getClass())) {     
  101.                     name = (String) value;     
  102.                 }     
  103.             }     
  104.         } catch (Exception e) {     
  105.             e.printStackTrace();     
  106.         }     
  107.     }     
  108.     @Override  
  109.     public AttributeList getAttributes(String[] attributeNames) {     
  110.         if (attributeNames == null)     
  111.             return null;     
  112.         AttributeList resultList = new AttributeList();     
  113.         // if attributeNames is empty, return an empty result list     
  114.         if (attributeNames.length == 0)     
  115.             return resultList;     
  116.         for (int i = 0; i < attributeNames.length; i++) {     
  117.             try {     
  118.                 Object value = getAttribute(attributeNames[i]);     
  119.                 resultList.add(new Attribute(attributeNames[i], value));     
  120.             } catch (Exception e) {     
  121.                 e.printStackTrace();     
  122.             }     
  123.         }     
  124.         return resultList;     
  125.     }     
  126.     @Override  
  127.     public AttributeList setAttributes(AttributeList attributes) {     
  128.         if (attributes == null)     
  129.             return null;     
  130.         AttributeList resultList = new AttributeList();     
  131.         // if attributeNames is empty, nothing more to do     
  132.         if (attributes.isEmpty())     
  133.             return resultList;     
  134.         // for each attribute, try to set it and add to the result list if successfull     
  135.         for (Iterator i = attributes.iterator(); i.hasNext();) {     
  136.             Attribute attr = (Attribute) i.next();     
  137.             try {     
  138.                 setAttribute(attr);     
  139.                 String name = attr.getName();     
  140.                 Object value = getAttribute(name);     
  141.                 resultList.add(new Attribute(name, value));     
  142.             } catch (Exception e) {     
  143.                 e.printStackTrace();     
  144.             }     
  145.         }     
  146.         return resultList;     
  147.     }     
  148.     
  149.     @Override  
  150.     public Object invoke(String operationName, Object params[], String signature[])   
  151.             throws MBeanException, ReflectionException {     
  152.         // Check for a recognized operation name and call the corresponding operation     
  153.         if (operationName.equals("print")) {     
  154.             //=======具体实现我们的操作方法print =======  
  155.             System.out.println("Hello, " + name + ", this is HellDynamic!");     
  156.             dynamicAddOperation();     
  157.             System.out.println("added a dynamic operation(print1)!");  
  158.             return null;     
  159.         } else if (operationName.equals("print1")) {     
  160.             System.out.println("这是动态增加的一方法print1");     
  161.             return null;     
  162.         } else {     
  163.             // unrecognized operation name:     
  164.             throw new ReflectionException(new NoSuchMethodException(operationName),  
  165.                     "Cannot find the operation " + operationName + " in " + className);     
  166.         }     
  167.     
  168.     }     
  169.     
  170.     @Override  
  171.     public MBeanInfo getMBeanInfo() {     
  172.         return mBeanInfo;     
  173.     }     
  174. }     

2)编写Agent

前面说了HelloDynamic和普通MBean的使用方法是一样的,因此HelloAgent和第一篇的HelloAgent基本一样,就是把Hello改成HelloDynamic而已。

[java]  view plain  copy
  1. public class HelloAgent {  
  2.      public static void main(String[] args) throws Exception {   
  3.               
  4.             //先创建了一个MBeanServer,用来做MBean的容器  
  5. //          MBeanServer server = MBeanServerFactory.createMBeanServer();     
  6.             MBeanServer server = ManagementFactory.getPlatformMBeanServer();     
  7.             ObjectName helloName = new ObjectName("alpha-dynamic:name=HelloWorld");    
  8.             HelloDynamic hello=new HelloDynamic();     
  9.             //将Hello这个类注入到MBeanServer中,注入需要创建一个ObjectName类  
  10.             server.registerMBean(hello, helloName);     
  11.               
  12.             //创建一个AdaptorServer,这个类将决定MBean的管理界面,这里用最普通的Html型界面。AdaptorServer其实也是一个MBean。  
  13.             // alpha:name=HelloWorld的名字是有一定规则的,格式为:“域名:name=MBean名称”,域名和MBean名称都可以任意取。  
  14.             ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");     
  15.             HtmlAdaptorServer adapter = new HtmlAdaptorServer();     
  16.             server.registerMBean(adapter, adapterName);     
  17.             adapter.start();     
  18.             System.out.println("start.....");     
  19.         }     
  20. }  


3)运行

先运行HelloAgent。

再打开浏览器,输入网址:http://localhost:8082/。单击进入“name=HelloDynamic ”项,执行print方法后再回到上一页面你会发现又多了一个print1方法。

[plain]  view plain  copy
  1. start.....  
  2. Hello, null, this is HellDynamic!  
  3. added a dynamic operation(print1)!  
  4. 这是动态增加的一方法print1  

4)总结

动态MBean的代码稍显复杂,但对于一些特殊需求的情况,它将显示出强大威力。而且它还是模型MBeans(Model MBeans)的基础。不过在一般的项目中,动态MBean还是用得比较少,所谓利器深藏之而不用,非常时方现光芒。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值