MXBean是一种MBean。
只会引用有限的几种数据类型,predefined set of data types。
如此,任何client都可以访问MXBean,包括远程client。client无需能访问到MXBean的attribute 的class。
命名规范:
接口 : XXXMXBean。或者用@MXBean注释,也可任意指定。
实现类 : 任意。
MXBean中的attribute可以是任意类的对象ClassA,而client无需ClassA的classpath也可以访问此MXBean的attribute。
ClassA的对象会被映射为Open Types。
javax.management.openmbean.CompositeData
Open Types :
javax.mamagement.openmbean.*
示例:
package test.xue.mxbean.model; import java.beans.ConstructorProperties; import java.util.Date; public class QueueSample { private final Date date; private final int size; private final String head; /** * MXBean framework calls getters to convert this into a javax.management.openmbean.CompositeData * ConstructorProperties reconstruct this from a COmpositeData */ @ConstructorProperties({ "date", "size", "head" }) public QueueSample(Date date, int size, String head) { this.date = date; this.size = size; this.head = head; } public Date getDate() { return date; } public int getSize() { return size; } public String getHead() { return head; } }
MXBean示例:
package test.xue.mxbean; import test.xue.mxbean.model.QueueSample; public interface QueueSamplerMXBean { public QueueSample getQueueSample(); // attribute public void clearQueue(); // operation }
package test.xue.mxbean; import java.util.Date; import java.util.Queue; import test.xue.mxbean.model.QueueSample; public class QueueSampler implements QueueSamplerMXBean { private Queue<String> queue; public QueueSampler(Queue<String> queue) { this.queue = queue; } @Override public QueueSample getQueueSample() { synchronized (queue) { return new QueueSample(new Date(), queue.size(), queue.peek()); } } @Override public void clearQueue() { synchronized (queue) { queue.clear(); } } }
package test.xue.mxbean; import java.lang.management.ManagementFactory; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; import javax.management.InstanceAlreadyExistsException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; public class QueueSampleAgent { public static void main(String[] args) throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InterruptedException { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("test.xue.mxbean:type=QueueSample"); Queue<String> queue = new ArrayBlockingQueue<String>(10); queue.add("Element-1"); queue.add("Element-2"); queue.add("Element-3"); QueueSampler mxbean = new QueueSampler(queue); server.registerMBean(mxbean, name); System.out.println("waiting here for remote management..."); Thread.sleep(Long.MAX_VALUE); } }
可以看到,attribute QueueSample的类型转换为了CompositeData。
区别:
MXBean的代码看起来和Standard MBean没什么区别,唯一的无别在于Bean的名字后缀是MXBean,而不是MBean。
但是,如果把QueueSamplerMXBean变为QueueSamplerMBean,那么就由MXBean变为了Standard MBean。
此时,JConsole测试结果为 :
可以看到,此时attribute QueueSample的值已经变为了“不可见”,因为JConsole的classpath中没有QueueSample。