今天在developerWorks中国上看《Java SE 6 新特性: JMX 与系统管理》,发现其例子用
jconsole监视不到其中的MBean,不过稍加改动就可以:
标准MBean中,Main.java需要改一下,关键是要用ManagementFactory.getPlatformMBeanServer( )
- package learn.mustang.jmx.standardbeans;
- import java.lang.management.ManagementFactory;
- import javax.management.MBeanServer;
- import javax.management.ObjectName;
- public class Main {
- private static ObjectName objectName;
- private static MBeanServer mBeanServer;
- public static void main(String[] args) throws Exception {
- init();
- while (true) {
- Thread.yield();
- }
- }
- private static void init() throws Exception {
- ServerImpl serverImpl = new ServerImpl();
- ServerMonitor serverMonitor = new ServerMonitor(serverImpl);
- mBeanServer = ManagementFactory.getPlatformMBeanServer();
- objectName = new ObjectName("objectName:id=ServerMonitor");
- mBeanServer.registerMBean(serverMonitor, objectName);
- }
- }
动态 MBean中,除了Main.java要同样改一下之外,ServerMonitor也要改一下,关键
是要实现public AttributeList getAttributes(String[] arg0)
- package learn.mustang.jmx.dynamicbeans;
- import javax.management.*;
- import java.lang.reflect.*;
- import learn.mustang.jmx.standardbeans.ServerImpl;
- public class ServerMonitor implements DynamicMBean {
- private final ServerImpl target;
- private MBeanInfo mBeanInfo;
- public ServerMonitor(ServerImpl target) {
- this.target = target;
- }
- // 实现获取被管理的 ServerImpl 的 upTime
- public long upTime() {
- return System.currentTimeMillis() - target.startTime;
- }
- public Object getAttribute(String attribute)
- throws AttributeNotFoundException, MBeanException,
- ReflectionException {
- if (attribute.equals("UpTime")) {
- return upTime();
- }
- return null;
- }
- public MBeanInfo getMBeanInfo() {
- if (mBeanInfo == null) {
- try {
- Class cls = this.getClass();
- // 用反射获得 "upTime" 属性的读方法
- Method readMethod = cls.getMethod("upTime", new Class[0]);
- // 用反射获得构造方法
- Constructor constructor = cls
- .getConstructor(new Class[] { ServerImpl.class });
- // 关于 "upTime" 属性的元信息:名称为 UpTime,只读属性(没有写方法)。
- MBeanAttributeInfo upTimeMBeanAttributeInfo = new MBeanAttributeInfo(
- "UpTime", "服务器运行时间", readMethod, null);
- // 关于构造函数的元信息
- MBeanConstructorInfo mBeanConstructorInfo = new MBeanConstructorInfo(
- "唯一构造器", constructor);
- // ServerMonitor 的元信息,为了简单起见,在这个例子里,
- // 没有提供 invocation 以及 listener 方面的元信息
- mBeanInfo = new MBeanInfo(cls.getName(), "监视示例服务器",
- new MBeanAttributeInfo[] { upTimeMBeanAttributeInfo },
- new MBeanConstructorInfo[] { mBeanConstructorInfo },
- null, null);
- } catch (Exception e) {
- throw new Error(e);
- }
- }
- return mBeanInfo;
- }
- public AttributeList getAttributes(String[] arg0) {
- AttributeList ret = new AttributeList();
- for (String attributeName : arg0) {
- Object attributeValue;
- try {
- attributeValue = getAttribute(attributeName);
- } catch (Exception e) {
- attributeValue = e;
- }
- ret.add(new Attribute(attributeName, attributeValue));
- }
- return ret;
- }
- public Object invoke(String arg0, Object[] arg1, String[] arg2)
- throws MBeanException, ReflectionException {
- return null;
- }
- public void setAttribute(Attribute arg0) throws AttributeNotFoundException,
- InvalidAttributeValueException, MBeanException, ReflectionException {
- return;
- }
- public AttributeList setAttributes(AttributeList arg0) {
- return null;
- }
- }
附图是在jconsole中看到的信息。