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/12/03 08:44:29 UTC

svn commit: r886693 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/DefaultExchange.java main/java/org/apache/camel/impl/InterceptSendToEndpoint.java test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java

Author: davsclaus
Date: Thu Dec  3 07:44:23 2009
New Revision: 886693

URL: http://svn.apache.org/viewvc?rev=886693&view=rev
Log:
CAMEL-2250: interceptSendToEndpoint should skip sending to intended endpoint in case of Exchange failure.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=886693&r1=886692&r2=886693&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java Thu Dec  3 07:44:23 2009
@@ -264,13 +264,11 @@
     }
 
     public boolean isTransacted() {
-        Boolean transacted = getProperty(TRANSACTED, Boolean.class);
-        return transacted != null && transacted;
+        return Boolean.TRUE.equals(getProperty(Exchange.TRANSACTED));
     }
 
     public boolean isRollbackOnly() {
-        Boolean rollback = getProperty(ROLLBACK_ONLY, Boolean.class);
-        return rollback != null && rollback;
+        return Boolean.TRUE.equals(getProperty(Exchange.ROLLBACK_ONLY));
     }
 
     public UnitOfWork getUnitOfWork() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java?rev=886693&r1=886692&r2=886693&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java Thu Dec  3 07:44:23 2009
@@ -115,25 +115,47 @@
                 // add header with the real endpoint uri
                 exchange.getIn().setHeader(Exchange.INTERCEPTED_ENDPOINT, delegate.getEndpointUri());
 
-                detour.process(exchange);
+                try {
+                    detour.process(exchange);
+                } catch (Exception e) {
+                    exchange.setException(e);
+                }
 
-                if (!skip) {
-                    if (!exchange.isFailed()) {
-                        if (exchange.hasOut()) {
-                            // replace OUT with IN as detour changed something
-                            exchange.setIn(exchange.getOut());
-                            exchange.setOut(null);
+                // check for error if so we should break out
+                boolean exceptionHandled = hasExceptionBeenHandledByErrorHandler(exchange);
+                if (exchange.isFailed() || exchange.isRollbackOnly() || exceptionHandled) {
+                    // The Exchange.ERRORHANDLED_HANDLED property is only set if satisfactory handling was done
+                    // by the error handler. It's still an exception, the exchange still failed.
+                    if (LOG.isDebugEnabled()) {
+                        StringBuilder sb = new StringBuilder();
+                        sb.append("Message exchange has failed so skip sending to original intended destination: ").append(getEndpointUri());
+                        sb.append(" for Exchange: ").append(exchange);
+                        if (exchange.isRollbackOnly()) {
+                            sb.append(" Marked as rollback only.");
                         }
-
-                        // route to original destination
-                        producer.process(exchange);
-                    } else {
-                        // exception is failed so do not route to original destination as we can use this to simulate errors
-                        // caused from the intended destination
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Exchange has failed so skip sending to original intended destination: " + getEndpointUri() + " for exchange: " + exchange);
+                        if (exchange.getException() != null) {
+                            sb.append(" Exception: ").append(exchange.getException());
                         }
+                        if (exchange.hasOut() && exchange.getOut().isFault()) {
+                            sb.append(" Fault: ").append(exchange.getOut());
+                        }
+                        if (exceptionHandled) {
+                            sb.append(" Handled by the error handler.");
+                        }
+                        LOG.debug(sb.toString());
                     }
+                    return;
+                }
+
+                if (!skip) {
+                    if (exchange.hasOut()) {
+                        // replace OUT with IN as detour changed something
+                        exchange.setIn(exchange.getOut());
+                        exchange.setOut(null);
+                    }
+
+                    // route to original destination
+                    producer.process(exchange);
                 } else {
                     if (LOG.isDebugEnabled()) {
                         LOG.debug("Stop() means skip sending exchange to original intended destination: " + getEndpointUri() + " for exchange: " + exchange);
@@ -155,6 +177,10 @@
         };
     }
 
+    private static boolean hasExceptionBeenHandledByErrorHandler(Exchange nextExchange) {
+        return Boolean.TRUE.equals(nextExchange.getProperty(Exchange.ERRORHANDLER_HANDLED));
+    }
+
     public Consumer createConsumer(Processor processor) throws Exception {
         return delegate.createConsumer(processor);
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java?rev=886693&r1=886692&r2=886693&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java Thu Dec  3 07:44:23 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.processor.interceptor;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 
@@ -72,6 +73,31 @@
         }
     }
 
+    public void testAdvisedThrowException() throws Exception {
+        context.getRouteDefinitions().get(0).adviceWith(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                interceptSendToEndpoint("mock:foo")
+                    .to("mock:advised")
+                    .throwException(new IllegalArgumentException("Damn"));
+            }
+        });
+
+        getMockEndpoint("mock:foo").expectedMessageCount(0);
+        getMockEndpoint("mock:advised").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:start", "Hello World");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("Damn", e.getCause().getMessage());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {