Standard MBean由一个接口和一个实现类组成。
命名规范 :
接口 : XXXMBean
实现类 : XXX
按照约定,MBean的name为XXX。自定。
接口中的每个方法定义了一个attribute或operation。
以get或set开头的方法 : 如果符合getter或setter的模式,就是定义了一个attribute,类型为getter的返回值类型或setter参数类型。
其他方法 : 定义了一个operation。
示例:
package test.xue.mbean; public interface HelloMBean { public void sayHello(); public int add(int x, int y); public String getName(); public int getCacheSize(); public void setCacheSize(int size); public void setText(); public void settitle(String title); }
attributes:
name : readonly
cacheSize : readable and writable
title : writable
operations:
sayHello
add
setText : 不符合setter模式。
实现类:
package test.xue.mbean; public class Hello implements HelloMBean { private final String name = "My HelloMBean"; private int cacheSize = 1024; private String title; @Override public void sayHello() { System.out.println("hello, my HelloMBean"); } @Override public int add(int x, int y) { return x+y; } @Override public String getName() { return name; } @Override public int getCacheSize() { return cacheSize; } @Override public void setCacheSize(int size) { this.cacheSize = size; System.out.println("cacheSize changed : " + size); } @Override public void setText() { System.out.println("setText called"); } @Override public void settitle(String title) { this.title = title; System.out.println("title changed : " + title); } }
现在,就可以创建一个JMX agent来通过MBean管理资源了。
package test.xue.mbean.management; import java.lang.management.ManagementFactory; import javax.management.InstanceAlreadyExistsException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; import test.xue.mbean.Hello; public class HelloAgent { public static void main(String[] args) throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InterruptedException { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("test.xue.mbean:type=Hello"); // any name // ObjectName name = new ObjectName("test.xue.mbean22:type=Hello2"); // also works Hello mbean = new Hello(); server.registerMBean(mbean, name); System.out.println("waiting here for remote management..."); Thread.sleep(Long.MAX_VALUE); } }
如上,每个MBean都需要一个ObjectName,ObjectName的命名规范:
domain:(key=value)+
例子中的domain=test.xue.mbean,type=Hello,可任意指定。
可通过JConsole来测试此JMX agent
运行 : jconsole
连接以后,
可以尝试修改attributes,调用operations,同时注意控制台输出。
如果出现javax.management.NotCompliantMBeanException :
Exception in thread "main" javax.management.NotCompliantMBeanException: MBean class test.xue.mbean.Hello2 does not implement DynamicMBean, neither follows the Standard MBean conventions (javax.management.NotCompliantMBeanException: Class test.xue.mbean.Hello2 is not a JMX compliant Standard MBean) nor the MXBean conventions (javax.management.NotCompliantMBeanException: test.xue.mbean.Hello2: Class test.xue.mbean.Hello2 is not a JMX compliant MXBean) at com.sun.jmx.mbeanserver.Introspector.checkCompliance(Unknown Source) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(Unknown Source) at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(Unknown Source) at test.xue.mbean.management.HelloAgent.main(HelloAgent.java:24)
接口和实现类的名字不符合命名规范: XXXMBean, XXX。