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 2009/06/24 13:31:38 UTC

svn commit: r787982 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java

Author: davsclaus
Date: Wed Jun 24 11:31:38 2009
New Revision: 787982

URL: http://svn.apache.org/viewvc?rev=787982&view=rev
Log:
CAMEL-1755: failover loadbalancer should prepare exchange when doing failover.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java?rev=787982&r1=787981&r2=787982&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.java Wed Jun 24 11:31:38 2009
@@ -83,8 +83,8 @@
         while (shouldFailOver(exchange)) {
             index++;
             if (index < list.size()) {
-                // try again but reset exception first
-                exchange.setException(null);
+                // try again but prepare exchange before we failover
+                prepareExchangeForFailover(exchange);
                 processor = list.get(index);
                 processExchange(processor, exchange);
             } else {
@@ -94,6 +94,21 @@
         }
     }
 
+    /**
+     * Prepares the exchange for failover
+     *
+     * @param exchange the exchange
+     */
+    protected void prepareExchangeForFailover(Exchange exchange) {
+        exchange.setException(null);
+
+        exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, null);
+        exchange.setProperty(Exchange.FAILURE_HANDLED, null);
+        exchange.setProperty(Exchange.EXCEPTION_CAUGHT, null);
+        exchange.getIn().removeHeader(Exchange.REDELIVERED);
+        exchange.getIn().removeHeader(Exchange.REDELIVERY_COUNTER);
+    }
+
     private void processExchange(Processor processor, Exchange exchange) {
         if (processor == null) {
             throw new IllegalStateException("No processors could be chosen to process " + exchange);

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java?rev=787982&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java Wed Jun 24 11:31:38 2009
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class FailOverLoadBalanceWithRedeliveryTest extends ContextTestSupport {
+
+    private static int counter;
+
+    public void testFailoverWithRedelivery() throws Exception {
+        counter = 0;
+
+        MockEndpoint a = getMockEndpoint("mock:a");
+        a.expectedMessageCount(3);
+
+        MockEndpoint b = getMockEndpoint("mock:b");
+        b.expectedMessageCount(2);
+
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.expectedBodiesReceived("Bye World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+
+                errorHandler(defaultErrorHandler().maximumRedeliveries(2).redeliverDelay(0));
+
+                from("direct:start")
+                    .loadBalance().failover().to("direct:a", "direct:b");
+
+                from("direct:a")
+                    // disable redelivery here as most often your load balancer over external
+                    // endpoints you do not have control off, such as a web service call
+                    // but we use mock for unit testing so no error handler here please
+                    .errorHandler(noErrorHandler())
+                    .to("mock:a")
+                    .throwException(new IllegalArgumentException("I cannot do this"));
+
+                from("direct:b")
+                    // disable redelivery here as most often your load balancer over external
+                    // endpoints you do not have control off, such as a web service call
+                    // but we use mock for unit testing so no error handler here please
+                    .errorHandler(noErrorHandler())
+                    .to("mock:b")
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            // fail on the first try but succeed on the 2nd try
+                            if (counter++ < 1) {
+                                throw new IllegalArgumentException("I can still not do this");
+                            }
+                            exchange.getIn().setBody("Bye World");
+                        }
+                    }).to("mock:result");
+            }
+        };
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWithRedeliveryTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date