Java Code Examples for javax.management.remote.JMXConnector.addConnectionNotificationListener()

The following are Jave code examples for showing how to use addConnectionNotificationListener() of the javax.management.remote.JMXConnector class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: jdk8u-jdk   File: NotifReconnectDeadlockTest.java   Source Code and License Vote up 4 votes
public static void main(String[] args) throws Exception {
    System.out.println(
       ">>> Tests reconnection done by a fetching notif thread.");

    ObjectName oname = new ObjectName ("Default:name=NotificationEmitter");
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    Map env = new HashMap(2);
    env.put("jmx.remote.x.server.connection.timeout", new Long(serverTimeout));
    env.put("jmx.remote.x.client.connection.check.period", new Long(Long.MAX_VALUE));

    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    mbs.registerMBean(new NotificationEmitter(), oname);
    JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(
                                                                           url,
                                                                           env,
                                                                           mbs);
    server.start();

    JMXServiceURL addr = server.getAddress();
    JMXConnector client = JMXConnectorFactory.connect(addr, env);

    Thread.sleep(100); // let pass the first client open notif if there is
    client.getMBeanServerConnection().addNotificationListener(oname,
                                                              listener,
                                                              null,
                                                              null);

    client.addConnectionNotificationListener(listener, null, null);

    // max test time: 2 minutes
    final long end = System.currentTimeMillis()+120000;

    synchronized(lock) {
        while(clientState == null && System.currentTimeMillis() < end) {
            mbs.invoke(oname, "sendNotifications",
                       new Object[] {new Notification("MyType", "", 0)},
                       new String[] {"javax.management.Notification"});

            try {
                lock.wait(10);
            } catch (Exception e) {}
        }
    }

    if (clientState == null) {
        throw new RuntimeException(
              "No reconnection happened, need to reconfigure the test.");
    } else if (JMXConnectionNotification.FAILED.equals(clientState) ||
               JMXConnectionNotification.CLOSED.equals(clientState)) {
        throw new RuntimeException("Failed to reconnect.");
    }

    System.out.println(">>> Passed!");

    client.removeConnectionNotificationListener(listener);
    client.close();
    server.stop();
}
 
Example 2
Project: jdk8u-jdk   File: MissingClassTest.java   Source Code and License Vote up 4 votes
private static boolean notifyTest(JMXConnector client,
                                  MBeanServerConnection mbsc)
        throws Exception {
    System.out.println("Send notifications including unknown ones");
    result = new Result();
    LostListener ll = new LostListener();
    client.addConnectionNotificationListener(ll, null, null);
    TestListener nl = new TestListener(ll);
    mbsc.addNotificationListener(on, nl, new TestFilter(), null);
    mbsc.invoke(on, "sendNotifs", NO_OBJECTS, NO_STRINGS);

    // wait for the listeners to receive all their notifs
    // or to fail
    long deadline = System.currentTimeMillis() + 60000;
    long remain;
    while ((remain = deadline - System.currentTimeMillis()) >= 0) {
        synchronized (result) {
            if (result.failed
                || (result.knownCount >= NNOTIFS
                    && result.lostCount >= NNOTIFS*2))
                break;
            result.wait(remain);
        }
    }
    Thread.sleep(2);  // allow any spurious extra notifs to arrive
    if (result.failed) {
        System.out.println("TEST FAILS: Notification strangeness");
        return false;
    } else if (result.knownCount == NNOTIFS
               && result.lostCount == NNOTIFS*2) {
        System.out.println("Success: received known notifications and " +
                           "got NOTIFS_LOST for unknown and " +
                           "unserializable ones");
        return true;
    } else if (result.knownCount >= NNOTIFS
            || result.lostCount >= NNOTIFS*2) {
        System.out.println("TEST FAILS: Received too many notifs: " +
                "known=" + result.knownCount + "; lost=" + result.lostCount);
        return false;
    } else {
        System.out.println("TEST FAILS: Timed out without receiving " +
                           "all notifs: known=" + result.knownCount +
                           "; lost=" + result.lostCount);
        return false;
    }
}
 
