JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。
- 用于发布服务的接口类ITestHelloMXBean.java
[codesyntax lang="java" lines="normal"]
package org.suren.littlebird; /** * @author suren * 接口名必须以MXBean结尾 */ public interface ITestHelloMXBean { public void hello(); public String getName(); }
[/codesyntax]
- 用于发布服务的接口实现类TestHello.java
[codesyntax lang="java" lines="normal"]
package org.suren.littlebird; /** * @author suren */ public class TestHello implements ITestHelloMXBean { @Override public void hello() { System.out.println("impl hello"); } @Override public String getName() { return "test get name"; } }
[/codesyntax]
- 服务端测试类JmxServerTest.java
[codesyntax lang="java" lines="normal"]
package org.suren.littlebird; import java.lang.management.ManagementFactory; import java.rmi.registry.LocateRegistry; import java.util.HashMap; import java.util.Map; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL; import javax.naming.Context; /** * @author suren * */ public class JmxServerTest { private static final String DOMAIN = "suren"; //用jconsole连接的话,会在“MBean”选项卡中多出来下面包名定义的一个节点 private static final String PACKAGE = "org.suren.littlebird:type="; public static void main(String args[]) throws Exception { LocateRegistry.createRegistry(5006); //用于远程连接的端口 MBeanServer server = ManagementFactory.getPlatformMBeanServer(); //注册一个可供调用的Bean对象 TestHello testHello = new TestHello(); ObjectName testHelloObjName = new ObjectName(PACKAGE + "TestHello"); server.registerMBean(testHello, testHelloObjName); //注册一个可供远程连接的Bean对象 JMXServiceURL serverUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:5006/"+DOMAIN); Map<String, String> env = new HashMap<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverUrl,env, server); server.registerMBean(connectorServer, new ObjectName(PACKAGE + "JMXConnectorServer")); //启动服务,这个start动作也可以通过jconsole来调用 connectorServer.start(); } }
[/codesyntax]
- 用于测试服务连接的客户端类JmxClientTest.java
[codesyntax lang="java" lines="normal"]
package org.suren.littlebird; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.management.MBeanInfo; import javax.management.MBeanServerConnection; import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; /** * @author suren * */ public class JmxClientTest { /** * @param args */ public static void main(String[] args) throws Exception { String hostName = "127.0.0.1"; //发布JMX服务的地址 int portNum = 5006; //发布服务的端口 JMXServiceURL u = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostName + ":" + portNum + "/suren"); Map<String, Object> auth = new HashMap<String, Object>(); //认证所需要的用户信息 auth.put(JMXConnector.CREDENTIALS, new String[]{ "controlRole", "123" }); JMXConnector jmxConnector = JMXConnectorFactory.connect(u, auth); MBeanServerConnection connection = jmxConnector.getMBeanServerConnection(); Set<ObjectInstance> beans = connection.queryMBeans(null, null); for(ObjectInstance objInstance : beans) { ObjectName mBeanName = objInstance.getObjectName(); MBeanInfo mbeanInfo = connection.getMBeanInfo(mBeanName); System.out.println(Arrays.toString(mbeanInfo.getOperations())); } } }
[/codesyntax] tomcat对我发布的可能是jmxrmi,要访问的uri为service:jmx:rmi:///jndi/rmi://localhost:5006/jmxrmi
- Java认证配置
找到配置文件:$JAVA_HOME\jre\lib\management\jmxremote.password 添加如下配置: suren suren 然后修改jmxremote.password的权限为其他用户没有权限,chmod 700 jmxremote.password 找到配置文件:$JAVA_HOME\jre\lib\management\jmxremote.access suren readwrite \ create javax.management.monitor.*,javax.management.timer.* \ unregister
- 启动远程服务
如果要通过远程(IP地址+端口)来访问的话,需要在运行时加入以下参数(如何添加参数可以在本站搜索): -Dcom.sun.management.jmxremote.port=5006 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Djava.rmi.server.hostname=10.0.32.35