Java Code Examples for javax.management.remote.JMXConnectorServer#getAddress()

The following examples show how to use javax.management.remote.JMXConnectorServer#getAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListenerScaleTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Sender sender = new Sender();
    mbs.registerMBean(sender, testObjectName);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    try {
        test(mbs, cs, cc);
    } finally {
        cc.close();
        cs.stop();
    }
}
 
Example 2
Source File: ListenerScaleTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Sender sender = new Sender();
    mbs.registerMBean(sender, testObjectName);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    try {
        test(mbs, cs, cc);
    } finally {
        cc.close();
        cs.stop();
    }
}
 
Example 3
Source File: IIOPURLTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    JMXServiceURL inputAddr =
        new JMXServiceURL("service:jmx:iiop://");
    JMXConnectorServer s;
    try {
        s = JMXConnectorServerFactory.newJMXConnectorServer(inputAddr, null, null);
    } catch (java.net.MalformedURLException x) {
        try {
            Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");
            throw new RuntimeException("MalformedURLException thrown but iiop appears to be supported");
        } catch (ClassNotFoundException expected) { }
        System.out.println("IIOP protocol not supported, test skipped");
        return;
    }
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    mbs.registerMBean(s, new ObjectName("a:b=c"));
    s.start();
    JMXServiceURL outputAddr = s.getAddress();
    if (!outputAddr.getURLPath().startsWith("/ior/IOR:")) {
        throw new RuntimeException("URL path should start with \"/ior/IOR:\": " +
                                   outputAddr);
    }
    System.out.println("IIOP URL path looks OK: " + outputAddr);
    JMXConnector c = JMXConnectorFactory.connect(outputAddr);
    System.out.println("Successfully got default domain: " +
                       c.getMBeanServerConnection().getDefaultDomain());
    c.close();
    s.stop();
}
 
Example 4
Source File: RMIConnectorLogAttributesTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private JMXConnector connectToServer(JMXConnectorServer server) throws IOException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, ReflectionException, MBeanException {
    JMXServiceURL url = server.getAddress();
    Map<String, Object> env = new HashMap<String, Object>();
    JMXConnector connector = JMXConnectorFactory.connect(url, env);

    System.out.println("DEBUG: Client connected to RMI at: " + url);

    return connector;
}
 
Example 5
Source File: IIOPURLTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    JMXServiceURL inputAddr =
        new JMXServiceURL("service:jmx:iiop://");
    JMXConnectorServer s;
    try {
        s = JMXConnectorServerFactory.newJMXConnectorServer(inputAddr, null, null);
    } catch (java.net.MalformedURLException x) {
        try {
            Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");
            throw new RuntimeException("MalformedURLException thrown but iiop appears to be supported");
        } catch (ClassNotFoundException expected) { }
        System.out.println("IIOP protocol not supported, test skipped");
        return;
    }
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    mbs.registerMBean(s, new ObjectName("a:b=c"));
    s.start();
    JMXServiceURL outputAddr = s.getAddress();
    if (!outputAddr.getURLPath().startsWith("/ior/IOR:")) {
        throw new RuntimeException("URL path should start with \"/ior/IOR:\": " +
                                   outputAddr);
    }
    System.out.println("IIOP URL path looks OK: " + outputAddr);
    JMXConnector c = JMXConnectorFactory.connect(outputAddr);
    System.out.println("Successfully got default domain: " +
                       c.getMBeanServerConnection().getDefaultDomain());
    c.close();
    s.stop();
}
 
