8077423: jstatd is not terminated even though it cannot contact or bind to RMI Registry
authorykantser
Mon, 20 Apr 2015 08:45:54 +0200
changeset 30357 6c0dc3853096
parent 30356 a56e57aad51f
child 30358 15b7f8ff367c
8077423: jstatd is not terminated even though it cannot contact or bind to RMI Registry Reviewed-by: sla
jdk/src/jdk.jvmstat/share/classes/sun/tools/jstatd/Jstatd.java
jdk/test/sun/tools/jstatd/JstatdTest.java
jdk/test/sun/tools/jstatd/TestJstatdExternalRegistry.java
--- a/jdk/src/jdk.jvmstat/share/classes/sun/tools/jstatd/Jstatd.java	Fri Apr 17 14:37:44 2015 -0700
+++ b/jdk/src/jdk.jvmstat/share/classes/sun/tools/jstatd/Jstatd.java	Mon Apr 20 08:45:54 2015 +0200
@@ -64,15 +64,9 @@
                 int localport = (port < 0) ? Registry.REGISTRY_PORT : port;
                 registry = LocateRegistry.createRegistry(localport);
                 bind(name, remoteHost);
+            } else {
+                throw e;
             }
-            else {
-                System.out.println("Could not contact registry\n"
-                                   + e.getMessage());
-                e.printStackTrace();
-            }
-        } catch (RemoteException e) {
-            System.err.println("Could not bind " + name + " to RMI Registry");
-            e.printStackTrace();
         }
     }
 
@@ -148,19 +142,22 @@
             if (rminame != null) {
                 System.out.println("Bad RMI server name: " + rminame);
             } else {
-                System.out.println("Bad RMI URL: " + name + " : "
-                                   + e.getMessage());
+                System.out.println("Bad RMI URL: " + name);
             }
+            e.printStackTrace(System.out);
             System.exit(1);
         } catch (java.rmi.ConnectException e) {
             // could not attach to or create a registry
-            System.out.println("Could not contact RMI registry\n"
-                               + e.getMessage());
+            System.out.println("Could not contact RMI registry");
+            e.printStackTrace(System.out);
+            System.exit(1);
+        } catch (RemoteException e) {
+            System.out.println("Could not bind " + name + " to RMI Registry");
+            e.printStackTrace(System.out);
             System.exit(1);
         } catch (Exception e) {
-            System.out.println("Could not create remote object\n"
-                               + e.getMessage());
-            e.printStackTrace();
+            System.out.println("Could not create remote object");
+            e.printStackTrace(System.out);
             System.exit(1);
         }
     }
--- a/jdk/test/sun/tools/jstatd/JstatdTest.java	Fri Apr 17 14:37:44 2015 -0700
+++ b/jdk/test/sun/tools/jstatd/JstatdTest.java	Mon Apr 20 08:45:54 2015 +0200
@@ -135,30 +135,12 @@
         log("Start jps", cmd);
 
         ProcessBuilder processBuilder = new ProcessBuilder(cmd);
-        OutputAnalyzer output = waitForJstatdRMI(processBuilder);
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
     }
 
-    private OutputAnalyzer waitForJstatdRMI(ProcessBuilder pb) throws Exception {
-        OutputAnalyzer output = ProcessTools.executeProcess(pb);
-
-        String remoteHost = (serverName != null) ? serverName : "JStatRemoteHost";
-        while (output.getExitValue() != 0) {
-            String out = output.getOutput();
-
-            if (out.contains("RMI Registry not available") ||
-                out.contains("RMI Server " + remoteHost + " not available")) {
-                Thread.sleep(100);
-                output = ProcessTools.executeProcess(pb);
-            } else {
-                output.shouldHaveExitValue(0);
-            }
-        }
-        return output;
-    }
-
     /**
      * Verifies output form jps contains pids and programs' name information.
      * The function will discard any lines that come before the first line with pid.
@@ -208,7 +190,7 @@
         log("Start jstat", cmd);
 
         ProcessBuilder processBuilder = new ProcessBuilder(cmd);
-        OutputAnalyzer output = waitForJstatdRMI(processBuilder);
+        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
         System.out.println(output.getOutput());
 
         return output;
@@ -282,6 +264,9 @@
             launcher.addToolArg("-n");
             launcher.addToolArg(serverName);
         }
+        if (withExternalRegistry) {
+            launcher.addToolArg("-nr");
+        }
 
         String[] cmd = launcher.getCommand();
         log("Start jstatd", cmd);
@@ -315,17 +300,21 @@
     }
 
     public void doTest() throws Throwable {
+        if (useDefaultPort) {
+            verifyNoRmiRegistryOnDefaultPort();
+        }
+
         ProcessThread jstatdThread = null;
         try {
             while (jstatdThread == null) {
-                if (!useDefaultPort || withExternalRegistry) {
+                if (!useDefaultPort) {
                     port = String.valueOf(Utils.getFreePort());
                 }
 
                 if (withExternalRegistry) {
                     Registry registry = startRegistry();
                     if (registry == null) {
-                        // The port is already in use. Cancel and try with new one.
+                        // The port is already in use. Cancel and try with a new one.
                         continue;
                     }
                 }
@@ -347,4 +336,14 @@
                 "jstatd process exited with unexpected exit code");
     }
 
+    private void verifyNoRmiRegistryOnDefaultPort() throws Exception {
+        try {
+            Registry registry = LocateRegistry.getRegistry();
+            registry.list();
+            throw new Exception("There is already RMI registry on the default port: " + registry);
+        } catch (RemoteException e) {
+            // No RMI registry on default port is detected
+        }
+    }
+
 }
--- a/jdk/test/sun/tools/jstatd/TestJstatdExternalRegistry.java	Fri Apr 17 14:37:44 2015 -0700
+++ b/jdk/test/sun/tools/jstatd/TestJstatdExternalRegistry.java	Mon Apr 20 08:45:54 2015 +0200
@@ -32,6 +32,7 @@
 
     public static void main(String[] args) throws Throwable {
         JstatdTest test = new JstatdTest();
+        test.setUseDefaultPort(false);
         test.setWithExternalRegistry(true);
         test.doTest();
     }