当前位置:编程学习 > JAVA >>

JMX分析3-MXBean及OpenMBean

 MXBean跟标准MBean很像,标准MBean需要实现XXXXMBean这样命名的接口,而MXBean则需要实现XXXXMXBean这样命名的接口,也可以在接口上使用注解@MXBean,而不用强制使用XXXMXBean这样的命名格式。但是MXBean有点在于它可以供任何的client,包括remote client访问相关属性和执行相关操作。并且client不需要具有MXBean类(e.g. 在JConsole中,MBean类型也可以供remote client访问,基本类型是可以展示的,但是一旦有复杂类型,那就不能显示了)。为了满足这种机制,JMX提供了一套Open type-Open value用于双方交互。以使耦合度减少。VM的很多属性都是通过MXBean的形式提供的。
例子:
代码:ZooMXBean,MXBean接口
 1 package test.jmx.mxbean.易做图;
 2
 3 public inte易做图ce ZooMXBean {
 4    
 5     public Tiger getTiger();
 6    
 7     public void addTiger(Tiger tiger);
 8    
 9     public String getZooName();
10    
11     public int getTigerCount();
12 }
13

代码:ZooImpl,MXBean的实现类
 1 package test.jmx.mxbean.易做图;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5
 6 public class ZooImpl implements ZooMXBean {
 7
 8     private String zooName = " China zoo";
 9     private static List<Tiger> list;
10     static {
11         //初始化一只Tiger
12         Tiger tiger = new Tiger(" the first tiger");
13         list = new ArrayList<Tiger>();
14         list.add(tiger);
15     }
16     public void addTiger(Tiger tiger) {
17         list.add(tiger);
18     }
19
20     public Tiger getTiger() {
21         return list.get(0);
22     }
23
24     public int getTigerCount(){
25         return list.size();
26     }
27    
28     public String getZooName() {
29         return zooName;
30     }
31    
32     public String[] getAnimalNames(){
33         return new String[]{"bird","tiger","mouse"};
34     };
35 }
36

代码:Tiger,复杂的类型(不同于java基本类型)
 1 package test.jmx.mxbean.易做图;
 2
 3 import java.beans.ConstructorProperties;
 4
 5
 6 public class Tiger {
 7    
 8     private String name;
 9     @ConstructorProperties({})
10     public Tiger(){
11         this.name = "the default constructor";
12     }
13    
14     @ConstructorProperties({"name"})
15     public Tiger(String name){
16         this.name = name;
17     }
18    
19     public String getName(){
20         return name;
21     }
22    
23     public String roar(){
24         return "@¥%%……";
25     }
26    
27     public void setName(String name){
28         this.name=name;
29     }
30     public String[] getFoodNames(){
31         return new String[]{"rabbit","sheep","pig"};
32     }
33 }
34

代码:Server
 1 package test.jmx.mxbean.易做图;
 2
 3 import java.lang.management.ManagementFactory;
 4 import java.rmi.registry.LocateRegistry;
 5
 6 import javax.management.MBeanServer;
 7 import javax.management.ObjectName;
 8 import javax.management.remote.JMXConnectorServer;
 9 import javax.management.remote.JMXConnectorServerFactory;
10 import javax.management.remote.JMXServiceURL;
11
12 public class Server {
13     public static void main(String args[]) throws Exception{
14        
15 //        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
16         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
17         LocateRegistry.createRegistry(9999);
18         JMXServiceURL url = new JMXServiceURL(
19                 "service:jmx:rmi:///jndi/rmi://localhost:9999/server");
20         JMXConnectorServer cs = JMXConnectorServerFactory
21                 .newJMXConnectorServer(url, null, mbs);
22        
23         ZooMXBean mxbean = new ZooImpl();
24         ObjectName name = new ObjectName("ZooMXBean:type=MXBean");
25         //注册ZooOpenMBean这个OpenMBean
26         mbs.registerMBean(mxbean, name);
27         //开起RMI服务
28         cs.start();
29        
30         System.out.println(" the mxbean server is start");
31     }
32 }
33

代码:Client端
 1
 2 package test.jmx.mxbean.易做图;
 3
 4
 5
 6 import javax.management.MBeanServerConnection;
 7 import javax.management.ObjectName;
 8 import javax.management.openmbean.ArrayType;
 9 import javax.management.openmbean.CompositeData;
10 import javax.management.openmbean.CompositeDataSupport;
11 import javax.management.openmbean.CompositeType;
12 import javax.management.openmbean.OpenType;
13 import javax.management.openmbean.SimpleType;
14 import javax.management.remote.JMXConnector;
15 import javax.management.remote.JMXConnectorFactory;
16 import javax.management.remote.JMXServiceURL;
17
18 public class Client {
19    
20     public static void main(String[] arg

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,