JMX
JMX简介
JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架。JMX是一套标准的代理和服务,实际上,用户可以在任何Java应用程序中使用这些代理和服务实现管理。这是官方文档上的定义,我看过很多次也无法很好的理解。我个人的理解是JMX让程序有被管理的功能,例如你开发一个WEB网站,它是在24小时不间断运行,那么你肯定会对网站进行监控,如每天的UV、PV是多少;又或者在业务高峰的期间,你想对接口进行限流,就必须去修改接口并发的配置值。
JMX架构图
案例:
Mbean
package com.hpe.jmx; //MBean接口 public interface HelloMBean { public String getName(); public void setName(String name); public String getAge(); public void setAge(String age); public String helloWorld(); public void helloWorld(String str); public void getTelephone(); }
package com.hpe.jmx; import java.security.PublicKey; //Mbean的实现 public class Hello implements HelloMBean { private String name; private String age; public Hello(String name,String age){ this.name = name; this.age = age; } public void getTelephone() { System.out.println("get Telephone"); } public String helloWorld() { System.out.println("hello world"); return "gaojy"; } public void helloWorld(String str) { System.out.println("helloWorld:" + str); } public String getName() { System.out.println("get name 123"); return name; } public void setName(String name) { System.out.println("set name 123"); this.name = name; } public String getAge() { System.out.println("get age 123"); return age; } public void setAge(String age) { System.out.println("set age 123"); this.age = age; } }
适配层:MBeanServer,主要是提供对资源的注册和管理。
接入层:提供远程访问的入口。
package com.hpe.jmx;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import javax.management.JMException;
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 helloAgent {
public static void main(String[] args) throws JMException, Exception {
//适配层
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName helloName = new ObjectName("jmxBean:name=hello");
// create mbean and register mbean
//创建并注册Mbean
server.registerMBean(new Hello("gaojy","24"), helloName);
try {
//这个步骤很重要,注册一个端口,绑定url后用于客户端通过rmi方式连接JMXConnectorServer
LocateRegistry.createRegistry(9999);
// URL路径的结尾可以随意指定,但如果需要用Jconsole来进行连接,则必须使用jmxrmi
JMXServiceURL url = new JMXServiceURL
("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
JMXConnectorServer jcs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
System.out.println("begin rmi start");
jcs.start();
System.out.println("rmi start");
} catch (Exception e) {
// TODO: handle exception
}
Thread.sleep(60 * 60 * 1000);
}
}
客户端:
package com.hpe.jmx; import java.io.IOException; import java.net.MalformedURLException; import javax.management.Attribute; import javax.management.AttributeNotFoundException; import javax.management.InstanceNotFoundException; import javax.management.InvalidAttributeValueException; import javax.management.MBeanException; import javax.management.MBeanServerConnection; import javax.management.MBeanServerInvocationHandler; 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 Client { public static void main(String[] args) throws Exception { JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); // ObjectName的名称与前面注册时候的保持一致 ObjectName mbeanName = new ObjectName("jmxBean:name=hello"); System.out.println("Domains ......"); String[] domains = mbsc.getDomains(); for (int i = 0; i < domains.length; i++) { System.out.println("doumain[" + i + "]=" + domains[i]); } System.out.println("MBean count = " + mbsc.getMBeanCount()); // 设置指定Mbean的特定属性值 // 这里的setAttribute、getAttribute操作只能针对bean的属性 // 例如对getName或者setName进行操作,只能使用Name,需要去除方法的前缀 mbsc.setAttribute(mbeanName, new Attribute("Name", "杭州"));// 远程设置 mbsc.setAttribute(mbeanName, new Attribute("Age", "1990")); String age = (String) mbsc.getAttribute(mbeanName, "Age"); // 远程获取 String name = (String) mbsc.getAttribute(mbeanName, "Name"); System.out.println("age=" + age + ";name=" + name); // 远程方法调用 HelloMBean proxy = MBeanServerInvocationHandler.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false); proxy.helloWorld(); proxy.helloWorld("migu"); proxy.getTelephone(); // invoke调用bean的方法,只针对非设置属性的方法 // 例如invoke不能对getName方法进行调用 mbsc.invoke(mbeanName, "getTelephone", null, null); mbsc.invoke(mbeanName, "helloWorld", new String[] { "I'll connect to JMX Server via client2" }, new String[] { "java.lang.String" }); mbsc.invoke(mbeanName, "helloWorld", null, null); } }
参考