jdk/test/javax/management/remote/mandatory/notif/NotifBufferSizePropertyNameTest.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1247 b4c26443dee5
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
     2
 * Copyright 2004-2005 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test NotifBufferSizePropertyNameTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6174229
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify the property name specifying server notification buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Shanliang JIANG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean NotifBufferSizePropertyNameTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build NotifBufferSizePropertyNameTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main NotifBufferSizePropertyNameTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    34
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class tests also the size of a server notification buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class NotifBufferSizePropertyNameTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static ObjectName oname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static JMXServiceURL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final static NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                public void handleNotification(Notification n, Object hb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
           "Verify the property name specifying the server notification buffer size.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        oname = new ObjectName ("Default:name=NotificationEmitter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Map env = new HashMap(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println("Test the new property name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        env.put("jmx.remote.x.notification.buffer.size", String.valueOf(bufferSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        test(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println("Test the old property name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        env.remove("jmx.remote.x.notification.buffer.size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        env.put("jmx.remote.x.buffer.size", String.valueOf(bufferSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        test(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.println("Test that the new property name overwrite the old one.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        env.put("jmx.remote.x.notification.buffer.size", String.valueOf(bufferSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        env.put("jmx.remote.x.buffer.size", String.valueOf(bufferSize*6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        test(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.println("Test the old property name on system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.setProperty("jmx.remote.x.buffer.size", String.valueOf(bufferSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        test(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
             "Test that the new property name overwrite the old one on system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.setProperty("jmx.remote.x.notification.buffer.size",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                           String.valueOf(bufferSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.setProperty("jmx.remote.x.buffer.size", String.valueOf(bufferSize*6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        test(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static void test(Map env) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        mbs.registerMBean(new NotificationEmitter(), oname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                                               url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                                               env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                                               mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        JMXServiceURL addr = server.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JMXConnector client = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        client.getMBeanServerConnection().addNotificationListener(oname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                                                  listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                                                  null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                                                  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Thread.sleep(10); // give time to other notifs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        weakNotifs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // send notifd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        mbs.invoke(oname, "sendNotifications",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                   new Object[] {new Integer(toSend)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                   new String[] {"java.lang.Integer"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        client = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // give time to GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        for(int i=0; i<200; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (weakNotifs.keySet().size() > bufferSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (weakNotifs.keySet().size() != bufferSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new RuntimeException("The buffer size is not correctly specified."+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                   "\nExpected to be <= "+bufferSize+", but got "+weakNotifs.keySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
//--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
// private classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
//--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static class NotificationEmitter extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        implements NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public void sendNotifications(Integer nb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Notification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            for (int i=1; i<=nb.intValue(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                notif = new Notification("MyType", this, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                weakNotifs.put(notif, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public interface NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public void sendNotifications(Integer nb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int toSend = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final int bufferSize = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static WeakHashMap weakNotifs = new WeakHashMap(toSend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}