camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [08/15] camel git commit: [CAMEL-11229] Avoid recursion in onException
Date Thu, 11 May 2017 20:30:14 GMT
[CAMEL-11229] Avoid recursion in onException


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5491f483
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5491f483
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5491f483

Branch: refs/heads/camel-2.18.x
Commit: 5491f4830a593990149acffd0c42169b6f6e0304
Parents: 04d0b63
Author: Christian Schneider <chris@die-schneider.net>
Authored: Fri May 5 16:32:27 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu May 11 18:41:36 2017 +0200

----------------------------------------------------------------------
 .../camel/processor/RedeliveryErrorHandler.java | 11 ++-
 .../onexception/OnExceptionRecursionTest.java   | 85 ++++++++++++++++++++
 2 files changed, 94 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5491f483/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java
b/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java
index 5834b0a..32a1bbe 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java
@@ -852,8 +852,14 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
impleme
     protected void handleException(Exchange exchange, RedeliveryData data, boolean isDeadLetterChannel)
{
         Exception e = exchange.getException();
 
-        // store the original caused exception in a property, so we can restore it later
-        exchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
+        Throwable origExceptionCaught = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
+        if (origExceptionCaught != null) {
+            log.error("Second Exception occured inside exception handler. Failing exchange",
e);
+            exchange.setProperty(Exchange.UNIT_OF_WORK_EXHAUSTED, true);
+        } else {
+
+            // store the original caused exception in a property, so we can restore it later
+            exchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
 
         // find the error handler to use (if any)
         OnExceptionDefinition exceptionPolicy = getExceptionPolicy(exchange, e);
@@ -891,6 +897,7 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
impleme
                 data.onExceptionProcessor = processor;
             }
         }
+        }
 
         // only log if not failure handled or not an exhausted unit of work
         if (!ExchangeHelper.isFailureHandled(exchange) && !ExchangeHelper.isUnitOfWorkExhausted(exchange))
{

http://git-wip-us.apache.org/repos/asf/camel/blob/5491f483/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRecursionTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRecursionTest.java
b/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRecursionTest.java
new file mode 100644
index 0000000..39c5c1b
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRecursionTest.java
@@ -0,0 +1,85 @@
+/**
+ * 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.onexception;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Assert;
+
+
+
+/**
+ * Test that exceptions in an onException handler route do not go into recursion
+ */
+public class OnExceptionRecursionTest extends ContextTestSupport {
+    private int counter;
+
+    public void testRecursion() throws Exception {
+        try {
+            template.sendBody("direct:start", "Hello World");
+        } catch (CamelExecutionException e) {
+            Throwable inner = e.getCause();
+            Assert.assertEquals("Simulate exception in route", inner.getMessage());
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(MyException.class).process(new Processor() {
+                    
+                    @Override
+                    public void process(Exchange exchange) throws Exception {
+                        System.out.println();
+                    }
+                }).to("direct:exhandler");
+                from("direct:exhandler").process(recursionProcessor())
+                    .throwException(new MyException("Simulate Exception in handler route"));
+                from("direct:start").throwException(new MyException("Simulate exception in
route"));
+            }
+
+        };
+
+    }
+
+    private Processor recursionProcessor() {
+        return new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                counter++;
+                if (counter > 1) {
+                    throw new IllegalStateException("Test failed as camel would go into recursion");
+                }
+
+            }
+        };
+    }
+
+    class MyException extends RuntimeException {
+        private static final long serialVersionUID = 1L;
+
+        MyException(String msg) {
+            super(msg);
+        }
+    };
+
+}


Mime
View raw message