jmx server 和jmx client

启动jmx server 和jmx client,通过jconsole进入jmx server

然后通过其中远程进程,进入jmx client:

发现,两者可用的tab页不同, MBean的数量类型也不同:

如何理解呢?其实原因就是两者的MBeanServer不同, 前者是jmx自带的,后者是我new出来的。

访问JMImplementation 看看:


            ObjectName jmxname = new ObjectName("JMImplementation:type=MBeanServerDelegate");
            javax.management.MBeanServerDelegateMBean jmxMBean = JMX.newMXBeanProxy(connection, jmxname ,
              javax.management.MBeanServerDelegateMBean.class);
   String notificationInfo = jmxMBean.getSpecificationName();
   System.out.println(notificationInfo);
//   for (int i = 0; i < notificationInfo.length; i++) {
//    System.out.println(notificationInfo.toString());
//   }

不过却不能访问jmx server的MBean。。

即使在jmx server端访问,也是失败


   Hashtable<String, String> aa = new Hashtable<String, String>();
   aa.put("type", "MemoryPool");
   aa.put("name", "PS Eden Space");
   ObjectName memoryPoolMXBeanName  = new ObjectName("java.lang",aa );
   
         MemoryPoolMXBean memoryPoolMXBean = JMX.newMXBeanProxy(connection, memoryPoolMXBeanName ,
           MemoryPoolMXBean.class);
        
         MemoryType type = memoryPoolMXBean.getType();
         System.out.println(type);

二月 16, 2014 3:21:15 下午 sun.rmi.server.UnicastServerRef logCallException
FINE: RMI TCP Connection(2)-127.0.0.1: [127.0.0.1] exception:
javax.management.InstanceNotFoundException: java.lang:name=PS Eden Space,type=MemoryPool
 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095)
 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643)
 at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:668)

。。。

原因: 此时的connection不是通往jmx server的connection、JMXConnectorServer,应该

通过MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();然后创建 JMXConnectorServer

或者可以通过ManagementFactory 访问:

        List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
        for (Iterator iterator = memoryPoolMXBeans.iterator(); iterator
    .hasNext();) {
     MemoryPoolMXBean memoryPoolMXBean = (MemoryPoolMXBean) iterator.next();
     System.out.println(memoryPoolMXBean.getName());
  

  }

可见, 如果要访问jmx server内部的mbean(这些都可以理解为PlatformMBean 或者PlatformManagedObject),则需要使用ManagementFactory (静态类)

此外,这个类提供了静态方法访问jmx server的已注册的mbean 等方法,getPlatformManagementInterfaces等是前者抽象了的特殊方法:

  Set<Class<? extends PlatformManagedObject>> set = ManagementFactory.getPlatformManagementInterfaces();
        for (Class<? extends PlatformManagedObject> class1 : set) {
   System.out.println(class1.getName());
  }

  MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
  ObjectName name = new ObjectName("JMImplementation:type=MBeanServerDelegate");
  MBeanInfo beanInfo = platformMBeanServer.getMBeanInfo(name );
  MBeanAttributeInfo[] attributes = beanInfo.getAttributes();
  for (int i = 0; i < attributes.length; i++) {
   MBeanAttributeInfo beanAttributeInfo = attributes[i];
   System.out.println(beanAttributeInfo.getName());
  }

原文地址:https://www.cnblogs.com/FlyAway2013/p/3551233.html