You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/05/29 14:53:33 UTC

svn commit: r1343705 - in /camel/branches/camel-2.9.x: ./ camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java camel-core/src/test/java/org/apache/camel/processor/FailoverLoadBalancerBreakoutDuringShutdownTest.java

Author: davsclaus
Date: Tue May 29 12:53:33 2012
New Revision: 1343705

URL: http://svn.apache.org/viewvc?rev=1343705&view=rev
Log:
CAMEL-5316: Failover EIP now detects shutdown in progress and breaks out from failover loop.

Added:
    camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/processor/FailoverLoadBalancerBreakoutDuringShutdownTest.java
      - copied unchanged from r1343704, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailoverLoadBalancerBreakoutDuringShutdownTest.java
Modified:
    camel/branches/camel-2.9.x/   (props changed)
    camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1343704

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java?rev=1343705&r1=1343704&r2=1343705&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java (original)
+++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java Tue May 29 12:53:33 2012
@@ -17,10 +17,13 @@
 package org.apache.camel.processor.loadbalancer;
 
 import java.util.List;
+import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.AsyncProcessor;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.Traceable;
@@ -36,9 +39,10 @@ import org.apache.camel.util.ObjectHelpe
  * as the failover load balancer is a specialized pipeline. So the trick is to keep doing the same as the
  * pipeline to ensure it works the same and the async routing engine is flawless.
  */
-public class FailOverLoadBalancer extends LoadBalancerSupport implements Traceable {
+public class FailOverLoadBalancer extends LoadBalancerSupport implements Traceable, CamelContextAware {
 
     private final List<Class<?>> exceptions;
+    private CamelContext camelContext;
     private boolean roundRobin;
     private int maximumFailoverAttempts = -1;
 
@@ -60,6 +64,16 @@ public class FailOverLoadBalancer extend
         }
     }
 
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
     public List<Class<?>> getExceptions() {
         return exceptions;
     }
@@ -113,6 +127,16 @@ public class FailOverLoadBalancer extend
         return answer;
     }
 
+    @Override
+    public boolean isRunAllowed() {
+        // determine if we can still run, or the camel context is forcing a shutdown
+        boolean forceShutdown = camelContext.getShutdownStrategy().forceShutdown(this);
+        if (forceShutdown) {
+            log.trace("Run not allowed as ShutdownStrategy is forcing shutting down");
+        }
+        return !forceShutdown && super.isRunAllowed();
+    }
+
     public boolean process(final Exchange exchange, final AsyncCallback callback) {
         final List<Processor> processors = getProcessors();
 
@@ -133,6 +157,18 @@ public class FailOverLoadBalancer extend
         log.trace("Failover starting with endpoint index {}", index);
 
         while (first || shouldFailOver(copy)) {
+
+            // can we still run
+            if (!isRunAllowed()) {
+                log.trace("Run not allowed, will reject executing exchange: {}", exchange);
+                if (exchange.getException() == null) {
+                    exchange.setException(new RejectedExecutionException());
+                }
+                // we cannot process so invoke callback
+                callback.done(true);
+                return true;
+            }
+
             if (!first) {
                 attempts.incrementAndGet();
                 // are we exhausted by attempts?
@@ -240,6 +276,17 @@ public class FailOverLoadBalancer extend
             }
 
             while (shouldFailOver(copy)) {
+
+                // can we still run
+                if (!isRunAllowed()) {
+                    log.trace("Run not allowed, will reject executing exchange: {}", exchange);
+                    if (exchange.getException() == null) {
+                        exchange.setException(new RejectedExecutionException());
+                    }
+                    // we cannot process so invoke callback
+                    callback.done(false);
+                }
+
                 attempts.incrementAndGet();
                 // are we exhausted by attempts?
                 if (maximumFailoverAttempts > -1 && attempts.get() > maximumFailoverAttempts) {