Example 6
Source File: OldMBeanServerTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public MBeanServerConnection call() {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    try {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
        JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(
                url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        connector = JMXConnectorFactory.connect(addr);
        return connector.getMBeanServerConnection();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: RMIConnectorLogAttributesTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private JMXConnector connectToServer(JMXConnectorServer server) throws IOException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, ReflectionException, MBeanException {
    JMXServiceURL url = server.getAddress();
    Map<String, Object> env = new HashMap<String, Object>();
    JMXConnector connector = JMXConnectorFactory.connect(url, env);

    System.out.println("DEBUG: Client connected to RMI at: " + url);

    return connector;
}
 
Example 8
Source File: RMIDownloadTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testWithException(boolean send)
throws Exception {
    ClassLoader zoobyCL = new ZoobyClassLoader();
    Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
    Object zooby = zoobyClass.newInstance();

    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
    JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    Object rzooby;
    if (send) {
        System.out.println("Sending object...");
        mbsc.setAttribute(getSetName, new Attribute("It", zooby));
        rzooby = getSetInstance.getIt();
    } else {
        System.out.println("Receiving object...");
        getSetInstance.setIt(zooby);
        rzooby = mbsc.getAttribute(getSetName, "It");
    }

    if (!rzooby.getClass().getName().equals("Zooby")) {
        throw new Exception("FAILED: remote object is not a Zooby");
    }
    if (rzooby.getClass().getClassLoader() ==
            zooby.getClass().getClassLoader()) {
        throw new Exception("FAILED: same class loader: " +
                zooby.getClass().getClassLoader());
    }

    cc.close();
    cs.stop();
}
 
Example 9
Source File: MXBeanInteropTest2.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void run(Map<String, Object> args) {

        System.out.println("MXBeanInteropTest2::run: Start") ;
        int errorCount = 0 ;

        try {
            // JMX MbeanServer used inside single VM as if remote.
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

            JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
            JMXConnectorServer cs =
                JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
            cs.start();

            JMXServiceURL addr = cs.getAddress();
            JMXConnector cc = JMXConnectorFactory.connect(addr);
            MBeanServerConnection mbsc = cc.getMBeanServerConnection();

            // Prints all MBeans whatever the domain is.
            printMBeans(mbsc) ;

            // Call test body
            errorCount += doBasicMXBeanTest(mbsc) ;

            // Terminate the JMX Client
            cc.close();

        } catch(Exception e) {
            Utils.printThrowable(e, true) ;
            throw new RuntimeException(e);
        }

        if ( errorCount == 0 ) {
            System.out.println("MXBeanInteropTest2::run: Done without any error") ;
        } else {
            System.out.println("MXBeanInteropTest2::run: Done with "
                    + errorCount
                    + " error(s)") ;
            throw new RuntimeException("errorCount = " + errorCount);
        }
    }
 
Example 10
Source File: RMIConnectorInternalMapTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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) {
        }
    }
}
 
Example 11
Source File: RMIExitTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void test() {
    try {
        JMXServiceURL u = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer server;
        JMXServiceURL addr;
        JMXConnector client;
        MBeanServerConnection mserver;

        final ObjectName delegateName =
            new ObjectName("JMImplementation:type=MBeanServerDelegate");
        final NotificationListener dummyListener =
            new NotificationListener() {
                    public void handleNotification(Notification n,
                                                   Object o) {
                        // do nothing
                        return;
                    }
                };

        server = JMXConnectorServerFactory.newJMXConnectorServer(u,
                                                                 null,
                                                                 mbs);
        server.start();

        addr = server.getAddress();
        client = JMXConnectorFactory.newJMXConnector(addr, null);
        client.connect(null);

        mserver = client.getMBeanServerConnection();
        String s1 = "1";
        String s2 = "2";
        String s3 = "3";

        mserver.addNotificationListener(delegateName,
                                        dummyListener, null, s1);
        mserver.addNotificationListener(delegateName,
                                        dummyListener, null, s2);
        mserver.addNotificationListener(delegateName,
                                        dummyListener, null, s3);

        mserver.removeNotificationListener(delegateName,
                                           dummyListener, null, s3);
        mserver.removeNotificationListener(delegateName,
                                           dummyListener, null, s2);
        mserver.removeNotificationListener(delegateName,
                                           dummyListener, null, s1);
        client.close();

        server.stop();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        System.exit(1);
    }
}
 
