UnexpectedNotifTest.java 8.3 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
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
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test
 * @bug 7654321
 * @summary Tests whether a listener receives notifs emitted before the
 * listener is registered.
 * @author Shanliang JIANG
 * @run clean UnexpectedNotifTest
 * @run build UnexpectedNotifTest
 * @run main UnexpectedNotifTest
 */

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectName;
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;
D
duke 已提交
52
//
53
import javax.management.remote.rmi.RMIConnectorServer;
D
duke 已提交
54 55 56 57

public class UnexpectedNotifTest {

    public static void main(String[] args) throws Exception {
58 59
        List<String> protos = new ArrayList<String>();
        protos.add("rmi");
D
duke 已提交
60 61
        try {
            Class.forName("javax.management.remote.jmxmp.JMXMPConnectorServer");
62
            protos.add("jmxmp");
D
duke 已提交
63
        } catch (ClassNotFoundException e) {
64
            // OK: JMXMP not present so don't test it.
D
duke 已提交
65
        }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        for (String proto : protos) {
            test(proto, false);
            test(proto, true);
        }
    }

    private static void test(String proto, boolean eventService)
            throws Exception {
        System.out.println("Unexpected notifications test for protocol " +
                           proto + " with" +
                           (eventService ? "" : "out") + " event service");
        MBeanServer mbs = null;
        try {
            // Create a MBeanServer
            //
            mbs = MBeanServerFactory.createMBeanServer();

            // Create a NotificationEmitter MBean
            //
            mbean = new ObjectName ("Default:name=NotificationEmitter");
            mbs.registerMBean(new NotificationEmitter(), mbean);
D
duke 已提交
87

88 89 90 91 92 93
            // Create a connector server
            //
            url = new JMXServiceURL("service:jmx:" + proto + "://");
            Map<String, String> env = Collections.singletonMap(
                    RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE,
                    Boolean.toString(eventService));
D
duke 已提交
94

95 96 97
            server = JMXConnectorServerFactory.newJMXConnectorServer(url,
                                                                     env,
                                                                     mbs);
D
duke 已提交
98

99 100 101
            mbs.registerMBean(
                        server,
                        new ObjectName("Default:name=ConnectorServer"));
D
duke 已提交
102

103
            server.start();
D
duke 已提交
104

105
            url = server.getAddress();
D
duke 已提交
106

107 108
            for (int j = 0; j < 2; j++) {
                test();
D
duke 已提交
109
            }
110 111 112 113 114 115 116
        } finally {
            // Stop server
            //
            server.stop();
            // Release the MBeanServer
            //
            MBeanServerFactory.releaseMBeanServer(mbs);
D
duke 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        }
    }

    private static void test() throws Exception {
        // Create client
        //
        JMXConnector connector = JMXConnectorFactory.connect(url);
        MBeanServerConnection client = connector.getMBeanServerConnection();

        // Add listener at the client side
        //
        client.addNotificationListener(mbean, listener, null, null);

        // Cleanup
        //
        receivedNotifs = 0;

        // Ask to send notifs
        //
        Object[] params = new Object[] {new Integer(nb)};
        String[] signatures = new String[] {"java.lang.Integer"};

        client.invoke(mbean, "sendNotifications", params, signatures);

        // Waiting...
        //
        synchronized (lock) {
            for (int i = 0; i < 10; i++) {
                if (receivedNotifs < nb) {
                    lock.wait(1000);
                }
            }
        }

        // Waiting again to ensure no more notifs
        //
        Thread.sleep(3000);

        synchronized (lock) {
            if (receivedNotifs != nb) {
                throw new Exception("The client expected to receive " +
                                    nb + " notifs, but got " + receivedNotifs);
            }
        }

        // Remove listener
        //
        client.removeNotificationListener(mbean, listener);

        connector.close();
    }

    //--------------------------
    // private classes
    //--------------------------

    private static class Listener implements NotificationListener {
        public void handleNotification(Notification notif, Object handback) {
            System.out.println("Received: " + notif + " (" +
                               notif.getSequenceNumber() + ")");
            synchronized(lock) {
                if(++receivedNotifs == nb) {
                    lock.notifyAll();
                } else if (receivedNotifs > nb) {
                    System.out.println("The client expected to receive " +
                                       nb + " notifs, but got at least " +
                                       receivedNotifs);
                    System.exit(1);
                }
            }
        }
    }

    public static class NotificationEmitter
        extends NotificationBroadcasterSupport
        implements NotificationEmitterMBean {

        /**
         * Returns a NotificationInfo object containing the name of the Java
         * class of the notification and the notification types sent by this
         * notification broadcaster.
         */
        public MBeanNotificationInfo[] getNotificationInfo() {

            MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];

            String[] ntfTypes = new String[1];
            ntfTypes[0] = myType;

            ntfInfoArray[0] = new MBeanNotificationInfo(
                              ntfTypes,
                              "javax.management.Notification",
                              "Notifications sent by the NotificationEmitter");
            return ntfInfoArray;
        }

        /**
         * Send a Notification object with the specified times.
         * The sequence number will be from zero to times-1.
         *
         * @param nb The number of notifications to send
         */
        public void sendNotifications(Integer nb) {
            System.out.println("NotificationEmitter: asked to send " +
                               "notifications: " + nb);

            Notification notif;
            for (int i = 1; i <= nb.intValue(); i++) {
                notif = new Notification(myType, this, ++seqno);
                sendNotification(notif);
            }
        }

        private String myType = "notification.my_notification";
    }

    public interface NotificationEmitterMBean {
        public void sendNotifications(Integer nb);
    }

    private static JMXConnectorServer server;
    private static JMXServiceURL url;
    private static ObjectName mbean;
    private static NotificationListener listener = new Listener();

    private static int nb = 10;
    private static int receivedNotifs = 0;
    private static int[] lock = new int[0];
    private static volatile long seqno;
}