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 2011/06/15 14:56:30 UTC

svn commit: r1136018 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/processor/ camel-core/src/test/java/org/apache/camel/processor/ components/camel-spring/src/test/java/org/apache/camel/spring/processor/ components/camel-spring/src/test/...

Author: davsclaus
Date: Wed Jun 15 12:56:29 2011
New Revision: 1136018

URL: http://svn.apache.org/viewvc?rev=1136018&view=rev
Log:
CAMEL-4106: Made logic about determine to redeliver or exhaust a bit more obvious. Only invoke shouldRedeliver on redelivery policy if its a potential redelviery attempt.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.java
      - copied, changed from r1135974, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.xml
      - copied, changed from r1135974, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java?rev=1136018&r1=1136017&r2=1136018&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java Wed Jun 15 12:56:29 2011
@@ -228,9 +228,9 @@ public abstract class RedeliveryErrorHan
                 handleException(exchange, data);
             }
 
-            // compute if we should redeliver or not
-            boolean shouldRedeliver = shouldRedeliver(exchange, data);
-            if (!shouldRedeliver) {
+            // compute if we are exhausted or not
+            boolean exhausted = isExhausted(exchange, data);
+            if (exhausted) {
                 Processor target = null;
                 boolean deliver = true;
 
@@ -368,9 +368,9 @@ public abstract class RedeliveryErrorHan
             handleException(exchange, data);
         }
 
-        // compute if we should redeliver or not
-        boolean shouldRedeliver = shouldRedeliver(exchange, data);
-        if (!shouldRedeliver) {
+        // compute if we are exhausted or not
+        boolean exhausted = isExhausted(exchange, data);
+        if (exhausted) {
             Processor target = null;
             boolean deliver = true;
 
@@ -781,20 +781,29 @@ public abstract class RedeliveryErrorHan
     }
 
     /**
-     * Determines whether or not to we should try to redeliver
+     * Determines whether the exchange is exhausted (or anyway marked to not continue such as rollback).
+     * <p/>
+     * If the exchange is exhausted, then we will not continue processing, but let the
+     * failure processor deal with the exchange.
      *
      * @param exchange the current exchange
      * @param data     the redelivery data
-     * @return <tt>true</tt> to redeliver, or <tt>false</tt> to exhaust.
+     * @return <tt>false</tt> to continue/redeliver, or <tt>true</tt> to exhaust.
      */
-    private boolean shouldRedeliver(Exchange exchange, RedeliveryData data) {
-        // if marked as rollback only then do not redeliver
+    private boolean isExhausted(Exchange exchange, RedeliveryData data) {
+        // if marked as rollback only then do not continue/redeliver
         boolean rollbackOnly = exchange.getProperty(Exchange.ROLLBACK_ONLY, false, Boolean.class);
         if (rollbackOnly) {
-            log.trace("This exchange is marked as rollback only, should not be redelivered: {}", exchange);
+            log.trace("This exchange is marked as rollback only, so forcing it to be exhausted: {}", exchange);
+            return true;
+        }
+        // its the first original call so continue
+        if (data.redeliveryCounter == 0) {
             return false;
         }
-        return data.currentRedeliveryPolicy.shouldRedeliver(exchange, data.redeliveryCounter, data.retryWhilePredicate);
+        // its a potential redelivery so determine if we should redeliver or not
+        boolean redeliver = data.currentRedeliveryPolicy.shouldRedeliver(exchange, data.redeliveryCounter, data.retryWhilePredicate);
+        return !redeliver;
     }
 
     /**

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java?rev=1136018&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java Wed Jun 15 12:56:29 2011
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+/**
+ *
+ */
+public class DeadLetterChannelNoRedeliveryTest extends ContextTestSupport {
+
+    private static volatile int counter;
+
+    public void testDLCNoRedelivery() throws Exception {
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(0);
+        getMockEndpoint("mock:dead").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals("Only the original attempt", 1, counter);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                errorHandler(deadLetterChannel("mock:dead")
+                        .useOriginalMessage()
+                        .maximumRedeliveries(0));
+
+                from("direct:start")
+                    .to("mock:a")
+                    .process(new MyFailProcessor())
+                    .to("mock:b");
+            }
+        };
+    }
+
+    public static final class MyFailProcessor implements Processor {
+
+        @Override
+        public void process(Exchange exchange) throws Exception {
+            counter++;
+            throw new IllegalArgumentException("Forced");
+        }
+    }
+}

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.java (from r1135974, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java&r1=1135974&r2=1136018&rev=1136018&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.java Wed Jun 15 12:56:29 2011
@@ -17,15 +17,16 @@
 package org.apache.camel.spring.processor;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.processor.AOPAfterTest;
+import org.apache.camel.processor.DeadLetterChannelNoRedeliveryTest;
+
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
 /**
  * @version 
  */
-public class SpringAOPAfterTest extends AOPAfterTest {
+public class SpringDeadLetterChannelNoRedeliveryTest extends DeadLetterChannelNoRedeliveryTest {
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/aopafter.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.xml");
     }
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.xml (from r1135974, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml&r1=1135974&r2=1136018&rev=1136018&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDeadLetterChannelNoRedeliveryTest.xml Wed Jun 15 12:56:29 2011
@@ -22,16 +22,22 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <!-- START SNIPPET: e1 -->
-    <camelContext xmlns="http://camel.apache.org/schema/spring">
-        <route>
-            <from uri="direct:start"/>
-            <aop beforeUri="mock:before">
-                <transform><constant>Bye World</constant></transform>
-                <to uri="mock:result"/>
-            </aop>
-        </route>
-    </camelContext>
-    <!-- END SNIPPET: e1 -->
+  <bean id="MyFailProcessor" class="org.apache.camel.processor.DeadLetterChannelNoRedeliveryTest$MyFailProcessor"/>
+
+  <!-- enable error handler on camel context -->
+  <camelContext errorHandlerRef="dlc" xmlns="http://camel.apache.org/schema/spring">
+
+    <errorHandler id="dlc" type="DeadLetterChannel" deadLetterUri="mock:dead" useOriginalMessage="true">
+      <!-- no redeliveries -->
+      <redeliveryPolicy maximumRedeliveries="0"/>
+    </errorHandler>
+
+    <route>
+      <from uri="direct:start"/>
+      <to uri="mock:a"/>
+      <process ref="MyFailProcessor"/>
+      <to uri="mock:result"/>
+    </route>
+  </camelContext>
 
 </beans>