/** * 得到JMX的连接 * * @param ip * 网址 * @param port * 端口 * @param environment * 环境 * @return JMX连接 */ public static JMXConnector getConnect(String ip, String port, Map<String, ?> environment) { if (StringUtil.isNullOrBlank(ip) || StringUtil.isNullOrBlank(port)) { logger.error("ip和端口不能为空."); return null; } try { Registry registry = LocateRegistry.getRegistry(ip, Integer.parseInt(port)); RMIServer stub = (RMIServer) registry.lookup("jmxrmi"); JMXConnector jmxc = new RMIConnector(stub, environment); jmxc.connect(); return jmxc; } catch (Exception e) { logger.error("连接JMX服务器失败."); } return null; }
public JMXConnector newJMXConnector(JMXServiceURL serviceURL, Map<String,?> environment) throws IOException { if (!serviceURL.getProtocol().equals("rmi")) { throw new MalformedURLException("Protocol not rmi: " + serviceURL.getProtocol()); } return new RMIConnector(serviceURL, environment); }
public JMXConnector newJMXConnector(JMXServiceURL serviceURL, Map<String,?> environment) throws IOException { if (!serviceURL.getProtocol().equals("iiop")) { throw new MalformedURLException("Protocol not iiop: " + serviceURL.getProtocol()); } return new RMIConnector(serviceURL, environment); }
public JMXConnector newJMXConnector(JMXServiceURL url, Map<String,?> map) throws IOException { final String protocol = url.getProtocol(); called = true; System.out.println("JMXConnectorProviderImpl called"); if(protocol.equals("rmi")) return new RMIConnector(url, map); if(protocol.equals("throw-provider-exception")) throw new JMXProviderException("I have been asked to throw"); throw new IllegalArgumentException("UNKNOWN PROTOCOL"); }
public static void main(String[] args) throws Exception { System.out.println("---RMIConnectorNullSubjectConnTest starting..."); JMXConnectorServer connectorServer = null; JMXConnector connectorClient = null; try { MBeanServer mserver = ManagementFactory.getPlatformMBeanServer(); JMXServiceURL serverURL = new JMXServiceURL("rmi", "localhost", 0); connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverURL, null, mserver); connectorServer.start(); JMXServiceURL serverAddr = connectorServer.getAddress(); connectorClient = JMXConnectorFactory.connect(serverAddr, null); connectorClient.connect(); Field nullSubjectConnField = RMIConnector.class.getDeclaredField("nullSubjectConnRef"); nullSubjectConnField.setAccessible(true); WeakReference<MBeanServerConnection> weak = (WeakReference<MBeanServerConnection>)nullSubjectConnField.get(connectorClient); if (weak != null && weak.get() != null) { throw new RuntimeException("nullSubjectConnRef must be null at initial time."); } MBeanServerConnection conn1 = connectorClient.getMBeanServerConnection(null); MBeanServerConnection conn2 = connectorClient.getMBeanServerConnection(null); if (conn1 == null) { throw new RuntimeException("A connection with null subject should not be null."); } else if (conn1 != conn2) { throw new RuntimeException("The 2 connections with null subject are not equal."); } conn1 = null; conn2 = null; int i = 1; do { System.gc(); Thread.sleep(100); weak = (WeakReference<MBeanServerConnection>)nullSubjectConnField.get(connectorClient); } while ((weak != null && weak.get() != null) && i++ < 60); System.out.println("---GC times: " + i); if (weak != null && weak.get() != null) { throw new RuntimeException("Failed to clean RMIConnector's nullSubjectConn"); } else { System.out.println("---RMIConnectorNullSubjectConnTest: PASSED!"); } } finally { try { connectorClient.close(); connectorServer.stop(); } catch (Exception e) { } } }
public static void main(String[] args) throws Exception { System.out.println("---RMIConnectorInternalMapTest starting..."); JMXConnectorServer connectorServer = null; JMXConnector connectorClient = null; try { MBeanServer mserver = ManagementFactory.getPlatformMBeanServer(); JMXServiceURL serverURL = new JMXServiceURL("rmi", "localhost", 0); connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverURL, null, mserver); connectorServer.start(); JMXServiceURL serverAddr = connectorServer.getAddress(); connectorClient = JMXConnectorFactory.connect(serverAddr, null); connectorClient.connect(); Field rmbscMapField = RMIConnector.class.getDeclaredField("rmbscMap"); rmbscMapField.setAccessible(true); Map<Subject, WeakReference<MBeanServerConnection>> map = (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient); if (map != null && !map.isEmpty()) { // failed throw new RuntimeException("RMIConnector's rmbscMap must be empty at the initial time."); } Subject delegationSubject = new Subject(true, Collections.singleton(new JMXPrincipal("delegate")), Collections.EMPTY_SET, Collections.EMPTY_SET); MBeanServerConnection mbsc1 = connectorClient.getMBeanServerConnection(delegationSubject); MBeanServerConnection mbsc2 = connectorClient.getMBeanServerConnection(delegationSubject); if (mbsc1 == null) { throw new RuntimeException("Got null connection."); } if (mbsc1 != mbsc2) { throw new RuntimeException("Not got same connection with a same subject."); } map = (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient); if (map == null || map.isEmpty()) { // failed throw new RuntimeException("RMIConnector's rmbscMap has wrong size " + "after creating a delegated connection."); } delegationSubject = null; mbsc1 = null; mbsc2 = null; int i = 0; while (!map.isEmpty() && i++ < 60) { System.gc(); Thread.sleep(100); } System.out.println("---GC times: " + i); if (!map.isEmpty()) { throw new RuntimeException("Failed to clean RMIConnector's rmbscMap"); } else { System.out.println("---RMIConnectorInternalMapTest: PASSED!"); } } finally { try { connectorClient.close(); connectorServer.stop(); } catch (Exception e) { } } }