Author: davsclaus
Date: Mon Sep 12 06:01:30 2011
New Revision: 1169610

URL: http://svn.apache.org/viewvc?rev=1169610&view=rev
Log:
CAMEL-4438: Fixed issue with onException using redeliveryPolicyRef, not being 
detected in the RedeliveryErrorHandler in all situations.

Added:
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest.java
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-1.xml
      - copied, changed from r1167474, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/ProduceSplitMethodCallIssueTest.xml
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-2.xml
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java?rev=1169610&r1=1169609&r2=1169610&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
 Mon Sep 12 06:01:30 2011
@@ -134,24 +134,25 @@ public class OnExceptionDefinition exten
      * Allows an exception handler to create a new redelivery policy for this 
exception type
      *
      * @param context      the camel context
-     * @param parentPolicy the current redelivery policy
+     * @param parentPolicy the current redelivery policy, is newer 
<tt>null</tt>
      * @return a newly created redelivery policy, or return the original 
policy if no customization is required
      *         for this exception handler.
      */
     public RedeliveryPolicy createRedeliveryPolicy(CamelContext context, 
RedeliveryPolicy parentPolicy) {
         if (redeliveryPolicyRef != null) {
-            parentPolicy = CamelContextHelper.mandatoryLookup(context, 
redeliveryPolicyRef, RedeliveryPolicy.class);
-        }
-
-        if (redeliveryPolicy != null) {
+            return CamelContextHelper.mandatoryLookup(context, 
redeliveryPolicyRef, RedeliveryPolicy.class);
+        } else if (redeliveryPolicy != null) {
             return redeliveryPolicy.createRedeliveryPolicy(context, 
parentPolicy);
-        } else if (errorHandler != null) {
-            // lets create a new error handler that has no retries
+        } else if (!outputs.isEmpty() && parentPolicy.getMaximumRedeliveries() 
> 0) {
+            // if we have outputs, then do not inherit parent 
maximumRedeliveries
+            // as you would have to explicit configure maximumRedeliveries on 
this onException to use it
+            // this is the behavior Camel has always had
             RedeliveryPolicy answer = parentPolicy.copy();
             answer.setMaximumRedeliveries(0);
             return answer;
+        } else {
+            return parentPolicy;
         }
-        return parentPolicy;
     }
 
     public void addRoutes(RouteContext routeContext, Collection<Route> routes) 
