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