1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4927217
  27  * @summary test to reconnect
  28  * @author Shanliang JIANG
  29  * @modules java.management
  30  * @run clean ReconnectTest
  31  * @run build ReconnectTest
  32  * @run main ReconnectTest
  33  */
  34 
  35 import java.util.*;
  36 import java.net.MalformedURLException;
  37 import java.io.IOException;
  38 
  39 import javax.management.*;
  40 import javax.management.remote.*;
  41 
  42 public class ReconnectTest {
  43     private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
  44     private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  45 
  46     private static HashMap env = new HashMap(2);
  47 
  48     static {
  49         String timeout = "1000";
  50         env.put("jmx.remote.x.server.connection.timeout", timeout);
  51         env.put("jmx.remote.x.client.connection.check.period", timeout);
  52     }
  53 
  54     public static void main(String[] args) throws Exception {
  55         System.out.println(">>> test to reconnect.");
  56 
  57 
  58         boolean ok = true;
  59         for (int i = 0; i < protocols.length; i++) {
  60             try {
  61                 if (!test(protocols[i])) {
  62                     System.out.println(">>> Test failed for " + protocols[i]);
  63                     ok = false;
  64                 } else {
  65                     System.out.println(">>> Test successed for " + protocols[i]);
  66                 }
  67             } catch (Exception e) {
  68                 System.out.println(">>> Test failed for " + protocols[i]);
  69                 e.printStackTrace(System.out);
  70                 ok = false;
  71             }
  72         }
  73 
  74         if (ok) {
  75             System.out.println(">>> Test passed");
  76         } else {
  77             System.out.println(">>> TEST FAILED");
  78             System.exit(1);
  79         }
  80     }
  81 
  82     private static boolean test(String proto)
  83             throws Exception {
  84         System.out.println("\n\n>>> Test for protocol " + proto);
  85 
  86         JMXServiceURL u = null;
  87         JMXConnectorServer server = null;
  88 
  89         try {
  90             u = new JMXServiceURL(proto, null, 0);
  91             server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);
  92         } catch (MalformedURLException e) {
  93             System.out.println("Skipping unsupported URL " + proto);
  94             return true;
  95         }
  96 
  97         server.start();
  98         u = server.getAddress();
  99 
 100         JMXConnector conn = JMXConnectorFactory.newJMXConnector(u, env);
 101         conn.connect();
 102         System.out.print("The default domain is ");
 103         System.out.println(conn.getMBeanServerConnection().getDefaultDomain());
 104 
 105         for (int i=0; i<3; i++) {
 106             System.out.println("************** Sleeping ...... "+i);
 107             Thread.sleep(2000);
 108             System.out.println("Sleep done.");
 109 
 110             System.out.println("The default domain is "
 111                   +conn.getMBeanServerConnection().getDefaultDomain());
 112         }
 113 
 114         System.out.println("Close the client ...");
 115 
 116         conn.close();
 117 
 118         System.out.println("Close the server ...");
 119 
 120         server.stop();
 121 
 122         System.out.println("Bye bye.");
 123 
 124         return true;
 125     }
 126 }