之前有了解过JMX,工作中有时候也在用,但是啥东西不系统的学一下,你的内心是空虚的,因此这两天整了整。就放些代码吧,和JMX规范的架构图把。
JMX 架构图:
package com.blackbeans.example.mbeans; /** * Mbean接口 * @author blackbeans * */ public interface GunMBean { public void aimAndShoot(String[] targetName); }
package com.blackbeans.example.mbeans; import javax.management.AttributeChangeNotification; import javax.management.NotificationBroadcasterSupport; /** * 实现的Mbean,即所谓的JMX Resources * * @author blackbeans * */ public class Gun extends NotificationBroadcasterSupport implements GunMBean { private String gunModel; public Gun(String gunModel) { this.gunModel = gunModel; } @Override public void aimAndShoot(String[] targetNames){ for(String targetName : targetNames ){ System.out.println(gunModel + "aim at "+ targetName +" !"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(gunModel + " fire !"); } AttributeChangeNotification notification = new AttributeChangeNotification(this, 0, 0L, "Gun shot!", null, null, null, null); sendNotification(notification); } }
为了监控MBean的值变,JMX 的Agent Level有提供基于java 事件的Notification机制可以通知其他的JMX当前的资源发生了变更。JMX 的Agent Level提供了Monitor服务并通知所有的Listener。
package com.blackbeans.example.mbeans; import javax.management.Notification; import javax.management.NotificationListener; public class GunFiredNotificationListener implements NotificationListener { @Override public void handleNotification(Notification notification, Object handback) { System.out.println("notifyInfo:\t"+notification+"\n handback:\t"+handback); } }
MBServer
package com.blackbeans.example.mbeans; import java.io.IOException; import java.lang.management.ManagementFactory; import java.net.MalformedURLException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL; public class MyMBeanServer { private MBeanServer mbServer; public MyMBeanServer() { this.mbServer = ManagementFactory.getPlatformMBeanServer(); try { Registry reg = LocateRegistry.createRegistry(9999); for(String bind : reg.list()){ System.out.println(bind); } JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://192.168.1.101:9999/MBServer"); JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbServer); connector.start(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void registerMBean(String objectName,Object obj) { ObjectName objName = null; try { objName = new ObjectName(objectName); this.mbServer.registerMBean(obj, objName); this.mbServer.addNotificationListener(objName, new GunFiredNotificationListener(), null, null); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { Gun gun = new Gun("AK-47"); MyMBeanServer myServer = new MyMBeanServer(); myServer.registerMBean(gun.getClass().getPackage().getName() + ":name="+gun.getClass().getSimpleName(), gun); Thread.sleep(100000); } }
MBeanClient
package com.blackbeans.example.mbeans; import java.io.IOException; import java.net.MalformedURLException; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.management.ReflectionException; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; public class MyMBeanClient{ private MBeanServerConnection connection; public MyMBeanClient(String jmxUrl) { JMXServiceURL url = null; try { url = new JMXServiceURL(jmxUrl); JMXConnector connector = JMXConnectorFactory.connect(url); connector.connect(); this.connection = connector.getMBeanServerConnection(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void invokeMethod(ObjectName objName,String opName ,Object[] paras,String[] signature) { try { this.connection.invoke(objName, opName, paras, signature); } catch (InstanceNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MBeanException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ReflectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { String[] gunTypes = new String[]{"AK-47","M16"}; MyMBeanClient client = new MyMBeanClient("service:jmx:rmi:///jndi/rmi://192.168.1.101:9999/MBServer"); try { ObjectName objName = new ObjectName("com.blackbeans.example.mbeans:name=Gun"); Object[] parameters = new Object[]{gunTypes}; String[] signatures = new String[]{"[Ljava.lang.String;"}; client.invokeMethod(objName, "aimAndShoot", parameters, signatures); } catch (MalformedObjectNameException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NullPointerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
我的例子使用的JMX的RMI方式调用,当然你也可以选择使用本地方式或者使用Sun的 HtmlJmxAdapter 来完成JMX调用或者不用客户端直接使用jconsol连接上去调用即可.
reference:
http://java.sun.com/developer/technicalArticles/J2SE/jmx.html