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