Example 12
Source File: NotSerializableNotifTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void test(String proto) throws Exception {
    System.out.println("\n>>> Test for protocol " + proto);

    JMXServiceURL url = new JMXServiceURL(proto, null, 0);

    System.out.println(">>> Create a server: "+url);

    JMXConnectorServer server = null;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
    } catch (MalformedURLException e) {
        System.out.println("System does not recognize URL: " + url +
                           "; ignoring");
        return;
    }

    server.start();

    url = server.getAddress();

    System.out.println(">>> Creating a client connectint to: "+url);
    JMXConnector conn = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection client = conn.getMBeanServerConnection();

    // add listener from the client side
    Listener listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);

    // ask to send one not serializable notif
    Object[] params = new Object[] {new Integer(1)};
    String[] signatures = new String[] {"java.lang.Integer"};
    client.invoke(emitter, "sendNotserializableNotifs", params, signatures);

    // listener clean
    client.removeNotificationListener(emitter, listener);
    listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);

    //ask to send serializable notifs
    params = new Object[] {new Integer(sentNotifs)};
    client.invoke(emitter, "sendNotifications", params, signatures);

    // waiting ...
    synchronized (listener) {
        while (listener.received() < sentNotifs) {
            listener.wait(); // either pass or test timeout (killed by test harness)

        }
    }

    // clean
    client.removeNotificationListener(emitter, listener);

    conn.close();
    server.stop();
}
 
Example 13
Source File: MultiThreadDeadLockTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    print("Create the MBean server");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();

    print("Initialize environment map");
    HashMap env = new HashMap();

    print("Specify a client socket factory to control socket creation.");
    env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
            clientFactory);

    print("Specify a server idle timeout to make a server close an idle connection.");
    env.put("jmx.remote.x.server.connection.timeout", serverTimeout);

    print("Disable client heartbeat.");
    env.put("jmx.remote.x.client.connection.check.period", 0);

    env.put("jmx.remote.x.notification.fetch.timeout", serverTimeout);

    print("Create an RMI server");
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer server =
            JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
    server.start();

    url = server.getAddress();

    print("Create jmx client on "+url);
    StateMachine.setState(CREATE_SOCKET); // allow to create client socket
    client = JMXConnectorFactory.connect(url, env);
    Thread.sleep(100);

    totoName = new ObjectName("default:name=toto");
    mbs.registerMBean(toto, totoName);
    print("Register the mbean: " + totoName);

    print("Add listener to toto MBean");
    client.getMBeanServerConnection().addNotificationListener(
            totoName, myListener, null, null);
    Thread.sleep(10);

    print("send notif, listener will block the fetcher");
    toto.sendNotif();
    Thread.sleep(100);

    StateMachine.setState(NO_OP);

    print("Sleep 3 times of server idle timeout: "+serverTimeout+
            ", the sever should close the idle connection.");
    Thread.sleep(serverTimeout*3);

    print("start the user thread to call mbean method, it will get IOexception" +
            " and start the reconnection, the socket factory will block the" +
            " socket creation.");
    UserThread ut = new UserThread();
    ut.start();
    Thread.sleep(10);

    print("Free the listener, the fetcher will get IO and makes " +
            "a deadlock if the bug is not fixed.");
    StateMachine.setState(FREE_LISTENER);
    Thread.sleep(100);

    print("Allow to create new socket for the reconnection");
    StateMachine.setState(CREATE_SOCKET);

    print("Check whether the user thread gets free to call the mbean.");
    if (!ut.waitDone(5000)) {
        throw new RuntimeException("Possible deadlock!");
    }

    print("Remove the listener.");
    client.getMBeanServerConnection().removeNotificationListener(
            totoName, myListener, null, null);
    Thread.sleep(serverTimeout*3);

    print("\nWell passed, bye!");

    client.close();
    Thread.sleep(10);
    server.stop();
}
 
