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 2013/01/12 13:18:55 UTC

svn commit: r1432413 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/processor/ test/java/org/apache/camel/processor/

Author: davsclaus
Date: Sat Jan 12 12:18:54 2013
New Revision: 1432413

URL: http://svn.apache.org/viewvc?rev=1432413&view=rev
Log:
CAMEL-5959: Made the failed route id available for end users in Camel error handler.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelFailureRouteIdTest.java
      - copied, changed from r1432397, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.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/Exchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=1432413&r1=1432412&r2=1432413&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Sat Jan 12 12:18:54 2013
@@ -111,6 +111,7 @@ public interface Exchange {
 
     String FAILURE_HANDLED      = "CamelFailureHandled";
     String FAILURE_ENDPOINT     = "CamelFailureEndpoint";
+    String FAILURE_ROUTE_ID     = "CamelFailureRouteId";
     String FILTER_NON_XML_CHARS = "CamelFilterNonXmlChars";
     String FILE_LOCAL_WORK_PATH = "CamelFileLocalWorkPath";
     String FILE_NAME            = "CamelFileName";

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=1432413&r1=1432412&r2=1432413&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 Sat Jan 12 12:18:54 2013
@@ -784,6 +784,10 @@ public abstract class RedeliveryErrorHan
 
             // store the last to endpoint as the failure endpoint
             exchange.setProperty(Exchange.FAILURE_ENDPOINT, exchange.getProperty(Exchange.TO_ENDPOINT));
+            // and store the route id so we know in which route we failed
+            if (exchange.getUnitOfWork().getRouteContext() != null) {
+                exchange.setProperty(Exchange.FAILURE_ROUTE_ID, exchange.getUnitOfWork().getRouteContext().getRoute().getId());
+            }
 
             // the failure processor could also be asynchronous
             AsyncProcessor afp = AsyncProcessorConverterHelper.convert(processor);
@@ -860,6 +864,10 @@ public abstract class RedeliveryErrorHan
             exchange.setException(exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class));
             // and put failure endpoint back as well
             exchange.setProperty(Exchange.FAILURE_ENDPOINT, exchange.getProperty(Exchange.TO_ENDPOINT));
