JMX学习笔记(三)-MXBean
在MBean中有只要遵循以下两个规则,我们就可以在jconsole中动态的改变MBean中的属性值
1. JMX中要定义接口必须以xxxMBean的规范定义
2. 得有类实现xxxMBean接口
例如:
package com.dxz.mbean.demo3; public interface ServerConfigureMBean { public int getPort(); public void setPort(int port); public String getHost(); public void setHost(String host); public int getMaxThread(); public void setMaxThread(int maxThread); public int getMinThread(); public void setMinThread(int minThread); }
接着实现这个MBean:
package com.dxz.mbean.demo3; public class ServerConfigure implements ServerConfigureMBean { private int port; private String host; private int maxThread; private int minThread; public ServerConfigure(int port, String host, int maxThread, int minThread) { this.port = port; this.host = host; this.maxThread = maxThread; this.minThread = minThread; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getMaxThread() { return maxThread; } public void setMaxThread(int maxThread) { this.maxThread = maxThread; } public int getMinThread() { return minThread; } public void setMinThread(int minThread) { this.minThread = minThread; } }
测试:
package com.dxz.mbean.demo3; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import javax.management.ObjectName; public class ServerTest { public static void main(String[] args) throws Exception { testServerConfigureMBean(); } public static void testServerConfigureMBean() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ServerConfigure serverConfigure = new ServerConfigure(8080, "china.com", 20, 100); ObjectName serverName = new ObjectName("com.dxz.mbean.demo3:type=ServerConfigure"); mbs.registerMBean(serverConfigure, serverName); System.out.println("Waiting..."); Thread.sleep(Long.MAX_VALUE); } }
打开jconsole:
但是以上这些ServerConfigure的属性都是基本类型,包含在Java.lang.*里,设想,如果说jconsole在类外一个远端的jvm上,如果这里的属性是一个自定义对象,远端的jvm可能不知道该类型的对象:
修改上边的ServerConfigre,使他成为一个聚合对象,去掉ServerConfigureMBean接口实现:
测试结果,打开jconsole查看:
为了让与远端JVM认识该对象,我们需要将ServerMbean改为ServerMXBean,并使用java.beans.ConstructorProperties注解告诉JVM怎样组装该对象:
package com.dxz.mbean.demo5; public interface ServerMXBean { public ServerConfigure getServerConfigure(); public void setServerConfigure(ServerConfigure serverConfigure); public void defaultServerConfigure(); }
1. 使用java.beans.ConstructorProperties注解:
package com.dxz.mbean.demo5; import java.beans.ConstructorProperties; public class ServerConfigure { private int port; private String host; private int maxThread; private int minThread; /** * 加入@ConstructorProperties注解 */ @ConstructorProperties({ "port", "host", "maxThread", "minThread" }) //如果没有会报错,Caused by: java.io.InvalidObjectException: Do not know how to make a com.dxz.mbean.demo5.ServerConfigure from a CompositeData: no method from(CompositeData); no constructor has @ConstructorProperties annotation; does not have a public no-arg constructor; not an interface public ServerConfigure(int port, String host, int maxThread, int minThread) { this.port = port; this.host = host; this.maxThread = maxThread; this.minThread = minThread; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getMaxThread() { return maxThread; } public void setMaxThread(int maxThread) { this.maxThread = maxThread; } public int getMinThread() { return minThread; } public void setMinThread(int minThread) { this.minThread = minThread; } }
package com.dxz.mbean.demo5; public class Server implements ServerMXBean { /** * 封装 */ private ServerConfigure serverConfigure; public Server(ServerConfigure serverConfigure) { this.serverConfigure = serverConfigure; } @Override public ServerConfigure getServerConfigure() { synchronized (serverConfigure) { return this.serverConfigure; } } @Override public void setServerConfigure(ServerConfigure serverConfigure) { synchronized (serverConfigure) { this.serverConfigure = serverConfigure; } } @Override public void defaultServerConfigure() { synchronized (serverConfigure) { serverConfigure.setPort(80); serverConfigure.setHost("www.haitao.com"); serverConfigure.setMinThread(10); serverConfigure.setMaxThread(500); } } }
package com.dxz.mbean.demo5; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import javax.management.ObjectName; public class ServerTest { public static void main(String[] args) throws Exception { testServerMBean(); } public static void testServerMBean() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ServerConfigure serverConfigure = new ServerConfigure(8080, "www.china5.com", 20, 100); Server server = new Server(serverConfigure); ObjectName serverName = new ObjectName( "com.dxz.mbean.demo5:type=Server"); mbs.registerMBean(server, serverName); System.out.println("Waiting..."); Thread.sleep(Long.MAX_VALUE); } }
测试代码一样,就不再重复,最后运行测试,查看jconsole:
双击值,我们将会看到:
实际上MXBean的概念很简单,就是定义xxxMXBean接口,然后实现这个接口,与MBean类似,为了阐述这个MXBean与MBean的不同才列出这么多代码,希望大家能理解:)