1 /* 2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 * 30 * @run clean NotifBufferSizePropertyNameTest 31 * @run build NotifBufferSizePropertyNameTest 32 * @run main NotifBufferSizePropertyNameTest 33 */ 34 35 import java.io.IOException; 36 import java.util.*; 37 38 import javax.management.*; 39 import javax.management.remote.*; 40 41 /** 42 * This class tests also the size of a server notification buffer. 43 */ 44 public class NotifBufferSizePropertyNameTest { 45 46 private static ObjectName oname; 47 private static JMXServiceURL url; 48 private final static NotificationListener listener = new NotificationListener() { 49 public void handleNotification(Notification n, Object hb) { 50 // nothing 51 } 52 }; 53 54 public static void main(String[] args) throws Exception { 55 System.out.println( 56 "Verify the property name specifying the server notification buffer size."); 57 58 oname = new ObjectName ("Default:name=NotificationEmitter"); 59 url = new JMXServiceURL("rmi", null, 0); 60 Map env = new HashMap(2); 61 62 System.out.println("Test the new property name."); 63 env.put("jmx.remote.x.notification.buffer.size", String.valueOf(bufferSize)); 64 test(env); 65 66 System.out.println("Test the old property name."); 67 env.remove("jmx.remote.x.notification.buffer.size"); 68 env.put("jmx.remote.x.buffer.size", String.valueOf(bufferSize)); 69 test(env); 70 71 System.out.println("Test that the new property name overwrite the old one."); 72 env.put("jmx.remote.x.notification.buffer.size", String.valueOf(bufferSize)); 73 env.put("jmx.remote.x.buffer.size", String.valueOf(bufferSize*6)); 74 test(env); 75 76 System.out.println("Test the old property name on system."); 77 System.setProperty("jmx.remote.x.buffer.size", String.valueOf(bufferSize)); 78 test(null); 79 80 System.out.println( 81 "Test that the new property name overwrite the old one on system."); 82 System.setProperty("jmx.remote.x.notification.buffer.size", 83 String.valueOf(bufferSize)); 84 System.setProperty("jmx.remote.x.buffer.size", String.valueOf(bufferSize*6)); 85 test(null); 86 } 87 88 89 private static void test(Map env) throws Exception { 90 final MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 91 92 mbs.registerMBean(new NotificationEmitter(), oname); 93 JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer( 94 url, 95 env, 96 mbs); 97 server.start(); 98 99 JMXServiceURL addr = server.getAddress(); 100 JMXConnector client = JMXConnectorFactory.connect(addr); 101 client.getMBeanServerConnection().addNotificationListener(oname, 102 listener, 103 null, 104 null); 105 106 Thread.sleep(10); // give time to other notifs 107 weakNotifs.clear(); 108 109 // send notifd 110 mbs.invoke(oname, "sendNotifications", 111 new Object[] {new Integer(toSend)}, 112 new String[] {"java.lang.Integer"}); 113 114 client.close(); 115 client = null; 116 117 // give time to GC 118 for(int i=0; i<200; i++) { 119 if (weakNotifs.keySet().size() > bufferSize) { 120 Thread.sleep(10); 121 System.gc(); 122 } else { 123 break; 124 } 125 } 126 127 // check 128 if (weakNotifs.keySet().size() != bufferSize) { 129 throw new RuntimeException("The buffer size is not correctly specified."+ 130 "\nExpected to be <= "+bufferSize+", but got "+weakNotifs.keySet().size()); 131 } 132 133 server.stop(); 134 server = null; 135 } 136 137 //-------------------------- 138 // private classes 139 //-------------------------- 140 public static class NotificationEmitter extends NotificationBroadcasterSupport 141 implements NotificationEmitterMBean { 142 143 public void sendNotifications(Integer nb) { 144 Notification notif; 145 for (int i=1; i<=nb.intValue(); i++) { 146 notif = new Notification("MyType", this, i); 147 weakNotifs.put(notif, null); 148 sendNotification(notif); 149 } 150 } 151 } 152 153 public interface NotificationEmitterMBean { 154 public void sendNotifications(Integer nb); 155 } 156 157 private static final int toSend = 20; 158 private static final int bufferSize = 10; 159 private static WeakHashMap weakNotifs = new WeakHashMap(toSend); 160 161 }