Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/RMIExitTest.java
38867 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @bug 491723727* @summary test that process exit immediately after stop() / close() called28* @author Jean Francois Denise29* @run clean RMIExitTest30* @run build RMIExitTest31* @run main RMIExitTest32*/3334import java.net.MalformedURLException;35import java.io.IOException;3637import javax.management.MBeanServerFactory;38import javax.management.MBeanServer;39import javax.management.ObjectName;40import javax.management.MBeanServerConnection;41import javax.management.NotificationListener;42import javax.management.Notification;4344import javax.management.remote.JMXServiceURL;45import javax.management.remote.JMXConnectorServer;46import javax.management.remote.JMXConnector;47import javax.management.remote.JMXConnectorServerFactory;48import javax.management.remote.JMXConnectorFactory;4950/**51* VM shutdown hook. Test that the hook is called less than 5 secs52* after expected exit.53*/54class TimeChecker extends Thread {55@Override56public void run() {57System.out.println("shutdown hook called");58long elapsedTime =59System.currentTimeMillis() - RMIExitTest.exitStartTime;60if(elapsedTime >= 5000) {61System.out.println("BUG 4917237 not Fixed.");62// Once in hook, to provide an exit status != 0, halt must63// be called. Hooks are not called when halt is called.64Runtime.getRuntime().halt(1);65} else {66System.out.println("BUG 4917237 Fixed");67}68}69}7071/**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*/76public class RMIExitTest {77private static final MBeanServer mbs =78MBeanServerFactory.createMBeanServer();79public static long exitStartTime = 0;8081public static void main(String[] args) {82System.out.println("Start test");83Runtime.getRuntime().addShutdownHook(new TimeChecker());84test();85exitStartTime = System.currentTimeMillis();86System.out.println("End test");87}8889private static void test() {90try {91JMXServiceURL u = new JMXServiceURL("rmi", null, 0);92JMXConnectorServer server;93JMXServiceURL addr;94JMXConnector client;95MBeanServerConnection mserver;9697final ObjectName delegateName =98new ObjectName("JMImplementation:type=MBeanServerDelegate");99final NotificationListener dummyListener =100new NotificationListener() {101public void handleNotification(Notification n,102Object o) {103// do nothing104return;105}106};107108server = JMXConnectorServerFactory.newJMXConnectorServer(u,109null,110mbs);111server.start();112113addr = server.getAddress();114client = JMXConnectorFactory.newJMXConnector(addr, null);115client.connect(null);116117mserver = client.getMBeanServerConnection();118String s1 = "1";119String s2 = "2";120String s3 = "3";121122mserver.addNotificationListener(delegateName,123dummyListener, null, s1);124mserver.addNotificationListener(delegateName,125dummyListener, null, s2);126mserver.addNotificationListener(delegateName,127dummyListener, null, s3);128129mserver.removeNotificationListener(delegateName,130dummyListener, null, s3);131mserver.removeNotificationListener(delegateName,132dummyListener, null, s2);133mserver.removeNotificationListener(delegateName,134dummyListener, null, s1);135client.close();136137server.stop();138} catch (Exception e) {139System.out.println(e);140e.printStackTrace();141System.exit(1);142}143}144}145146147