J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 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 | /* |
| 26 | * @test |
| 27 | * @bug 4917237 |
| 28 | * @summary test that process exit immediately after stop() / close() called |
| 29 | * @author Jean Francois Denise |
| 30 | * @run clean RMIExitTest |
| 31 | * @run build RMIExitTest |
| 32 | * @run main RMIExitTest |
| 33 | */ |
| 34 | |
| 35 | import java.net.MalformedURLException; |
| 36 | import java.io.IOException; |
| 37 | |
| 38 | import javax.management.MBeanServerFactory; |
| 39 | import javax.management.MBeanServer; |
| 40 | import javax.management.ObjectName; |
| 41 | import javax.management.MBeanServerConnection; |
| 42 | import javax.management.NotificationListener; |
| 43 | import javax.management.Notification; |
| 44 | |
| 45 | import javax.management.remote.JMXServiceURL; |
| 46 | import javax.management.remote.JMXConnectorServer; |
| 47 | import javax.management.remote.JMXConnector; |
| 48 | import javax.management.remote.JMXConnectorServerFactory; |
| 49 | import javax.management.remote.JMXConnectorFactory; |
| 50 | |
| 51 | /** |
| 52 | * VM shutdown hook. Test that the hook is called less than 5 secs |
| 53 | * after expected exit. |
| 54 | */ |
| 55 | class TimeChecker extends Thread { |
| 56 | public void run() { |
| 57 | System.out.println("shutdown hook called"); |
| 58 | long elapsedTime = |
| 59 | System.currentTimeMillis() - RMIExitTest.exitStartTime; |
| 60 | if(elapsedTime >= 5000) { |
| 61 | System.out.println("BUG 4917237 not Fixed."); |
| 62 | // Once in hook, to provide an exit status != 0, halt must |
| 63 | // be called. Hooks are not called when halt is called. |
| 64 | Runtime.getRuntime().halt(1); |
| 65 | } else { |
| 66 | System.out.println("BUG 4917237 Fixed"); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Start a server, connect a client, add/remove listeners, close client, |
| 73 | * stop server. Check that VM exits in less than 5 secs. |
| 74 | * |
| 75 | */ |
| 76 | public class RMIExitTest { |
| 77 | private static final MBeanServer mbs = |
| 78 | MBeanServerFactory.createMBeanServer(); |
| 79 | public static long exitStartTime = 0; |
| 80 | |
| 81 | public static void main(String[] args) { |
| 82 | System.out.println("Start test"); |
| 83 | Runtime.getRuntime().addShutdownHook(new TimeChecker()); |
| 84 | test(); |
| 85 | exitStartTime = System.currentTimeMillis(); |
| 86 | System.out.println("End test"); |
| 87 | } |
| 88 | |
| 89 | private static void test() { |
| 90 | try { |
| 91 | JMXServiceURL u = new JMXServiceURL("rmi", null, 0); |
| 92 | JMXConnectorServer server; |
| 93 | JMXServiceURL addr; |
| 94 | JMXConnector client; |
| 95 | MBeanServerConnection mserver; |
| 96 | |
| 97 | final ObjectName delegateName = |
| 98 | new ObjectName("JMImplementation:type=MBeanServerDelegate"); |
| 99 | final NotificationListener dummyListener = |
| 100 | new NotificationListener() { |
| 101 | public void handleNotification(Notification n, |
| 102 | Object o) { |
| 103 | // do nothing |
| 104 | return; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | server = JMXConnectorServerFactory.newJMXConnectorServer(u, |
| 109 | null, |
| 110 | mbs); |
| 111 | server.start(); |
| 112 | |
| 113 | addr = server.getAddress(); |
| 114 | client = JMXConnectorFactory.newJMXConnector(addr, null); |
| 115 | client.connect(null); |
| 116 | |
| 117 | mserver = client.getMBeanServerConnection(); |
| 118 | String s1 = "1"; |
| 119 | String s2 = "2"; |
| 120 | String s3 = "3"; |
| 121 | |
| 122 | mserver.addNotificationListener(delegateName, |
| 123 | dummyListener, null, s1); |
| 124 | mserver.addNotificationListener(delegateName, |
| 125 | dummyListener, null, s2); |
| 126 | mserver.addNotificationListener(delegateName, |
| 127 | dummyListener, null, s3); |
| 128 | |
| 129 | mserver.removeNotificationListener(delegateName, |
| 130 | dummyListener, null, s3); |
| 131 | mserver.removeNotificationListener(delegateName, |
| 132 | dummyListener, null, s2); |
| 133 | mserver.removeNotificationListener(delegateName, |
| 134 | dummyListener, null, s1); |
| 135 | client.close(); |
| 136 | |
| 137 | server.stop(); |
| 138 | } catch (Exception e) { |
| 139 | System.out.println(e); |
| 140 | e.printStackTrace(); |
| 141 | System.exit(1); |
| 142 | } |
| 143 | } |
| 144 | } |