You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/06/26 08:01:01 UTC

[5/5] git commit: CAMEL-7050 Added checkException DSL in DeadLetterChannelBuilder to check the exception

CAMEL-7050 Added checkException DSL in DeadLetterChannelBuilder to check the exception


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

Branch: refs/heads/master
Commit: cd59dbb88a09c5aa0fea055a0fefd89376029171
Parents: ab46884
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Jun 26 12:02:52 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Jun 26 14:00:37 2014 +0800

----------------------------------------------------------------------
 .../camel/builder/DeadLetterChannelBuilder.java | 10 ++-
 .../apache/camel/processor/SendProcessor.java   | 21 +++++-
 ...msDeadLetterChannelHandlerExceptionTest.java | 77 ++++++++++++++++++++
 3 files changed, 106 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/cd59dbb8/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
index b67914a..4ea7f1d 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
@@ -37,6 +37,7 @@ import org.slf4j.LoggerFactory;
  * @version 
  */
 public class DeadLetterChannelBuilder extends DefaultErrorHandlerBuilder {
+    boolean checkException;
 
     public DeadLetterChannelBuilder() {
         // no-arg constructor used by Spring DSL
@@ -49,6 +50,11 @@ public class DeadLetterChannelBuilder extends DefaultErrorHandlerBuilder {
     public DeadLetterChannelBuilder(String uri) {
         setDeadLetterUri(uri);
     }
+    
+    public DeadLetterChannelBuilder checkException() {
+        checkException = true;
+        return this;
+    }
 
     public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
         validateDeadLetterUri(routeContext);
@@ -78,7 +84,9 @@ public class DeadLetterChannelBuilder extends DefaultErrorHandlerBuilder {
     public Processor getFailureProcessor() {
         if (failureProcessor == null) {
             // force MEP to be InOnly so when sending to DLQ we would not expect a reply if the MEP was InOut
-            failureProcessor = new SendProcessor(deadLetter, ExchangePattern.InOnly);
+            // If the checkException is true, sendProcessor will checkException 
+            // and mark the exchange ERRORHANDLER_HANDLED property to false
+            failureProcessor = new SendProcessor(deadLetter, ExchangePattern.InOnly, checkException);
         }
         return failureProcessor;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/cd59dbb8/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
index 6ba6fc8..d8e8803 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
@@ -53,16 +53,22 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
     protected ProducerCache producerCache;
     protected AsyncProcessor producer;
     protected Endpoint destination;
+    protected final boolean unhandleException;
 
     public SendProcessor(Endpoint destination) {
         this(destination, null);
     }
 
     public SendProcessor(Endpoint destination, ExchangePattern pattern) {
+        this(destination, pattern, false);
+    }
+    
+    public SendProcessor(Endpoint destination, ExchangePattern pattern, boolean unhandleException) {
         ObjectHelper.notNull(destination, "destination");
         this.destination = destination;
         this.camelContext = destination.getCamelContext();
         this.pattern = pattern;
+        this.unhandleException = unhandleException;
         ObjectHelper.notNull(this.camelContext, "camelContext");
     }
 
@@ -81,7 +87,7 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
     public String getTraceLabel() {
         return URISupport.sanitizeUri(destination.getEndpointUri());
     }
-
+    
     public void process(final Exchange exchange) throws Exception {
         AsyncProcessorHelper.process(this, exchange);
     }
@@ -93,6 +99,7 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
             return true;
         }
 
+
         // we should preserve existing MEP so remember old MEP
         // if you want to permanently to change the MEP then use .setExchangePattern in the DSL
         final ExchangePattern existingPattern = exchange.getPattern();
@@ -120,6 +127,7 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
                             long timeTaken = watch.stop();
                             EventHelper.notifyExchangeSent(target.getContext(), target, destination, timeTaken);
                         } finally {
+                            checkException(target);
                             callback.done(doneSync);
                         }
                     }
@@ -127,6 +135,7 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
             } catch (Throwable throwable) {
                 if (exchange != null) {
                     exchange.setException(throwable);
+                    checkException(exchange);
                 }
 
             }
@@ -144,6 +153,7 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
                     public void done(boolean doneSync) {
                         // restore previous MEP
                         target.setPattern(existingPattern);
+                        checkException(target);
                         // signal we are done
                         callback.done(doneSync);
                     }
@@ -151,6 +161,15 @@ public class SendProcessor extends ServiceSupport implements AsyncProcessor, Tra
             }
         });
     }
+    
+    protected void checkException(Exchange exchange) {
+        if (unhandleException && exchange.getException() != null) {
+            // Override the default setting of DeadLetterChannel
+            exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, "false");
+            // just override the exception with the new added
+            exchange.setProperty(Exchange.EXCEPTION_CAUGHT, exchange.getException());
+        }
+    }
 
     public Endpoint getDestination() {
         return destination;

http://git-wip-us.apache.org/repos/asf/camel/blob/cd59dbb8/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsDeadLetterChannelHandlerExceptionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsDeadLetterChannelHandlerExceptionTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsDeadLetterChannelHandlerExceptionTest.java
new file mode 100644
index 0000000..8604e87
--- /dev/null
+++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsDeadLetterChannelHandlerExceptionTest.java
@@ -0,0 +1,77 @@
+/**
+ * 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.component.jms.issues;
+
+
+import java.util.UUID;
+
+import javax.jms.ConnectionFactory;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Handler;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jms.CamelJmsTestHelper;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.apache.camel.component.jms.JmsComponent.jmsComponentTransacted;
+
+public class JmsDeadLetterChannelHandlerExceptionTest extends CamelTestSupport {
+    
+    public static class BadErrorHandler {
+        @Handler
+        public void onException(Exchange exchange, Exception exception) throws Exception {
+            
+            throw new RuntimeException("error in errorhandler");
+        }
+    }
+   
+    private final String testingEndpoint = "activemq:test." + getClass().getName();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
+                errorHandler(deadLetterChannel("bean:" + BadErrorHandler.class.getName()).checkException());
+
+                from(testingEndpoint).throwException(new RuntimeException("bad error"));
+                
+                
+            }
+        };
+    }
+
+    @Test
+    public void shouldNotLoseMessagesOnExceptionInErrorHandler() throws Exception {
+        UUID message = UUID.randomUUID();
+        template.sendBody(testingEndpoint, message);
+
+        Object dlqBody = consumer.receiveBody("activemq:ActiveMQ.DLQ", 3000);
+
+        assertEquals(message, dlqBody);
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+        ConnectionFactory connectionFactory = CamelJmsTestHelper.createConnectionFactory();
+        camelContext.addComponent("activemq", jmsComponentTransacted(connectionFactory));
+        return camelContext;
+    }
+}
\ No newline at end of file