MXBean
MXBean也是JMX MBean的一种,与MBean的区别是:MXBean引用一组预定义的数据类型,通过这种方式,MBean可以被任何形式的客户端使用,包括远程客户端。MXBean提供一种方便的方式将相关值打包在一起,不需要客户专门配置为处理包。
我个人比较浅显的理解是,MBean与MXBean的区别是:定义MBean接口实现类的属性时,MBean只支持Java的八种基本数据和一些简单的引用类型,如String。不能支持复杂数据类型的关联映射。而MXBean接口实现类定义属性时,支持基本数据类型,引用数据类型,和自定义的数据类型。
MBean与MXBean编写规则非常相似,同样是定义xxxMXBean interface,实现xxxMXBean接口;在编写MXBean接口时,也可以不以MXBean结尾,而在是接口上使用@MXBean注释,表示接口为MXBean接口。
例:以MXBean方式编写程序,在 MXBean实现类中定义属性为引用类型:
#Step 1:定义MXBean接口
package com.jmx.demo3; import java.util.List; public interface LibrariesMXBean { public int getTotal(); public List<Book> getBookList(); public Book getBook(int index); public void clearBookList(); }
#Step 2 :编写一个普通类Book
package com.jmx.demo3; import java.beans.ConstructorProperties; public class Book { private int index; private String title; private int price; private String author; @ConstructorProperties({ "index", "title", "price", "author" }) public Book(int index, String title, int price, String author) { this.index = index; this.title = title; this.price = price; this.author = author; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
#Step 3: 编写MXBean的实现类Libraries
package com.jmx.demo3; import java.util.List; public class Libraries implements LibrariesMXBean { private static List<Book> bookList; public Libraries(List<Book> bookList) { this.bookList = bookList; } @Override public List<Book> getBookList() { return bookList; } @Override public int getTotal() { return bookList.size() ; } @Override public void clearBookList() { bookList.clear(); } @Override public Book getBook(int index) { if(bookList.size() > 0){ return bookList.get(index); } return null; } }
#Step 4: 创建MBeanServer,并注册MXBean
package com.jmx.demo3; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.List; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; public class Main { public static void main(String[] args) throws Exception { // MBeanServer mbs = MBeanServerFactory.createMBeanServer(); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName mxbeanName = new ObjectName("com.jmx.demo3:type=Libraries"); List<Book> bookList = new ArrayList<Book>(); bookList.add(new Book(1, "Thinking in Java", 99, "Bruce Eckel")); bookList.add(new Book(2, "Effective Java", 88, "Joshua Bloch")); bookList.add(new Book(3, "Core java", 42, "Cay S. Horstmann")); Libraries libraries = new Libraries(bookList); mbs.registerMBean(libraries, mxbeanName); System.out.println("Waiting forever..."); Thread.sleep(Long.MAX_VALUE); } }
注意:如果在上面的程序中将接口定义为MBean的方式,在MBean实现类中定义bookList属性,会出现如下错误
总结:MXBean是MBean的一种,比MBean支持更多属性数据类型的定义,其它我还没搞清楚。