1 /* 2 * Copyright (c) 2004, 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 26 * @bug 5039210 27 * @summary test on a client notification deadlock. 28 * @author Shanliang JIANG 29 * @run clean DeadLockTest 30 * @run build DeadLockTest 31 * @run main DeadLockTest 32 */ 33 34 import java.net.MalformedURLException; 35 import java.io.IOException; 36 import java.util.HashMap; 37 38 import javax.management.*; 39 import javax.management.remote.*; 40 41 public class DeadLockTest { 42 private static final String[] protocols = {"rmi", "iiop", "jmxmp"}; 43 private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 44 45 public static void main(String[] args) { 46 System.out.println(">>> test on a client notification deadlock."); 47 48 boolean ok = true; 49 for (int i = 0; i < protocols.length; i++) { 50 try { 51 test(protocols[i]); 52 } catch (Exception e) { 53 System.out.println(">>> Test failed for " + protocols[i]); 54 e.printStackTrace(System.out); 55 } 56 } 57 58 System.out.println(">>> Test passed"); 59 } 60 61 private static void test(String proto) 62 throws Exception { 63 System.out.println(">>> Test for protocol " + proto); 64 65 JMXServiceURL u = null; 66 JMXConnectorServer server = null; 67 68 HashMap env = new HashMap(2); 69 // server will close a client connection after 1 second 70 env.put("jmx.remote.x.server.connection.timeout", "1000"); 71 72 // disable the client ping 73 env.put("jmx.remote.x.client.connection.check.period", "0"); 74 75 try { 76 u = new JMXServiceURL(proto, null, 0); 77 server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs); 78 } catch (MalformedURLException e) { 79 System.out.println(">>> Skipping unsupported URL " + proto); 80 } 81 82 server.start(); 83 84 JMXServiceURL addr = server.getAddress(); 85 86 long st = 2000; 87 MyListener myListener; 88 89 // a cycle to make sure that we test the blocking problem. 90 do { 91 JMXConnector client = JMXConnectorFactory.connect(addr, env); 92 MBeanServerConnection conn = client.getMBeanServerConnection(); 93 myListener = new MyListener(conn); 94 client.addConnectionNotificationListener(myListener, null, null); 95 96 // wait the server to close the client connection 97 Thread.sleep(st); 98 99 // makes the listener to do a remote request via the connection 100 // which should be closed by the server. 101 conn.getDefaultDomain(); 102 103 // allow the listner to have time to work 104 Thread.sleep(100); 105 106 // get a closed notif, should no block. 107 client.close(); 108 Thread.sleep(100); 109 110 st += 2000; 111 112 } while(!myListener.isDone()); 113 114 server.stop(); 115 } 116 117 private static class MyListener implements NotificationListener { 118 public MyListener(MBeanServerConnection conn) { 119 this.conn = conn; 120 } 121 122 public void handleNotification(Notification n, Object h) { 123 if (n instanceof JMXConnectionNotification) { 124 JMXConnectionNotification jcn = (JMXConnectionNotification)n; 125 final String type = jcn.getType(); 126 System.out.println(">>> The listener receives notif with the type:"+type); 127 128 if (JMXConnectionNotification.CLOSED.equals(type) || 129 JMXConnectionNotification.FAILED.equals(type)) { 130 131 synchronized(this) { 132 done = false; 133 } 134 135 try { 136 conn.getDefaultDomain(); 137 } catch (IOException ioe) { 138 // Greate ! 139 } 140 141 synchronized(this) { 142 done = true; 143 } 144 145 System.out.println(">>> The listener is not blocked!"); 146 } 147 } 148 } 149 150 public boolean isDone() { 151 synchronized(this) { 152 return done; 153 } 154 } 155 156 private boolean done = false; 157 private MBeanServerConnection conn; 158 } 159 }