Example 14
Source File: MultiThreadDeadLockTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    print("Create the MBean server");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();

    print("Initialize environment map");
    HashMap env = new HashMap();

    print("Specify a client socket factory to control socket creation.");
    env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
            clientFactory);

    print("Specify a server idle timeout to make a server close an idle connection.");
    env.put("jmx.remote.x.server.connection.timeout", serverTimeout);

    print("Disable client heartbeat.");
    env.put("jmx.remote.x.client.connection.check.period", 0);

    env.put("jmx.remote.x.notification.fetch.timeout", serverTimeout);

    print("Create an RMI server");
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer server =
            JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
    server.start();

    url = server.getAddress();

    print("Create jmx client on "+url);
    StateMachine.setState(CREATE_SOCKET); // allow to create client socket
    client = JMXConnectorFactory.connect(url, env);
    Thread.sleep(100);

    totoName = new ObjectName("default:name=toto");
    mbs.registerMBean(toto, totoName);
    print("Register the mbean: " + totoName);

    print("Add listener to toto MBean");
    client.getMBeanServerConnection().addNotificationListener(
            totoName, myListener, null, null);
    Thread.sleep(10);

    print("send notif, listener will block the fetcher");
    toto.sendNotif();
    Thread.sleep(100);

    StateMachine.setState(NO_OP);

    print("Sleep 3 times of server idle timeout: "+serverTimeout+
            ", the sever should close the idle connection.");
    Thread.sleep(serverTimeout*3);

    print("start the user thread to call mbean method, it will get IOexception" +
            " and start the reconnection, the socket factory will block the" +
            " socket creation.");
    UserThread ut = new UserThread();
    ut.start();
    Thread.sleep(10);

    print("Free the listener, the fetcher will get IO and makes " +
            "a deadlock if the bug is not fixed.");
    StateMachine.setState(FREE_LISTENER);
    Thread.sleep(100);

    print("Allow to create new socket for the reconnection");
    StateMachine.setState(CREATE_SOCKET);

    print("Check whether the user thread gets free to call the mbean.");
    if (!ut.waitDone(5000)) {
        throw new RuntimeException("Possible deadlock!");
    }

    print("Remove the listener.");
    client.getMBeanServerConnection().removeNotificationListener(
            totoName, myListener, null, null);
    Thread.sleep(serverTimeout*3);

    print("\nWell passed, bye!");

    client.close();
    Thread.sleep(10);
    server.stop();
}
 
Example 15
Source File: MXBeanTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testInterface(Class<T> c, boolean nullTest)
        throws Exception {

    System.out.println("Testing " + c.getName() +
                       (nullTest ? " for null values" : "") + "...");

    MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
    InvocationHandler ih =
        nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
                   new MXBeanImplInvocationHandler(c, namedMXBeans);
    T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
                                           new Class[] {c},
                                           ih));
    ObjectName on = new ObjectName("test:type=" + c.getName());
    mbs.registerMBean(impl, on);

    System.out.println("Register any MXBeans...");

    Field[] fields = c.getFields();
    for (Field field : fields) {
        String n = field.getName();
        if (n.endsWith("ObjectName")) {
            String objectNameString = (String) field.get(null);
            String base = n.substring(0, n.length() - 10);
            Field f = c.getField(base);
            Object mxbean = f.get(null);
            ObjectName objectName =
                ObjectName.getInstance(objectNameString);
            mbs.registerMBean(mxbean, objectName);
            namedMXBeans.put(objectName, mxbean);
        }
    }

    try {
        testInterface(c, mbsc, on, namedMXBeans, nullTest);
    } finally {
        try {
            cc.close();
        } finally {
            cs.stop();
        }
    }
}
 
