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 2021/02/01 14:09:47 UTC

[camel] branch master updated: CAMEL-16126: camel-core - errorHandler(ref) to make it easier to specify error handler by refernce

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 22e939d  CAMEL-16126: camel-core - errorHandler(ref) to make it easier to specify error handler by refernce
22e939d is described below

commit 22e939d595bac4e75180067368f3f3f4fd400697
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Feb 1 15:08:57 2021 +0100

    CAMEL-16126: camel-core - errorHandler(ref) to make it easier to specify error handler by refernce
---
 .../org/apache/camel/builder/RouteBuilder.java     | 13 +++++
 .../ErrorHandlerRefPropertyPlaceholderTest.java    | 55 ++++++++++++++++++++++
 .../apache/camel/builder/ErrorHandlerRefTest.java  | 49 +++++++++++++++++++
 3 files changed, 117 insertions(+)

diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java
index 5860f57..355fe43 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java
@@ -276,6 +276,19 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild
     }
 
     /**
+     * Installs the given <a href="http://camel.apache.org/error-handler.html">error handler</a> builder
+     *
+     * @param ref reference to the error handler to use
+     */
+    public void errorHandler(String ref) {
+        if (!getRouteCollection().getRoutes().isEmpty()) {
+            throw new IllegalArgumentException("errorHandler must be defined before any routes in the RouteBuilder");
+        }
+        getRouteCollection().setCamelContext(getContext());
+        setErrorHandlerBuilder(new ErrorHandlerBuilderRef(ref));
+    }
+
+    /**
      * Injects a property placeholder value with the given key converted to the given type.
      *
      * @param  key       the property key
diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerRefPropertyPlaceholderTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerRefPropertyPlaceholderTest.java
new file mode 100644
index 0000000..be78695
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerRefPropertyPlaceholderTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.builder;
+
+import java.util.Properties;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+public class ErrorHandlerRefPropertyPlaceholderTest extends ContextTestSupport {
+
+    @Test
+    public void testRef() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:dead");
+        mock.expectedBodiesReceived("Bye World");
+
+        template.sendBody("direct:start", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                Properties prop = new Properties();
+                prop.put("myErrorHandler", "myDead");
+                context.getPropertiesComponent().setOverrideProperties(prop);
+
+                DeadLetterChannelBuilder dlc = new DeadLetterChannelBuilder("mock:dead");
+                context.getRegistry().bind("myDead", dlc);
+
+                errorHandler("{{myErrorHandler}}");
+
+                from("direct:start").throwException(new IllegalArgumentException("Forced"));
+            }
+        };
+    }
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerRefTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerRefTest.java
new file mode 100644
index 0000000..96e7c5f
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerRefTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.builder;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+public class ErrorHandlerRefTest extends ContextTestSupport {
+
+    @Test
+    public void testRef() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:dead");
+        mock.expectedBodiesReceived("Bye World");
+
+        template.sendBody("direct:start", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                DeadLetterChannelBuilder dlc = new DeadLetterChannelBuilder("mock:dead");
+                context.getRegistry().bind("myDead", dlc);
+
+                errorHandler("myDead");
+
+                from("direct:start").throwException(new IllegalArgumentException("Forced"));
+            }
+        };
+    }
+}