UsingEventService.java 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 * @test UsingEventService.java 1.10 08/01/22
 * @bug 5108776
 * @summary Basic test for EventManager.
 * @author Shanliang JIANG
 * @run clean UsingEventService
 * @run build UsingEventService
 * @run main UsingEventService
 */

import java.util.HashMap;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.event.EventConsumer;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class UsingEventService {
    private static JMXServiceURL url;
    private static JMXConnectorServer server;
    private static JMXConnector conn;
    private static MBeanServerConnection client;

    public static void main(String[] args) throws Exception {
        if (System.getProperty("java.version").startsWith("1.5")) {
            System.out.println(">>> UsingEventService-main not available for JDK1.5, bye");
            return;
        }

        ObjectName oname = new ObjectName("test:t=t");
        Notification n = new Notification("", oname, 0);

        System.out.println(">>> UsingEventService-main basic tests ...");
        MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();

        url = new JMXServiceURL("rmi", null, 0) ;
        JMXConnectorServer server =
                JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
        server.start();
        url = server.getAddress();

        System.out.println(">>> UsingEventService-main test to not use the event service...");
        conn = JMXConnectorFactory.connect(url, null);
        client = conn.getMBeanServerConnection();

        System.out.println(">>> UsingEventService-main test to use the event service...");
        Map env = new HashMap(1);
        env.put("jmx.remote.use.event.service", "true");
        conn = JMXConnectorFactory.connect(url, env);
        client = conn.getMBeanServerConnection();

        ((EventConsumer)client).subscribe(oname, listener, null, null);

        System.out.println(">>> UsingEventService-main using event service as expected!");

        System.out.println(">>> UsingEventService-main test to use" +
                " the event service with system property...");

        System.setProperty("jmx.remote.use.event.service", "true");
        conn = JMXConnectorFactory.connect(url, null);
        client = conn.getMBeanServerConnection();

        ((EventConsumer)client).subscribe(oname, listener, null, null);

        System.out.println("" +
                ">>> UsingEventService-main using event service as expected!");

        System.out.println(">>> Happy bye bye!");
    }

    private final static NotificationListener listener = new NotificationListener() {
        public void handleNotification(Notification n, Object hk) {
            //
        }
    };
}