Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/ReconnectTest.java
41159 views
/*1* Copyright (c) 2003, 2020, 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* @library /test/lib29* @run clean ReconnectTest30* @run build ReconnectTest31* @run main ReconnectTest32*/3334import jdk.test.lib.Utils;3536import java.util.*;37import java.net.MalformedURLException;3839import javax.management.*;40import javax.management.remote.*;4142public class ReconnectTest {43private static final String[] protocols = {"rmi", "iiop", "jmxmp"};44private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4546private static HashMap env = new HashMap(2);4748static {49String timeout = Long.toString(Utils.adjustTimeout(1000));50env.put("jmx.remote.x.server.connection.timeout", timeout);51env.put("jmx.remote.x.client.connection.check.period", timeout);52}5354public static void main(String[] args) throws Exception {55System.out.println(">>> test to reconnect.");565758boolean ok = true;59for (int i = 0; i < protocols.length; i++) {60try {61if (!test(protocols[i])) {62System.out.println(">>> Test failed for " + protocols[i]);63ok = false;64} else {65System.out.println(">>> Test successed for " + protocols[i]);66}67} catch (Exception e) {68System.out.println(">>> Test failed for " + protocols[i]);69e.printStackTrace(System.out);70ok = false;71}72}7374if (ok) {75System.out.println(">>> Test passed");76} else {77System.out.println(">>> TEST FAILED");78System.exit(1);79}80}8182private static boolean test(String proto)83throws Exception {84System.out.println("\n\n>>> Test for protocol " + proto);8586JMXServiceURL u = null;87JMXConnectorServer server = null;8889try {90u = new JMXServiceURL(proto, null, 0);91server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);92} catch (MalformedURLException e) {93System.out.println("Skipping unsupported URL " + proto);94return true;95}9697server.start();98u = server.getAddress();99100JMXConnector conn = JMXConnectorFactory.newJMXConnector(u, env);101conn.connect();102System.out.print("The default domain is ");103System.out.println(conn.getMBeanServerConnection().getDefaultDomain());104105for (int i=0; i<3; i++) {106System.out.println("************** Sleeping ...... "+i);107Thread.sleep(Utils.adjustTimeout(2000));108System.out.println("Sleep done.");109110System.out.println("The default domain is "111+conn.getMBeanServerConnection().getDefaultDomain());112}113114System.out.println("Close the client ...");115116conn.close();117118System.out.println("Close the server ...");119120server.stop();121122System.out.println("Bye bye.");123124return true;125}126}127128129