You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by an...@apache.org on 2014/09/26 13:12:57 UTC

svn commit: r1627752 - in /tomee/tomee/trunk: container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java

Author: andygumbrecht
Date: Fri Sep 26 11:12:56 2014
New Revision: 1627752

URL: http://svn.apache.org/r1627752
Log:
TOMEE-1364 - Allow configuration of shutdown attempts, and use sensible defaults.
Thanks Christofer Dutz for pointing this out.

Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java?rev=1627752&r1=1627751&r2=1627752&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java Fri Sep 26 11:12:56 2014
@@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
- * NOTE: don't add inner classes or anonymous one or dependency without updating ExecMojo
+ * NOTE: Do not add inner or anonymous classes or a dependency without updating ExecMojo
  *
  * @version $Rev$ $Date$
  */
@@ -81,7 +81,7 @@ public class RemoteServer {
     }
 
     public RemoteServer(final int tries, final boolean verbose) {
-        this.tries = tries;
+        this.tries = (tries < 1 ? 1 : (tries > 3600 ? 3600 : tries)); //Wait at least 1 second to start or stop, but not more than an hour.
         this.verbose = verbose;
         home = getHome();
         tomcat = (home != null) && (new File(new File(home, "bin"), "catalina.sh").exists());
@@ -109,7 +109,11 @@ public class RemoteServer {
         } else if (args[0].equalsIgnoreCase(STOP)) {
             final RemoteServer remoteServer = new RemoteServer();
             remoteServer.serverHasAlreadyBeenStarted = false;
-            remoteServer.stop();
+            try {
+                remoteServer.forceStop();
+            } catch (final Exception e) {
+                e.printStackTrace(System.err);
+            }
         } else {
             throw new OpenEJBRuntimeException("valid arguments are 'start' or 'stop'");
         }
@@ -475,44 +479,60 @@ public class RemoteServer {
     }
 
     public boolean stop() {
-        try {
-            shutdown(5);
+        if (sendShutdown(5)) {
             return true;
-        } catch (final Exception e) {
-            if (verbose && !serverHasAlreadyBeenStarted) {
-                e.printStackTrace(System.err);
+        } else {
+            if (verbose) {
+                notSent();
             }
-        }
 
         return false;
     }
+    }
+
+    private void notSent() {
+        System.out.println("Failed to send the shutdown notification - TomEE is likely shut down already");
+    }
 
     public void forceStop() throws Exception {
-        shutdown(5);
+        if (sendShutdown(5)) {
 
-        // check tomcat was effectively shutted down
-        // we can have some concurrent shutdown commands (catalina shutdown hook for instance)
-        // so we can have to wait here since it is important to be synchronous in this method
+            // Check TomEE was effectively shut down after getting the message
+            // There can be concurrent shutdown operations (catalina shutdown hook for instance),
+            // so we have to wait here since it is important to be synchronous in this method
         waitForServerShutdown();
+        } else {
+            if (verbose) {
+                notSent();
+    }
+        }
     }
 
-    private void waitForServerShutdown() throws InterruptedException {
+    private void waitForServerShutdown() throws Exception {
+
         if (verbose) {
             System.out.print("Waiting for TomEE shutdown.");
         }
-        while (connect(portShutdown, tries)) {
-            Thread.sleep(1000);
+
+        final boolean b = connect(portShutdown, tries);
+
             if (verbose) {
-                System.out.print(".");
+            System.out.println();
             }
+
+        if (b) {
+            //We need to know about this
+            System.out.println("SEVERE: Failed to shutdown TomEE running on port " + portStartup + " using shutdown port: " + portShutdown);
         }
-        if (verbose) {
-            System.out.println();
         }
-    }
 
-    // same as catalina.sh stop {@see org.apache.catalina.startup.Catalina#stopServer}
-    private void shutdown(int attempts) throws Exception {
+    /**
+     * Send the shutdown message to the running server
+     *
+     * @param attempts How many times to try to send the message before giving up
+     * @return True is the message was sent, else false if unable to connect after the defined number of attempts
+     */
+    private boolean sendShutdown(int attempts) {
         Socket socket = null;
         OutputStream stream = null;
         try {
@@ -525,10 +545,14 @@ public class RemoteServer {
             stream.flush();
         } catch (final Exception e) {
             if (attempts > 0) {
+                try {
                 Thread.sleep(1000);
-                shutdown(--attempts);
+                } catch (final InterruptedException ie) {
+                    return false;
+                }
+                return sendShutdown(--attempts);
             } else {
-                throw e;
+                return false;
             }
         } finally {
             IO.close(stream);
@@ -540,6 +564,8 @@ public class RemoteServer {
                 }
             }
         }
+
+        return true;
     }
 
     private boolean connect(final int port, int tries) {

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java?rev=1627752&r1=1627751&r2=1627752&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java Fri Sep 26 11:12:56 2014
@@ -127,6 +127,9 @@ public abstract class AbstractTomEEMojo 
     @Parameter(property = "tomee-plugin.shutdown", defaultValue = "8005")
     protected int tomeeShutdownPort;
 
+    @Parameter(property = "tomee-plugin.shutdown.attempts", defaultValue = "60")
+    protected int tomeeShutdownAttempts;
+
     @Parameter(property = "tomee-plugin.shutdown-command", defaultValue = "SHUTDOWN")
     protected String tomeeShutdownCommand;
 
@@ -761,7 +764,7 @@ public abstract class AbstractTomEEMojo 
         System.setProperty("server.shutdown.port", Integer.toString(tomeeShutdownPort));
         System.setProperty("server.shutdown.command", tomeeShutdownCommand);
 
-        server = new RemoteServer(getConnectAttempts(), false);
+        server = new RemoteServer(getConnectAttempts(), debug);
         server.setAdditionalClasspath(getAdditionalClasspath());
 
         addShutdownHooks(server); // some shutdown hooks are always added (see UpdatableTomEEMojo)
@@ -1029,7 +1032,7 @@ public abstract class AbstractTomEEMojo 
     }
 
     protected int getConnectAttempts() {
-        return Integer.MAX_VALUE;
+        return (tomeeShutdownAttempts == 0 ? 60 : tomeeShutdownAttempts);
     }
 
     protected boolean getWaitTomEE() {