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/07/08 12:06:47 UTC

[camel-examples] branch main updated: CAMEL-16757: camel-core - Global error handling, interceptor in all DSL

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 0306aad  CAMEL-16757: camel-core - Global error handling, interceptor in all DSL
0306aad is described below

commit 0306aad682bb61ac8390e79fb08c04e880338605
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jul 8 14:06:29 2021 +0200

    CAMEL-16757: camel-core - Global error handling, interceptor in all DSL
---
 examples/pom.xml                                   |   1 +
 examples/routes-configuration/README.adoc          |  26 +++++
 examples/routes-configuration/pom.xml              | 128 +++++++++++++++++++++
 .../org/apache/camel/example/MyApplication.java    |  39 +++++++
 .../src/main/resources/application.properties      |  28 +++++
 .../src/main/resources/logback.xml                 |  30 +++++
 .../main/resources/myerror/GlobalErrorHandler.java |  27 +++++
 .../src/main/resources/myerror/global-error.xml    |  27 +++++
 .../src/main/resources/myerror/global-error.yaml   |  26 +++++
 .../main/resources/myroutes/MyRouteBuilder.java    |  35 ++++++
 .../src/main/resources/myroutes/cheese-route.xml   |  30 +++++
 .../src/main/resources/myroutes/my-yaml-route.yaml |  30 +++++
 12 files changed, 427 insertions(+)

diff --git a/examples/pom.xml b/examples/pom.xml
index 9d90533..93d85fb 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -149,6 +149,7 @@
         <module>route-throttling</module>
         <module>routeloader</module>
         <module>routetemplate</module>
+        <module>routes-configuration</module>
         <module>salesforce-consumer</module>
         <module>servlet-tomcat</module>
         <module>splunk</module>
