Updated Branches:
  refs/heads/camel-2.10.x f4d4b8610 -> 9d5883e9b
  refs/heads/camel-2.11.x b13eef887 -> 54918c88e


CAMEL-6341: Shutdown strategy requires a positive timeout value to be set, as 
using 0 is counter intuitive, and can lead to stuck app.

Conflicts:
        
camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/54918c88
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/54918c88
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/54918c88

Branch: refs/heads/camel-2.11.x
Commit: 54918c88ea68672199a725fc2a2c8022a0e89048
Parents: b13eef8
Author: Claus Ibsen <[email protected]>
Authored: Fri May 17 15:47:01 2013 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Fri May 17 15:49:23 2013 +0200

----------------------------------------------------------------------
 .../apache/camel/impl/DefaultShutdownStrategy.java |   28 ++++++++-------
 1 files changed, 15 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/54918c88/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
index 4da2d18..2176ce9 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
@@ -28,6 +28,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -161,21 +162,17 @@ public class DefaultShutdownStrategy extends 
ServiceSupport implements ShutdownS
             Collections.reverse(routesOrdered);
         }
 
-        if (timeout > 0) {
-            LOG.info("Starting to graceful shutdown " + routesOrdered.size() + 
" routes (timeout " + timeout + " " + 
timeUnit.toString().toLowerCase(Locale.ENGLISH) + ")");
-        } else {
-            LOG.info("Starting to graceful shutdown " + routesOrdered.size() + 
" routes (no timeout)");
-        }
+        LOG.info("Starting to graceful shutdown " + routesOrdered.size() + " 
routes (timeout " + timeout + " " + 
timeUnit.toString().toLowerCase(Locale.ENGLISH) + ")");
 
         // use another thread to perform the shutdowns so we can support 
timeout
-        Future<?> future = getExecutorService().submit(new 
ShutdownTask(context, routesOrdered, timeout, timeUnit, suspendOnly, 
abortAfterTimeout));
+        final AtomicBoolean timeoutOccurred = new AtomicBoolean();
+        Future<?> future = getExecutorService().submit(new 
ShutdownTask(context, routesOrdered, timeout, timeUnit, suspendOnly, 
abortAfterTimeout, timeoutOccurred));
         try {
-            if (timeout > 0) {
-                future.get(timeout, timeUnit);
-            } else {
-                future.get();
-            }
+            future.get(timeout, timeUnit);
         } catch (TimeoutException e) {
+            // we hit a timeout, so set the flag
+            timeoutOccurred.set(true);
+
             // timeout then cancel the task
             future.cancel(true);
 
@@ -220,6 +217,9 @@ public class DefaultShutdownStrategy extends ServiceSupport 
implements ShutdownS
     }
 
     public void setTimeout(long timeout) {
+        if (timeout <= 0) {
+            throw new IllegalArgumentException("Timeout must be a positive 
value");
+        }
         this.timeout = timeout;
     }
 
@@ -424,15 +424,17 @@ public class DefaultShutdownStrategy extends 
ServiceSupport implements ShutdownS
         private final boolean abortAfterTimeout;
         private final long timeout;
         private final TimeUnit timeUnit;
+        private final AtomicBoolean timeoutOccurred;
 
         public ShutdownTask(CamelContext context, List<RouteStartupOrder> 
routes, long timeout, TimeUnit timeUnit,
-                            boolean suspendOnly, boolean abortAfterTimeout) {
+                            boolean suspendOnly, boolean abortAfterTimeout, 
AtomicBoolean timeoutOccurred) {
             this.context = context;
             this.routes = routes;
             this.suspendOnly = suspendOnly;
             this.abortAfterTimeout = abortAfterTimeout;
             this.timeout = timeout;
             this.timeUnit = timeUnit;
+            this.timeoutOccurred = timeoutOccurred;
         }
 
         public void run() {
@@ -515,7 +517,7 @@ public class DefaultShutdownStrategy extends ServiceSupport 
implements ShutdownS
             boolean done = false;
             long loopDelaySeconds = 1;
             long loopCount = 0;
-            while (!done) {
+            while (!done && !timeoutOccurred.get()) {
                 int size = 0;
                 for (RouteStartupOrder order : routes) {
                     int inflight = 
context.getInflightRepository().size(order.getRoute().getId());

Reply via email to