Example 16
Source File: MXBeanTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testInterface(Class<T> c, boolean nullTest)
        throws Exception {

    System.out.println("Testing " + c.getName() +
                       (nullTest ? " for null values" : "") + "...");

    MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
    InvocationHandler ih =
        nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
                   new MXBeanImplInvocationHandler(c, namedMXBeans);
    T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
                                           new Class[] {c},
                                           ih));
    ObjectName on = new ObjectName("test:type=" + c.getName());
    mbs.registerMBean(impl, on);

    System.out.println("Register any MXBeans...");

    Field[] fields = c.getFields();
    for (Field field : fields) {
        String n = field.getName();
        if (n.endsWith("ObjectName")) {
            String objectNameString = (String) field.get(null);
            String base = n.substring(0, n.length() - 10);
            Field f = c.getField(base);
            Object mxbean = f.get(null);
            ObjectName objectName =
                ObjectName.getInstance(objectNameString);
            mbs.registerMBean(mxbean, objectName);
            namedMXBeans.put(objectName, mxbean);
        }
    }

    try {
        testInterface(c, mbsc, on, namedMXBeans, nullTest);
    } finally {
        try {
            cc.close();
        } finally {
            cs.stop();
        }
    }
}
 
Example 17
Source File: RMIExitTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void test() {
    try {
        JMXServiceURL u = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer server;
        JMXServiceURL addr;
        JMXConnector client;
        MBeanServerConnection mserver;

        final ObjectName delegateName =
            new ObjectName("JMImplementation:type=MBeanServerDelegate");
        final NotificationListener dummyListener =
            new NotificationListener() {
                    public void handleNotification(Notification n,
                                                   Object o) {
                        // do nothing
                        return;
                    }
                };

        server = JMXConnectorServerFactory.newJMXConnectorServer(u,
                                                                 null,
                                                                 mbs);
        server.start();

        addr = server.getAddress();
        client = JMXConnectorFactory.newJMXConnector(addr, null);
        client.connect(null);

        mserver = client.getMBeanServerConnection();
        String s1 = "1";
        String s2 = "2";
        String s3 = "3";

        mserver.addNotificationListener(delegateName,
                                        dummyListener, null, s1);
        mserver.addNotificationListener(delegateName,
                                        dummyListener, null, s2);
        mserver.addNotificationListener(delegateName,
                                        dummyListener, null, s3);

        mserver.removeNotificationListener(delegateName,
                                           dummyListener, null, s3);
        mserver.removeNotificationListener(delegateName,
                                           dummyListener, null, s2);
        mserver.removeNotificationListener(delegateName,
                                           dummyListener, null, s1);
        client.close();

        server.stop();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        System.exit(1);
    }
}
 
Example 18
Source File: MultiThreadDeadLockTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    print("Create the MBean server");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();

    print("Initialize environment map");
    HashMap env = new HashMap();

    print("Specify a client socket factory to control socket creation.");
    env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
            clientFactory);

    print("Specify a server idle timeout to make a server close an idle connection.");
    env.put("jmx.remote.x.server.connection.timeout", serverTimeout);

    print("Disable client heartbeat.");
    env.put("jmx.remote.x.client.connection.check.period", 0);

    env.put("jmx.remote.x.notification.fetch.timeout", serverTimeout);

    print("Create an RMI server");
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer server =
            JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
    server.start();

    url = server.getAddress();

    print("Create jmx client on "+url);
    StateMachine.setState(CREATE_SOCKET); // allow to create client socket
    client = JMXConnectorFactory.connect(url, env);
    Thread.sleep(100);

    totoName = new ObjectName("default:name=toto");
    mbs.registerMBean(toto, totoName);
    print("Register the mbean: " + totoName);

    print("Add listener to toto MBean");
    client.getMBeanServerConnection().addNotificationListener(
            totoName, myListener, null, null);
    Thread.sleep(10);

    print("send notif, listener will block the fetcher");
    toto.sendNotif();
    Thread.sleep(100);

    StateMachine.setState(NO_OP);

    print("Sleep 3 times of server idle timeout: "+serverTimeout+
            ", the sever should close the idle connection.");
    Thread.sleep(serverTimeout*3);

    print("start the user thread to call mbean method, it will get IOexception" +
            " and start the reconnection, the socket factory will block the" +
            " socket creation.");
    UserThread ut = new UserThread();
    ut.start();
    Thread.sleep(10);

    print("Free the listener, the fetcher will get IO and makes " +
            "a deadlock if the bug is not fixed.");
    StateMachine.setState(FREE_LISTENER);
    Thread.sleep(100);

    print("Allow to create new socket for the reconnection");
    StateMachine.setState(CREATE_SOCKET);

    print("Check whether the user thread gets free to call the mbean.");
    if (!ut.waitDone(5000)) {
        throw new RuntimeException("Possible deadlock!");
    }

    print("Remove the listener.");
    client.getMBeanServerConnection().removeNotificationListener(
            totoName, myListener, null, null);
    Thread.sleep(serverTimeout*3);

    print("\nWell passed, bye!");

    client.close();
    Thread.sleep(10);
    server.stop();
}
 
Example 19
Source File: NotSerializableNotifTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void test(String proto) throws Exception {
    System.out.println("\n>>> Test for protocol " + proto);

    JMXServiceURL url = new JMXServiceURL(proto, null, 0);

    System.out.println(">>> Create a server: "+url);

    JMXConnectorServer server = null;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
    } catch (MalformedURLException e) {
        System.out.println("System does not recognize URL: " + url +
                           "; ignoring");
        return;
    }

    server.start();

    url = server.getAddress();

    System.out.println(">>> Creating a client connectint to: "+url);
    JMXConnector conn = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection client = conn.getMBeanServerConnection();

    // add listener from the client side
    Listener listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);

    // ask to send one not serializable notif
    Object[] params = new Object[] {new Integer(1)};
    String[] signatures = new String[] {"java.lang.Integer"};
    client.invoke(emitter, "sendNotserializableNotifs", params, signatures);

    // listener clean
    client.removeNotificationListener(emitter, listener);
    listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);

    //ask to send serializable notifs
    params = new Object[] {new Integer(sentNotifs)};
    client.invoke(emitter, "sendNotifications", params, signatures);

    // waiting ...
    synchronized (listener) {
        while (listener.received() < sentNotifs) {
            listener.wait(); // either pass or test timeout (killed by test harness)

        }
    }

    // clean
    client.removeNotificationListener(emitter, listener);

    conn.close();
    server.stop();
}
 
Example 20
Source File: MXBeanTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testInterface(Class<T> c, boolean nullTest)
        throws Exception {

    System.out.println("Testing " + c.getName() +
                       (nullTest ? " for null values" : "") + "...");

    MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
    InvocationHandler ih =
        nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
                   new MXBeanImplInvocationHandler(c, namedMXBeans);
    T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
                                           new Class[] {c},
                                           ih));
    ObjectName on = new ObjectName("test:type=" + c.getName());
    mbs.registerMBean(impl, on);

    System.out.println("Register any MXBeans...");

    Field[] fields = c.getFields();
    for (Field field : fields) {
        String n = field.getName();
        if (n.endsWith("ObjectName")) {
            String objectNameString = (String) field.get(null);
            String base = n.substring(0, n.length() - 10);
            Field f = c.getField(base);
            Object mxbean = f.get(null);
            ObjectName objectName =
                ObjectName.getInstance(objectNameString);
            mbs.registerMBean(mxbean, objectName);
            namedMXBeans.put(objectName, mxbean);
        }
    }

    try {
        testInterface(c, mbsc, on, namedMXBeans, nullTest);
    } finally {
        try {
            cc.close();
        } finally {
            cs.stop();
        }
    }
}