diff --git a/examples/routes-configuration/README.adoc b/examples/routes-configuration/README.adoc
new file mode 100644
index 0000000..8e7768c
--- /dev/null
+++ b/examples/routes-configuration/README.adoc
@@ -0,0 +1,26 @@
+== Camel Example Routes Configuration
+
+This example shows how Camel is capable of loading routes during startup using the new route loader system.
+The route loader has support for loading routes in XML, Java and YAML (other languages to be added).
+
+In this example the focus is on how you can use global _routes configuration_ with the DSL to seperate
+error handling (and other cross routes functionality) from all your routes.
+
+
+=== How to run
+
+You can run this example using
+
+----
+$ mvn camel:run
+----
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/examples/routes-configuration/pom.xml b/examples/routes-configuration/pom.xml
new file mode 100644
index 0000000..980c10f
--- /dev/null
+++ b/examples/routes-configuration/pom.xml
@@ -0,0 +1,128 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel.example</groupId>
+        <artifactId>examples</artifactId>
+        <version>3.12.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-routes-configuration</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Example :: Routes Configuration</name>
+    <description>Example with global routes configuration for error handling</description>
+
+    <properties>
+        <category>Beginner</category>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Add Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-bom</artifactId>
+                <version>${camel.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+
+        <!-- camel main -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-main</artifactId>
+        </dependency>
+        <!-- joor with java route loader -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-java-joor-dsl</artifactId>
+        </dependency>
+        <!-- xml-io with fast xml route loader -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-xml-io-dsl</artifactId>
+        </dependency>
+        <!-- yaml route loader -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-yaml-dsl</artifactId>
+        </dependency>
+        <!-- used to dump routes as XML -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-xml-jaxb</artifactId>
+        </dependency>
+
+        <!-- components -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-timer</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-log</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-bean</artifactId>
+        </dependency>
+
+        <!-- logging -->
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-core</artifactId>
+            <version>${logback-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>${logback-version}</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-maven-plugin</artifactId>
+                <version>${camel.version}</version>
+                <configuration>
+                    <logClasspath>false</logClasspath>
+                    <mainClass>org.apache.camel.main.Main</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/examples/routes-configuration/src/main/java/org/apache/camel/example/MyApplication.java b/examples/routes-configuration/src/main/java/org/apache/camel/example/MyApplication.java
new file mode 100644
index 0000000..6de6397
--- /dev/null
+++ b/examples/routes-configuration/src/main/java/org/apache/camel/example/MyApplication.java
@@ -0,0 +1,39 @@
+/*
+ * 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.example;
+
+import org.apache.camel.main.Main;
+
+/**
+ * Main class that boot the Camel application.
+ *
+ * This class has been added to make it easy to run the application from a Java editor.
+ * However you can start this application by running org.apache.camel.main.Main directory (see Maven pom.xml)
+ */
+public final class MyApplication {
+
+    private MyApplication() {
+    }
+
+    public static void main(String[] args) throws Exception {
+        // use Camels Main class
+        Main main = new Main();
+        // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
+        main.run(args);
+    }
+
+}
diff --git a/examples/routes-configuration/src/main/resources/application.properties b/examples/routes-configuration/src/main/resources/application.properties
new file mode 100644
index 0000000..4f788bb
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/application.properties
@@ -0,0 +1,28 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+# to configure camel main
+# here you can configure options on camel main (see MainConfigurationProperties class)
+camel.main.name = MyRoutesConfiguration
+
+# dump routes as XML (routes are coded in different DSLs but can be dumped as XML)
+camel.main.dump-routes = true
+
+# which directory(s) to scan for routes which can be xml, yaml or java files
+# we can control which error handler to use by changing the filter pattern (*.xml, *.yaml or *.java)
+camel.main.routes-include-pattern=classpath:myroutes/*,classpath:myerror/*.yaml
+
diff --git a/examples/routes-configuration/src/main/resources/logback.xml b/examples/routes-configuration/src/main/resources/logback.xml
new file mode 100644
index 0000000..a798d0b
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/logback.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<configuration>
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+
+    <root level="INFO">
+        <appender-ref ref="STDOUT" />
+    </root>
+</configuration>
diff --git a/examples/routes-configuration/src/main/resources/myerror/GlobalErrorHandler.java b/examples/routes-configuration/src/main/resources/myerror/GlobalErrorHandler.java
new file mode 100644
index 0000000..309555a
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/myerror/GlobalErrorHandler.java
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+import org.apache.camel.builder.RouteConfigurationBuilder;
+
+public class GlobalErrorHandler extends RouteConfigurationBuilder {
+
+    @Override
+    public void configuration() throws Exception {
+        routeConfiguration()
+            .onException(Exception.class).handled(true)
+            .log("Java WARN: ${exception.message}");
+    }
+}
diff --git a/examples/routes-configuration/src/main/resources/myerror/global-error.xml b/examples/routes-configuration/src/main/resources/myerror/global-error.xml
new file mode 100644
index 0000000..dba8481
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/myerror/global-error.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+
+<routeConfiguration>
+    <onException>
+        <exception>java.lang.Exception</exception>
+        <handled><constant>true</constant></handled>
+        <log message="XML WARN: ${exception.message}"/>
+    </onException>
+</routeConfiguration>
diff --git a/examples/routes-configuration/src/main/resources/myerror/global-error.yaml b/examples/routes-configuration/src/main/resources/myerror/global-error.yaml
new file mode 100644
index 0000000..67f9c63
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/myerror/global-error.yaml
@@ -0,0 +1,26 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+- route-configuration:
+    - on-exception:
+        handled:
+          constant: "true"
+        exception:
+          - "java.lang.Exception"
+        steps:
+          - log:
+              message: "YAML WARN ${exception.message}"
diff --git a/examples/routes-configuration/src/main/resources/myroutes/MyRouteBuilder.java b/examples/routes-configuration/src/main/resources/myroutes/MyRouteBuilder.java
new file mode 100644
index 0000000..b4aef0e
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/myroutes/MyRouteBuilder.java
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+import java.util.Random;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("timer:java?period=2s")
+            .setBody(method(MyRouteBuilder.class, "randomNumber"))
+            .log("Random number ${body}")
+            .filter(simple("${body} < 30"))
+                .throwException(new IllegalArgumentException("The number is too low"));
+    }
+
+    public static int randomNumber() {
+        return new Random().nextInt(100);
+    }
+}
diff --git a/examples/routes-configuration/src/main/resources/myroutes/cheese-route.xml b/examples/routes-configuration/src/main/resources/myroutes/cheese-route.xml
new file mode 100644
index 0000000..a21f516
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/myroutes/cheese-route.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+
+<!--
+    if you want to have multiple routes, you can either have multiple files with 1 <route> per file or
+    you can use <routes> as root tag, such as <routes><route>...</route><route>...</route></routes>
+-->
+
+<route>
+    <from uri="timer:xml?period=5s"/>
+    <log message="I am XML"/>
+    <throwException exceptionType="java.lang.Exception" message="Some kind of XML error"/>
+</route>
diff --git a/examples/routes-configuration/src/main/resources/myroutes/my-yaml-route.yaml b/examples/routes-configuration/src/main/resources/myroutes/my-yaml-route.yaml
new file mode 100644
index 0000000..0d71afb
--- /dev/null
+++ b/examples/routes-configuration/src/main/resources/myroutes/my-yaml-route.yaml
@@ -0,0 +1,30 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+- route:
+    from: "timer:yaml?period=3s"
+    steps:
+      - set-body:
+          simple: "Timer fired ${header.CamelTimerCounter} times"
+      - to:
+          uri: "log:yaml"
+          parameters:
+            show-body-type: false
+            show-exchange-pattern: false
+      - throw-exception:
+          exception-type: "java.lang.IllegalArgumentException"
+          message: "Error from yaml"