package jmx; import java.io.IOException; import java.net.MalformedURLException; import javax.management.*; import javax.management.remote.*; public class XuqkTestServerJMXMP { public static void main(String Args[]) { try { MBeanServer mbs = MBeanServerFactory.newMBeanServer(); String domain = mbs.getDefaultDomain(); echo("<<domain:" + domain); String className = "jmx.XuqkTest"; String name = domain + ":" + "type=" + className + ",index=1"; ObjectName objectName = ObjectName.getInstance(name); mbs.createMBean(className, objectName); String protocol = "jmxmp"; String host = "127.0.0.1"; int port = 1110; JMXServiceURL jmxURL = new JMXServiceURL(protocol, host, port); JMXConnectorServer connectorServer = JMXConnectorServerFactory .newJMXConnectorServer(jmxURL, null, mbs); connectorServer.start(); echo("<<mbs is started"); echo("please click any key to stop mbs!"); waitForEnterPressed(); connectorServer.stop(); } catch (MalformedObjectNameException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (InstanceAlreadyExistsException e) { e.printStackTrace(); } catch (MBeanRegistrationException e) { e.printStackTrace(); } catch (NotCompliantMBeanException e) { e.printStackTrace(); } catch (ReflectionException e) { e.printStackTrace(); } catch (MBeanException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void echo(String msg) { System.out.println(msg); } private static void waitForEnterPressed() { try { echo("/nPress <Enter> to continue..."); System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
代码不难,主要就是用“jmxmp”协议建一个连接器供客户端连接,在我本机调试的时候一直报错(java.net.MalformedURLException: Unsupported protocol: jmxmp); 不支持jmxmp协议,弄了好几天
其原因是因为jmxmp在jmx体系里本身是作为一个可选的协议,jdk1.5里面没有带基于jmxmp协议的连接器,jmxmp是在JSR 160里面定义的,如果要让自己的jmx支持jmxmp的话,需要去sun网站下载jmxremote_optional.jar这个包。
后来我去sun网站下了jdmk51_02-allplatforms.zip这个文件,在其中有一个sun-jdmk-runtime-jmx- 5.1-b34.2.zip,这里面有实现jmxmp协议的连接器的jar,解压之后把里面的jar文件全部拷贝在javaHome/lib/jre /lib/ext下之后,如我所愿,该看到的调试信息都看到了,感觉真好!!!!!
原来就是少了jmxmp协议连接器实现的jar。
http://blog.csdn.net/qingkangxu/article/details/1943197