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 2015/03/25 11:23:45 UTC

[2/6] camel git commit: CAMEL-5452: DoCatch now also emits the failure handled event

CAMEL-5452: DoCatch now also emits the failure handled event


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

Branch: refs/heads/master
Commit: e2183378b12c13707600816339442737778b364e
Parents: f078028
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Mar 25 10:24:08 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Mar 25 11:25:10 2015 +0100

----------------------------------------------------------------------
 .../event/ExchangeFailureHandledEvent.java      |  2 +
 .../apache/camel/processor/CatchProcessor.java  |  7 ++-
 .../java/org/apache/camel/spi/EventFactory.java |  2 +-
 .../EventNotifierFailureHandledEventsTest.java  | 45 ++++++++++++++++++++
 4 files changed, 54 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e2183378/camel-core/src/main/java/org/apache/camel/management/event/ExchangeFailureHandledEvent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/management/event/ExchangeFailureHandledEvent.java b/camel-core/src/main/java/org/apache/camel/management/event/ExchangeFailureHandledEvent.java
index 2c0dd7d..4aeae2c 100644
--- a/camel-core/src/main/java/org/apache/camel/management/event/ExchangeFailureHandledEvent.java
+++ b/camel-core/src/main/java/org/apache/camel/management/event/ExchangeFailureHandledEvent.java
@@ -55,6 +55,8 @@ public class ExchangeFailureHandledEvent extends AbstractExchangeEvent {
         return handled;
     }
 
+    public boolean isContinued() { return !handled; }
+
     @Override
     public String toString() {
         if (isDeadLetterChannel()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/e2183378/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
index 063a776..f30954d 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
@@ -24,6 +24,7 @@ import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.Traceable;
 import org.apache.camel.spi.IdAware;
+import org.apache.camel.util.EventHelper;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
@@ -100,7 +101,11 @@ public class CatchProcessor extends DelegateAsyncProcessor implements Traceable,
 
         boolean sync = processor.process(exchange, new AsyncCallback() {
             public void done(boolean doneSync) {
-                if (!handled) {
+                if (handled) {
+                    ExchangeHelper.setFailureHandled(exchange);
+                    // emit event that the failure was handled
+                    EventHelper.notifyExchangeFailureHandled(exchange.getContext(), exchange, processor, false, null);
+                } else {
                     if (exchange.getException() == null) {
                         exchange.setException(exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class));
                     }

http://git-wip-us.apache.org/repos/asf/camel/blob/e2183378/camel-core/src/main/java/org/apache/camel/spi/EventFactory.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/EventFactory.java b/camel-core/src/main/java/org/apache/camel/spi/EventFactory.java
index 25c9578..e42d178 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/EventFactory.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/EventFactory.java
@@ -161,7 +161,7 @@ public interface EventFactory {
 
     /**
      * Creates an {@link EventObject} when an {@link org.apache.camel.Exchange} has failed
-     * but was handled by the Camel error handlers such as an dead letter channel.
+     * but was handled by the Camel error handlers such as an dead letter channel, or a doTry .. doCatch block.
      *
      * @param exchange          the exchange
      * @param failureHandler    the failure handler such as moving the message to a dead letter queue

http://git-wip-us.apache.org/repos/asf/camel/blob/e2183378/camel-core/src/test/java/org/apache/camel/management/EventNotifierFailureHandledEventsTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/management/EventNotifierFailureHandledEventsTest.java b/camel-core/src/test/java/org/apache/camel/management/EventNotifierFailureHandledEventsTest.java
index f7db679..0b2e313 100644
--- a/camel-core/src/test/java/org/apache/camel/management/EventNotifierFailureHandledEventsTest.java
+++ b/camel-core/src/test/java/org/apache/camel/management/EventNotifierFailureHandledEventsTest.java
@@ -104,6 +104,8 @@ public class EventNotifierFailureHandledEventsTest extends ContextTestSupport {
 
         ExchangeFailureHandledEvent e = assertIsInstanceOf(ExchangeFailureHandledEvent.class, events.get(8));
         assertEquals("should be DLC", true, e.isDeadLetterChannel());
+        assertTrue("should be marked as failure handled", e.isHandled());
+        assertFalse("should not be continued", e.isContinued());
         SendProcessor send = assertIsInstanceOf(SendProcessor.class, e.getFailureHandler());
         assertEquals("mock://dead", send.getDestination().getEndpointUri());
         assertEquals("mock://dead", e.getDeadLetterUri());
@@ -143,6 +145,49 @@ public class EventNotifierFailureHandledEventsTest extends ContextTestSupport {
 
         ExchangeFailureHandledEvent e = assertIsInstanceOf(ExchangeFailureHandledEvent.class, events.get(8));
         assertEquals("should NOT be DLC", false, e.isDeadLetterChannel());
+        assertTrue("should be marked as failure handled", e.isHandled());
+        assertFalse("should not be continued", e.isContinued());
+
+        // onException will handle the exception
+        assertIsInstanceOf(ExchangeCompletedEvent.class, events.get(9));
+        // and the last event should be the direct:start
+        assertIsInstanceOf(ExchangeSentEvent.class, events.get(10));
+        ExchangeSentEvent sent = (ExchangeSentEvent) events.get(10);
+        assertEquals("direct://start", sent.getEndpoint().getEndpointUri());
+    }
+
+    public void testExchangeDoTryDoCatch() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .doTry()
+                        .throwException(new IllegalArgumentException("Damn"))
+                    .doCatch(IllegalArgumentException.class)
+                        .to("mock:dead")
+                    .end();
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:dead").expectedMessageCount(1);
+        template.sendBody("direct:start", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        assertEquals(11, events.size());
+        assertIsInstanceOf(CamelContextStartingEvent.class, events.get(0));
+        assertIsInstanceOf(RouteAddedEvent.class, events.get(1));
+        assertIsInstanceOf(RouteStartedEvent.class, events.get(2));
+        assertIsInstanceOf(CamelContextStartedEvent.class, events.get(3));
+        assertIsInstanceOf(ExchangeSendingEvent.class, events.get(4));
+        assertIsInstanceOf(ExchangeCreatedEvent.class, events.get(5));
+        assertIsInstanceOf(ExchangeSendingEvent.class, events.get(6));
+        assertIsInstanceOf(ExchangeSentEvent.class, events.get(7));
+
+        ExchangeFailureHandledEvent e = assertIsInstanceOf(ExchangeFailureHandledEvent.class, events.get(8));
+        assertEquals("should NOT be DLC", false, e.isDeadLetterChannel());
+        assertFalse("should not be marked as failure handled as it was continued instead", e.isHandled());
+        assertTrue("should be continued", e.isContinued());
 
         // onException will handle the exception
         assertIsInstanceOf(ExchangeCompletedEvent.class, events.get(9));