Example 3
Project: openjdk-jdk10   File: NotifReconnectDeadlockTest.java   Source Code and License Vote up 4 votes
public static void main(String[] args) throws Exception {
    System.out.println(
       ">>> Tests reconnection done by a fetching notif thread.");

    ObjectName oname = new ObjectName ("Default:name=NotificationEmitter");
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    Map env = new HashMap(2);
    env.put("jmx.remote.x.server.connection.timeout", new Long(serverTimeout));
    env.put("jmx.remote.x.client.connection.check.period", new Long(Long.MAX_VALUE));

    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    mbs.registerMBean(new NotificationEmitter(), oname);
    JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(
                                                                           url,
                                                                           env,
                                                                           mbs);
    server.start();

    JMXServiceURL addr = server.getAddress();
    JMXConnector client = JMXConnectorFactory.connect(addr, env);

    Thread.sleep(100); // let pass the first client open notif if there is
    client.getMBeanServerConnection().addNotificationListener(oname,
                                                              listener,
                                                              null,
                                                              null);

    client.addConnectionNotificationListener(listener, null, null);

    // max test time: 2 minutes
    final long end = System.currentTimeMillis()+120000;

    synchronized(lock) {
        while(clientState == null && System.currentTimeMillis() < end) {
            mbs.invoke(oname, "sendNotifications",
                       new Object[] {new Notification("MyType", "", 0)},
                       new String[] {"javax.management.Notification"});

            try {
                lock.wait(10);
            } catch (Exception e) {}
        }
    }

    if (clientState == null) {
        throw new RuntimeException(
              "No reconnection happened, need to reconfigure the test.");
    } else if (JMXConnectionNotification.FAILED.equals(clientState) ||
               JMXConnectionNotification.CLOSED.equals(clientState)) {
        throw new RuntimeException("Failed to reconnect.");
    }

    System.out.println(">>> Passed!");

    client.removeConnectionNotificationListener(listener);
    client.close();
    server.stop();
}
 
Example 4
Project: openjdk-jdk10   File: MissingClassTest.java   Source Code and License Vote up 4 votes
private static boolean notifyTest(JMXConnector client,
                                  MBeanServerConnection mbsc)
        throws Exception {
    System.out.println("Send notifications including unknown ones");
    result = new Result();
    LostListener ll = new LostListener();
    client.addConnectionNotificationListener(ll, null, null);
    TestListener nl = new TestListener(ll);
    mbsc.addNotificationListener(on, nl, new TestFilter(), null);
    mbsc.invoke(on, "sendNotifs", NO_OBJECTS, NO_STRINGS);

    // wait for the listeners to receive all their notifs
    // or to fail
    long deadline = System.currentTimeMillis() + 60000;
    long remain;
    while ((remain = deadline - System.currentTimeMillis()) >= 0) {
        synchronized (result) {
            if (result.failed
                || (result.knownCount >= NNOTIFS
                    && result.lostCount >= NNOTIFS*2))
                break;
            result.wait(remain);
        }
    }
    Thread.sleep(2);  // allow any spurious extra notifs to arrive
    if (result.failed) {
        System.out.println("TEST FAILS: Notification strangeness");
        return false;
    } else if (result.knownCount == NNOTIFS
               && result.lostCount == NNOTIFS*2) {
        System.out.println("Success: received known notifications and " +
                           "got NOTIFS_LOST for unknown and " +
                           "unserializable ones");
        return true;
    } else if (result.knownCount >= NNOTIFS
            || result.lostCount >= NNOTIFS*2) {
        System.out.println("TEST FAILS: Received too many notifs: " +
                "known=" + result.knownCount + "; lost=" + result.lostCount);
        return false;
    } else {
        System.out.println("TEST FAILS: Timed out without receiving " +
                           "all notifs: known=" + result.knownCount +
                           "; lost=" + result.lostCount);
        return false;
    }
}