天天看点

学习《Java SE 6 新特性: JMX 与系统管理》

 今天在developerWorks中国上看《Java SE 6 新特性: JMX 与系统管理》,发现其例子用

jconsole监视不到其中的MBean,不过稍加改动就可以:

标准MBean中,Main.java需要改一下,关键是要用ManagementFactory.getPlatformMBeanServer( )

  1. package learn.mustang.jmx.standardbeans;
  2. import java.lang.management.ManagementFactory;
  3. import javax.management.MBeanServer;
  4. import javax.management.ObjectName;
  5. public class Main {
  6.  private static ObjectName objectName;
  7.  private static MBeanServer mBeanServer;
  8.  public static void main(String[] args) throws Exception {
  9.   init();
  10.   while (true) {
  11.    Thread.yield();
  12.   }
  13.  }
  14.  private static void init() throws Exception {
  15.   ServerImpl serverImpl = new ServerImpl();
  16.   ServerMonitor serverMonitor = new ServerMonitor(serverImpl);
  17.   mBeanServer = ManagementFactory.getPlatformMBeanServer();
  18.   objectName = new ObjectName("objectName:id=ServerMonitor");
  19.   mBeanServer.registerMBean(serverMonitor, objectName);
  20.  }
  21. }

动态 MBean中,除了Main.java要同样改一下之外,ServerMonitor也要改一下,关键

是要实现public AttributeList getAttributes(String[] arg0)

  1. package learn.mustang.jmx.dynamicbeans;
  2. import javax.management.*;
  3. import java.lang.reflect.*;
  4. import learn.mustang.jmx.standardbeans.ServerImpl;
  5. public class ServerMonitor implements DynamicMBean {
  6.  private final ServerImpl target;
  7.  private MBeanInfo mBeanInfo;
  8.  public ServerMonitor(ServerImpl target) {
  9.   this.target = target;
  10.  }
  11.  // 实现获取被管理的 ServerImpl 的 upTime
  12.  public long upTime() {
  13.   return System.currentTimeMillis() - target.startTime;
  14.  }
  15.  public Object getAttribute(String attribute)
  16.    throws AttributeNotFoundException, MBeanException,
  17.    ReflectionException {
  18.   if (attribute.equals("UpTime")) {
  19.    return upTime();
  20.   }
  21.   return null;
  22.  }
  23.  public MBeanInfo getMBeanInfo() {
  24.   if (mBeanInfo == null) {
  25.    try {
  26.     Class cls = this.getClass();
  27.     // 用反射获得 "upTime" 属性的读方法
  28.     Method readMethod = cls.getMethod("upTime", new Class[0]);
  29.     // 用反射获得构造方法
  30.     Constructor constructor = cls
  31.       .getConstructor(new Class[] { ServerImpl.class });
  32.     // 关于 "upTime" 属性的元信息:名称为 UpTime,只读属性(没有写方法)。
  33.     MBeanAttributeInfo upTimeMBeanAttributeInfo = new MBeanAttributeInfo(
  34.       "UpTime", "服务器运行时间", readMethod, null);
  35.     // 关于构造函数的元信息
  36.     MBeanConstructorInfo mBeanConstructorInfo = new MBeanConstructorInfo(
  37.       "唯一构造器", constructor);
  38.     // ServerMonitor 的元信息,为了简单起见,在这个例子里,
  39.     // 没有提供 invocation 以及 listener 方面的元信息
  40.     mBeanInfo = new MBeanInfo(cls.getName(), "监视示例服务器",
  41.       new MBeanAttributeInfo[] { upTimeMBeanAttributeInfo },
  42.       new MBeanConstructorInfo[] { mBeanConstructorInfo },
  43.       null, null);
  44.    } catch (Exception e) {
  45.     throw new Error(e);
  46.    }
  47.   }
  48.   return mBeanInfo;
  49.  }
  50.  public AttributeList getAttributes(String[] arg0) {
  51.   AttributeList ret = new AttributeList();
  52.   for (String attributeName : arg0) {
  53.    Object attributeValue;
  54.    try {
  55.     attributeValue = getAttribute(attributeName);
  56.    } catch (Exception e) {
  57.     attributeValue = e;
  58.    }
  59.    ret.add(new Attribute(attributeName, attributeValue));
  60.   }
  61.   return ret;
  62.  }
  63.  public Object invoke(String arg0, Object[] arg1, String[] arg2)
  64.    throws MBeanException, ReflectionException {
  65.   return null;
  66.  }
  67.  public void setAttribute(Attribute arg0) throws AttributeNotFoundException,
  68.    InvalidAttributeValueException, MBeanException, ReflectionException {
  69.   return;
  70.  }
  71.  public AttributeList setAttributes(AttributeList arg0) {
  72.   return null;
  73.  }
  74. }

附图是在jconsole中看到的信息。