Author: ningjiang
Date: Fri Jan 14 02:13:24 2011
New Revision: 1058836

URL: http://svn.apache.org/viewvc?rev=1058836&view=rev
Log:
CAMEL-3240 Graceful shutdown - Add support for give up if timeout triggered

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ShutdownStrategy.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=1058836&r1=1058835&r2=1058836&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Fri 
Jan 14 02:13:24 2011
@@ -427,6 +427,8 @@ public interface CamelContext extends Su
      */
     void stopRoute(String routeId, long timeout, TimeUnit timeUnit) throws 
Exception;
 
+    void stopRoute(String routeId, long timeout, TimeUnit timeUnit, boolean 
giveUp) throws Exception;
+    
     /**
      * Shutdown and <b>removes</b> the given route using {@link 
org.apache.camel.spi.ShutdownStrategy}.
      *

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1058836&r1=1058835&r2=1058836&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 Fri Jan 14 02:13:24 2011
@@ -726,6 +726,22 @@ public class DefaultCamelContext extends
         }
     }
 
+    public synchronized void stopRoute(String routeId, long timeout, TimeUnit 
timeUnit, boolean giveUp) throws Exception {
+        RouteService routeService = routeServices.get(routeId);
+        if (routeService != null) {
+            List<RouteStartupOrder> routes = new 
ArrayList<RouteStartupOrder>(1);
+            RouteStartupOrder order = new DefaultRouteStartupOrder(1, 
routeService.getRoutes().iterator().next(), routeService);
+            routes.add(order);
+
+            boolean completed = getShutdownStrategy().shutdown(this, routes, 
timeout, timeUnit, giveUp);
+            if (completed) {
+                   // must stop route service as well
+                   routeService.setRemovingRoutes(false);
+                   stopRouteService(routeService);
+            }
+        }
+    }
+    
     public synchronized void stopRoute(String routeId) throws Exception {
         RouteService routeService = routeServices.get(routeId);
         if (routeService != null) {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java?rev=1058836&r1=1058835&r2=1058836&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultShutdownStrategy.java
 Fri Jan 14 02:13:24 2011
@@ -85,18 +85,22 @@ public class DefaultShutdownStrategy ext
     }
 
     public void suspend(CamelContext context, List<RouteStartupOrder> routes) 
throws Exception {
-        doShutdown(context, routes, getTimeout(), getTimeUnit(), true);
+        doShutdown(context, routes, getTimeout(), getTimeUnit(), true, false);
     }
 
     public void shutdown(CamelContext context, List<RouteStartupOrder> routes, 
long timeout, TimeUnit timeUnit) throws Exception {
-        doShutdown(context, routes, timeout, timeUnit, false);
+        doShutdown(context, routes, timeout, timeUnit, false, false);
+    }
+
+    public boolean shutdown(CamelContext context, List<RouteStartupOrder> 
routes, long timeout, TimeUnit timeUnit, boolean giveUp) throws Exception {
+        return doShutdown(context, routes, timeout, timeUnit, false, giveUp);
     }
 
     public void suspend(CamelContext context, List<RouteStartupOrder> routes, 
long timeout, TimeUnit timeUnit) throws Exception {
-        doShutdown(context, routes, timeout, timeUnit, true);
+        doShutdown(context, routes, timeout, timeUnit, true, false);
     }
 
-    protected void doShutdown(CamelContext context, List<RouteStartupOrder> 
routes, long timeout, TimeUnit timeUnit, boolean suspendOnly) throws Exception {
+    protected boolean doShutdown(CamelContext context, List<RouteStartupOrder> 
routes, long timeout, TimeUnit timeUnit, boolean suspendOnly, boolean giveUp) 
throws Exception {
         StopWatch watch = new StopWatch();
 
         // at first sort according to route startup order
@@ -128,12 +132,18 @@ public class DefaultShutdownStrategy ext
             // timeout then cancel the task
             future.cancel(true);
 
-            if (shutdownNowOnTimeout) {
-                LOG.warn("Timeout occurred. Now forcing the routes to be 
shutdown now.");
-                // force the routes to shutdown now
-                shutdownRoutesNow(routesOrdered);
+            //if set, stop processing and return false to indicate that the 
shutdown is giving up
+            if( giveUp ) {
+                LOG.warn("Timeout occurred. Giving up now.");
+               return false;
             } else {
-                LOG.warn("Timeout occurred. Will ignore shutting down the 
remainder routes.");
+                   if (shutdownNowOnTimeout) {
+                       LOG.warn("Timeout occurred. Now forcing the routes to 
be shutdown now.");
+                       // force the routes to shutdown now
+                       shutdownRoutesNow(routesOrdered);
+                   } else {
+                       LOG.warn("Timeout occurred. Will ignore shutting down 
the remainder routes.");
+                   }
             }
         } catch (ExecutionException e) {
             // unwrap execution exception
@@ -144,6 +154,7 @@ public class DefaultShutdownStrategy ext
         long seconds = TimeUnit.SECONDS.convert(watch.stop(), 
TimeUnit.MILLISECONDS);
 
         LOG.info("Graceful shutdown of " + routesOrdered.size() + " routes 
completed in " + seconds + " seconds");
+        return true;
     }
 
     public void setTimeout(long timeout) {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ShutdownStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ShutdownStrategy.java?rev=1058836&r1=1058835&r2=1058836&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ShutdownStrategy.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ShutdownStrategy.java 
Fri Jan 14 02:13:24 2011
@@ -70,6 +70,18 @@ public interface ShutdownStrategy extend
     void shutdown(CamelContext context, List<RouteStartupOrder> routes, long 
timeout, TimeUnit timeUnit) throws Exception;
 
     /**
+     * Shutdown the routes using a specified timeout instead of the default 
timeout values and supports "give up" mode
+     *
+     * @param context   the camel context
+     * @param routes    the routes, ordered by the order they was started
+     * @param timeout   timeout
+     * @param timeUnit  the unit to use
+     * @param giveUp    should give up after timeout
+     * @throws Exception is thrown if error shutting down the consumers, 
however its preferred to avoid this
+     */
+    boolean shutdown(CamelContext context, List<RouteStartupOrder> routes, 
long timeout, TimeUnit timeUnit, boolean giveUp) throws Exception;
+
+    /**
      * Suspends the routes using a specified timeout instead of the default 
timeout values
      *
      * @param context   the camel context


Reply via email to