Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/ReconnectTest.java
38867 views
/*1* Copyright (c) 2003, 2004, 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*/2223/*24* @test25* @bug 492721726* @summary test to reconnect27* @author Shanliang JIANG28* @run clean ReconnectTest29* @run build ReconnectTest30* @run main ReconnectTest31*/3233import java.util.*;34import java.net.MalformedURLException;35import java.io.IOException;3637import javax.management.*;38import javax.management.remote.*;3940public class ReconnectTest {41private static final String[] protocols = {"rmi", "iiop", "jmxmp"};42private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4344private static HashMap env = new HashMap(2);4546static {47String timeout = "1000";48env.put("jmx.remote.x.server.connection.timeout", timeout);49env.put("jmx.remote.x.client.connection.check.period", timeout);50}5152public static void main(String[] args) throws Exception {53System.out.println(">>> test to reconnect.");545556boolean ok = true;57for (int i = 0; i < protocols.length; i++) {58try {59if (!test(protocols[i])) {60System.out.println(">>> Test failed for " + protocols[i]);61ok = false;62} else {63System.out.println(">>> Test successed for " + protocols[i]);64}65} catch (Exception e) {66System.out.println(">>> Test failed for " + protocols[i]);67e.printStackTrace(System.out);68ok = false;69}70}7172if (ok) {73System.out.println(">>> Test passed");74} else {75System.out.println(">>> TEST FAILED");76System.exit(1);77}78}7980private static boolean test(String proto)81throws Exception {82System.out.println("\n\n>>> Test for protocol " + proto);8384JMXServiceURL u = null;85JMXConnectorServer server = null;8687try {88u = new JMXServiceURL(proto, null, 0);89server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);90} catch (MalformedURLException e) {91System.out.println("Skipping unsupported URL " + proto);92return true;93}9495server.start();96u = server.getAddress();9798JMXConnector conn = JMXConnectorFactory.newJMXConnector(u, env);99conn.connect();100System.out.print("The default domain is ");101System.out.println(conn.getMBeanServerConnection().getDefaultDomain());102103for (int i=0; i<3; i++) {104System.out.println("************** Sleeping ...... "+i);105Thread.sleep(2000);106System.out.println("Sleep done.");107108System.out.println("The default domain is "109+conn.getMBeanServerConnection().getDefaultDomain());110}111112System.out.println("Close the client ...");113114conn.close();115116System.out.println("Close the server ...");117118server.stop();119120System.out.println("Bye bye.");121122return true;123}124}125126127