标签:
JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。package org.suren.littlebird; /** * @author suren * 接口名必须以MXBean结尾 */ public interface ITestHelloMXBean { public void hello(); public String getName(); }[/codesyntax]
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]
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]
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
标签:
原文地址:http://my.oschina.net/surenpi/blog/481991