test/java/rmi/reliability/juicer/ApplicationServer.java

Print this page

        

@@ -19,10 +19,11 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
+import java.rmi.NotBoundException;
 import java.rmi.RemoteException;
 import java.rmi.registry.Registry;
 import java.rmi.registry.LocateRegistry;
 import java.util.logging.Logger;
 import java.util.logging.Level;

@@ -58,31 +59,33 @@
 
     /*
      * On initialization, export remote objects and register
      * them with server.
      */
+    @Override
     public void run() {
         try {
             int i = 0;
 
             /*
-             * Locate apple user object in registry.  The lookup will
-             * occur until it is successful or fails LOOKUP_ATTEMPTS times.
+             * Locate apple user object in registry.  The lookup will occur
+             * every 5 seconds until it is successful or timeout 50 seconds.
              * These repeated attempts allow the ApplicationServer
              * to be started before the AppleUserImpl.
              */
             Exception exc = null;
-            for (i = 0; i < LOOKUP_ATTEMPTS; i++) {
+            long stopTime = System.currentTimeMillis() + LOOKUP_ATTEMPTS * 10000;
+            while (System.currentTimeMillis() < stopTime) {
                 try {
                     Registry registry = LocateRegistry.getRegistry(
                            registryHost, registryPort);
                     user = (AppleUser) registry.lookup("AppleUser");
                     user.startTest();
                     break; //successfully obtained AppleUser
-                } catch (Exception e) {
+                } catch (RemoteException | NotBoundException e) {
                     exc = e;
-                    Thread.sleep(10000); //sleep 10 seconds and try again
+                    Thread.sleep(5000); //sleep 5 seconds and try again
                 }
             }
             if (user == null) {
                 logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc);
                 return;

@@ -111,13 +114,12 @@
                 }
             } catch (RemoteException e) {
                 logger.log(Level.SEVERE,
                     "Failed to register callbacks for " + apples[i] + ":", e);
                 user.reportException(e);
-                return;
             }
-        } catch (Exception e) {
+        } catch (InterruptedException | RemoteException e) {
             logger.log(Level.SEVERE, "Unexpected exception:", e);
         }
     }
 
     private static void usage() {

@@ -141,21 +143,26 @@
 
         // parse command line args
         try {
             for (int i = 0; i < args.length ; i++ ) {
                 String arg = args[i];
-                if (arg.equals("-numApples")) {
+                switch (arg) {
+                    case "-numApples":
                     i++;
                     num = Integer.parseInt(args[i]);
-                } else if (arg.equals("-registryHost")) {
+                        break;
+                    case "-registryHost":
                     i++;
                     host = args[i];
-                } else if (arg.equals("-registryPort")) {
+                        break;
+                    case "-registryPort":
                     i++;
                     port = Integer.parseInt(args[i]);
-                } else {
+                        break;
+                    default:
                     usage();
+                        break;
                 }
             }
 
             if (port == -1) {
                 usage();