+            // and store the route id so we know in which route we failed
+            if (exchange.getUnitOfWork().getRouteContext() != null) {
+                exchange.setProperty(Exchange.FAILURE_ROUTE_ID, exchange.getUnitOfWork().getRouteContext().getRoute().getId());
+            }
         }
     }
 

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelFailureRouteIdTest.java (from r1432397, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelFailureRouteIdTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelFailureRouteIdTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java&r1=1432397&r2=1432413&rev=1432413&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelFailureRouteIdTest.java Sat Jan 12 12:18:54 2013
@@ -16,132 +16,50 @@
  */
 package org.apache.camel.processor;
 
-import java.util.Map;
-
-import org.apache.camel.Body;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Headers;
-import org.apache.camel.OutHeaders;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
 /**
- * Unit test to verify that handled policy is working as expected for wiki documentation.
+ * Unit test to test that route id of the failed route is available to the end user.
  */
-public class DeadLetterChannelHandledExampleTest extends ContextTestSupport {
-
-    public void testOrderOK() throws Exception {
-        MockEndpoint result = getMockEndpoint("mock:result");
-        result.expectedBodiesReceived("Order OK");
-        result.expectedHeaderReceived("orderid", "123");
+public class DeadLetterChannelFailureRouteIdTest extends ContextTestSupport {
 
-        MockEndpoint error = getMockEndpoint("mock:error");
-        error.expectedMessageCount(0);
+    public void testFailureRouteId() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        MockEndpoint dead = getMockEndpoint("mock:dead");
+        dead.expectedMessageCount(1);
+        dead.expectedPropertyReceived(Exchange.FAILURE_ROUTE_ID, "bar");
 
-        Object out = template.requestBodyAndHeader("direct:start", "Order: MacBook Pro", "customerid", "444");
-        assertEquals("Order OK", out);
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testOrderERROR() throws Exception {
-        MockEndpoint error = getMockEndpoint("mock:error");
-        error.expectedBodiesReceived("Order ERROR");
-        error.expectedHeaderReceived("orderid", "failed");
-
-        MockEndpoint result = getMockEndpoint("mock:result");
-        result.expectedMessageCount(0);
-
-        Object out = template.requestBodyAndHeader("direct:start", "Order: kaboom", "customerid", "555");
-        assertEquals("Order ERROR", out);
+        template.sendBody("direct:foo", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
+    @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
+            @Override
             public void configure() throws Exception {
-                // START SNIPPET: e1
-                // we do special error handling for when OrderFailedException is thrown
-                onException(OrderFailedException.class)
-                    // we mark the exchange as handled so the caller doesn't receive the
-                    // OrderFailedException but whatever we want to return instead
-                    .handled(true)
-                    // this bean handles the error handling where we can customize the error
-                    // response using java code
-                    .bean(OrderService.class, "orderFailed")
-                    // and since this is an unit test we use mocks for testing
-                    .to("mock:error");
-
-                // this is just the generic error handler where we set the destination
-                // and the number of redeliveries we want to try
-                errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(1));
-
-                // this is our route where we handle orders
-                from("direct:start")
-                    // this bean is our order service
-                    .bean(OrderService.class, "handleOrder")
-                    // this is the destination if the order is OK
+                errorHandler(deadLetterChannel("direct:dead"));
+
+                from("direct:foo").routeId("foo")
+                    .to("mock:foo")
+                    .to("direct:bar")
                     .to("mock:result");
-                // END SNIPPET: e1
-            }
-        };
-    }
 
-    // START SNIPPET: e2
-    /**
-     * Order service as a plain POJO class
-     */
-    public static class OrderService {
-
-        /**
-         * This method handle our order input and return the order
-         *
-         * @param in      the in headers
-         * @param payload the in payload
-         * @param out     the out headers
-         * @return the out payload
-         * @throws OrderFailedException is thrown if the order cannot be processed
-         */
-        public Object handleOrder(@Headers Map<?, ?> in, @Body String payload, @OutHeaders Map<String, Object> out)
-            throws OrderFailedException {
-            out.put("customerid", in.get("customerid"));
-            if ("Order: kaboom".equals(payload)) {
-                throw new OrderFailedException("Cannot order: kaboom");
-            } else {
-                out.put("orderid", "123");
-                return "Order OK";
+                from("direct:bar").routeId("bar")
+                    .to("mock:bar")
+                    .throwException(new IllegalArgumentException("Forced"));
+
+                from("direct:dead")
+                    .log("Failed at route ${property.CamelFailureRouteId}")
+                    .to("mock:dead");
             }
-        }
-
-        /**
-         * This method creates the response to the caller if the order could not be processed
-         * @param in      the in headers
-         * @param payload the in payload
-         * @param out     the out headers
-         * @return the out payload
-         */
-        public Object orderFailed(@Headers Map<?, ?> in, @Body String payload, @OutHeaders Map<String, Object> out) {
-            out.put("customerid", in.get("customerid"));
-            out.put("orderid", "failed");
-            return "Order ERROR";
-        }
-    }
-    // END SNIPPET: e2
-
-    // START SNIPPET: e3
-    /**
-     * Exception thrown if the order cannot be processed
-     */
-    public static class OrderFailedException extends Exception {
-
-        private static final long serialVersionUID = 1L;
-
-        public OrderFailedException(String message) {
-            super(message);
-        }
-        
+        };
     }
-    // END SNIPPET: e3
-
 }
\ No newline at end of file

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java?rev=1432413&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java Sat Jan 12 12:18:54 2013
@@ -0,0 +1,65 @@
+/**
+ * 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;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test to test that route id of the failed route is available to the end user.
+ */
+public class DefaultErrorHandlerFailureRouteIdTest extends ContextTestSupport {
+
+    public void testFailureRouteId() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        Exchange out = template.send("direct:foo", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("Hello World");
+            }
+        });
+        assertNotNull(out);
+
+        assertMockEndpointsSatisfied();
+
+        assertTrue(out.isFailed());
+        assertNotNull(out.getException(IllegalArgumentException.class));
+        assertEquals("bar", out.getProperty(Exchange.FAILURE_ROUTE_ID));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").routeId("foo")
+                    .to("mock:foo")
+                    .to("direct:bar")
+                    .to("mock:result");
+
+                from("direct:bar").routeId("bar")
+                    .to("mock:bar")
+                    .throwException(new IllegalArgumentException("Forced"));
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerFailureRouteIdTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java?rev=1432413&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java Sat Jan 12 12:18:54 2013
@@ -0,0 +1,48 @@
+/**
+ * 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;
+
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test to test that route id of the failed route is available to the end user.
+ */
+public class OnExceptionFailureRouteIdTest extends DeadLetterChannelFailureRouteIdTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(IllegalArgumentException.class).handled(true).to("direct:dead");
+
+                from("direct:foo").routeId("foo")
+                    .to("mock:foo")
+                    .to("direct:bar")
+                    .to("mock:result");
+
+                from("direct:bar").routeId("bar")
+                    .to("mock:bar")
+                    .throwException(new IllegalArgumentException("Forced"));
+
+                from("direct:dead")
+                    .log("Failed at route ${property.CamelFailureRouteId}")
+                    .to("mock:dead");
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnExceptionFailureRouteIdTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date