Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/RMIExitTest.java
41159 views
/*1* Copyright (c) 2003, 2015, 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*30* @run clean RMIExitTest31* @run build RMIExitTest32* @run main RMIExitTest33*/3435import java.net.MalformedURLException;36import java.io.IOException;3738import javax.management.MBeanServerFactory;39import javax.management.MBeanServer;40import javax.management.ObjectName;41import javax.management.MBeanServerConnection;42import javax.management.NotificationListener;43import javax.management.Notification;4445import javax.management.remote.JMXServiceURL;46import javax.management.remote.JMXConnectorServer;47import javax.management.remote.JMXConnector;48import javax.management.remote.JMXConnectorServerFactory;49import javax.management.remote.JMXConnectorFactory;5051/**52* VM shutdown hook. Test that the hook is called less than 5 secs53* after expected exit.54*/55class TimeChecker extends Thread {56@Override57public void run() {58System.out.println("shutdown hook called");59long elapsedTime =60System.currentTimeMillis() - RMIExitTest.exitStartTime;61if(elapsedTime >= 5000) {62System.out.println("BUG 4917237 not Fixed.");63// Once in hook, to provide an exit status != 0, halt must64// be called. Hooks are not called when halt is called.65Runtime.getRuntime().halt(1);66} else {67System.out.println("BUG 4917237 Fixed");68}69}70}7172/**73* Start a server, connect a client, add/remove listeners, close client,74* stop server. Check that VM exits in less than 5 secs.75*76*/77public class RMIExitTest {78private static final MBeanServer mbs =79MBeanServerFactory.createMBeanServer();80public static long exitStartTime = 0;8182public static void main(String[] args) {83System.out.println("Start test");84Runtime.getRuntime().addShutdownHook(new TimeChecker());85test();86exitStartTime = System.currentTimeMillis();87System.out.println("End test");88}8990private static void test() {91try {92JMXServiceURL u = new JMXServiceURL("rmi", null, 0);93JMXConnectorServer server;94JMXServiceURL addr;95JMXConnector client;96MBeanServerConnection mserver;9798final ObjectName delegateName =99new ObjectName("JMImplementation:type=MBeanServerDelegate");100final NotificationListener dummyListener =101new NotificationListener() {102public void handleNotification(Notification n,103Object o) {104// do nothing105return;106}107};108109server = JMXConnectorServerFactory.newJMXConnectorServer(u,110null,111mbs);112server.start();113114addr = server.getAddress();115client = JMXConnectorFactory.newJMXConnector(addr, null);116client.connect(null);117118mserver = client.getMBeanServerConnection();119String s1 = "1";120String s2 = "2";121String s3 = "3";122123mserver.addNotificationListener(delegateName,124dummyListener, null, s1);125mserver.addNotificationListener(delegateName,126dummyListener, null, s2);127mserver.addNotificationListener(delegateName,128dummyListener, null, s3);129130mserver.removeNotificationListener(delegateName,131dummyListener, null, s3);132mserver.removeNotificationListener(delegateName,133dummyListener, null, s2);134mserver.removeNotificationListener(delegateName,135dummyListener, null, s1);136client.close();137138server.stop();139} catch (Exception e) {140System.out.println(e);141e.printStackTrace();142System.exit(1);143}144}145}146147148