throws Exception {

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=1169610&r1=1169609&r2=1169610&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
 Mon Sep 12 06:01:30 2011
@@ -549,6 +549,9 @@ public abstract class RedeliveryErrorHan
     }
 
     protected void prepareExchangeForRedelivery(Exchange exchange, 
RedeliveryData data) {
+        if (!redeliveryEnabled) {
+            throw new IllegalStateException("Redelivery is not enabled on " + 
this + ". Make sure you have configured the error handler properly.");
+        }
         // there must be a defensive copy of the exchange
         ObjectHelper.notNull(data.original, "Defensive copy of Exchange is 
null", this);
 
@@ -951,21 +954,19 @@ public abstract class RedeliveryErrorHan
             // walk them to see if any of them have a maximum redeliveries > 0 
or retry until set
             for (OnExceptionDefinition def : exceptionPolicies.values()) {
 
-                if (def.getRedeliveryPolicy() != null) {
-                    String ref = def.getRedeliveryPolicyRef();
-                    if (ref != null) {
-                        // lookup in registry if ref provided
-                        RedeliveryPolicy policy = 
CamelContextHelper.mandatoryLookup(camelContext, ref, RedeliveryPolicy.class);
-                        if (policy.getMaximumRedeliveries() != 0) {
-                            // must check for != 0 as (-1 means redeliver 
forever)
-                            return true;
-                        }
-                    } else {
-                        Integer max = 
CamelContextHelper.parseInteger(camelContext, 
def.getRedeliveryPolicy().getMaximumRedeliveries());
-                        if (max != null && max != 0) {
-                            // must check for != 0 as (-1 means redeliver 
forever)
-                            return true;
-                        }
+                String ref = def.getRedeliveryPolicyRef();
+                if (ref != null) {
+                    // lookup in registry if ref provided
+                    RedeliveryPolicy policy = 
CamelContextHelper.mandatoryLookup(camelContext, ref, RedeliveryPolicy.class);
+                    if (policy.getMaximumRedeliveries() != 0) {
+                        // must check for != 0 as (-1 means redeliver forever)
+                        return true;
+                    }
+                } else if (def.getRedeliveryPolicy() != null) {
+                    Integer max = 
CamelContextHelper.parseInteger(camelContext, 
def.getRedeliveryPolicy().getMaximumRedeliveries());
+                    if (max != null && max != 0) {
+                        // must check for != 0 as (-1 means redeliver forever)
+                        return true;
                     }
                 }
 

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest.java?rev=1169610&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest.java
 Mon Sep 12 06:01:30 2011
@@ -0,0 +1,63 @@
+/**
+ * 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.spring.issues;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ *
+ */
+public class RedeliveryErrorHandlerTwoXmlFilesIssueTest extends 
SpringTestSupport {
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new 
ClassPathXmlApplicationContext("org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-1.xml",
+                
"org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-2.xml");
+    }
+
+    public void testRedeliveryErrorHandlerTwoXmlFilesIssue() throws Exception {
+        getMockEndpoint("mock:handled").expectedMessageCount(1);
+        getMockEndpoint("mock:dead").expectedMessageCount(0);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        OnRedeliveryCounter counter = context.getRegistry().lookup("counter", 
OnRedeliveryCounter.class);
+        assertEquals(3, counter.getCounter());
+    }
+
+    public static class OnRedeliveryCounter implements Processor {
+
+        private int counter;
+
+        @Override
+        public void process(Exchange exchange) throws Exception {
+            counter++;
+        }
+
+        public int getCounter() {
+            return counter;
+        }
+    }
+
+}

Copied: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-1.xml
 (from r1167474, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/ProduceSplitMethodCallIssueTest.xml)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-1.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-1.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/ProduceSplitMethodCallIssueTest.xml&r1=1167474&r2=1169610&rev=1169610&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/ProduceSplitMethodCallIssueTest.xml
 (original)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-1.xml
 Mon Sep 12 06:01:30 2011
@@ -22,18 +22,27 @@
        http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <bean id="cool" class="org.apache.camel.spring.issues.CoolService"/>
+  <bean id="damn" class="java.lang.IllegalArgumentException">
+    <constructor-arg index="0" value="Damn"/>
+  </bean>
 
-    <camelContext trace="true" xmlns="http://camel.apache.org/schema/spring";>
-        <route>
-            <from uri="direct:start"/>
-            <to uri="log:foo"/>
-            <split>
-                <method bean="cool" method="split"/>
-                <transform><simple>Hello ${body}</simple></transform>
-                <to uri="mock:split"/>
-            </split>
-        </route>
-    </camelContext>
+  <bean id="counter" 
class="org.apache.camel.spring.issues.RedeliveryErrorHandlerTwoXmlFilesIssueTest.OnRedeliveryCounter"/>
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring";>
+
+    <route errorHandlerRef="myErrorHandler">
+      <from uri="direct:start"/>
+      <onException redeliveryPolicyRef="myRedeliveryPolicy" 
onRedeliveryRef="counter">
+        <exception>java.lang.IllegalArgumentException</exception>
+        <handled>
+          <constant>true</constant>
+        </handled>
+        <to uri="mock:handled"/>
+      </onException>
+      <throwException ref="damn"/>
+      <to uri="mock:result"/>
+    </route>
+
+  </camelContext>
 
 </beans>

Added: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-2.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-2.xml?rev=1169610&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-2.xml
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RedeliveryErrorHandlerTwoXmlFilesIssueTest-2.xml
 Mon Sep 12 06:01:30 2011
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <!-- use no redeliveryDelay to speedup unit testing -->
+  <redeliveryPolicyProfile xmlns="http://camel.apache.org/schema/spring";
+    id="myRedeliveryPolicy" maximumRedeliveries="3" redeliveryDelay="0" 
retryAttemptedLogLevel="WARN"/>
+
+  <errorHandler xmlns="http://camel.apache.org/schema/spring";
+    id="myErrorHandler" type="DeadLetterChannel" deadLetterUri="mock:dead"/>
+
+</beans>


Reply via email to