CAMEL-16757: YAML DSL to support route configuration
diff --git a/README.adoc b/README.adoc
index 0a56a58..884786d 100644
--- a/README.adoc
+++ b/README.adoc
@@ -27,7 +27,7 @@
=== Examples
// examples: START
-Number of Examples: 50 (0 deprecated)
+Number of Examples: 51 (0 deprecated)
[width="100%",cols="4,2,4",options="header"]
|===
@@ -51,6 +51,8 @@
| link:rest-swagger-simple/README.adoc[REST Swagger] (rest-swagger-simple) | Beginner | This example shows how to call a Rest service defined using Swagger specification
+| link:routes-configuration/readme.adoc[Routes Configuration] (routes-configuration) | Beginner | Example with global routes configuration for error handling
+
| link:routetemplate/readme.adoc[Routetemplate] (routetemplate) | Beginner | How to use route templates (parameterized routes)
| link:splitter-eip/README.adoc[Splitter Eip] (splitter-eip) | Beginner | An example showing Splitter EIP with Camel and Spring Boot
diff --git a/pom.xml b/pom.xml
index db3e40f..6e380aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -71,6 +71,7 @@
<module>rest-openapi</module>
<module>rest-openapi-simple</module>
<module>routetemplate</module>
+ <module>routes-configuration</module>
<module>servicecall</module>
<module>supervising-route-controller</module>
<module>twitter-salesforce</module>
diff --git a/routes-configuration/pom.xml b/routes-configuration/pom.xml
new file mode 100644
index 0000000..f24fe88
--- /dev/null
+++ b/routes-configuration/pom.xml
@@ -0,0 +1,127 @@
+<?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.springboot.example</groupId>
+ <artifactId>examples</artifactId>
+ <version>3.12.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-example-spring-boot-routes-configuration</artifactId>
+ <name>Camel SB Examples :: Spring Boot :: Routes Configuration</name>
+ <description>Example with global routes configuration for error handling</description>
+
+ <properties>
+ <category>Beginner</category>
+
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <spring.boot-version>${spring-boot-version}</spring.boot-version>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- Spring Boot BOM -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-dependencies</artifactId>
+ <version>${spring.boot-version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <!-- Camel BOM -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-boot-bom</artifactId>
+ <version>${camel-version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+
+ <!-- Spring Boot -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-tomcat</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <!-- Camel -->
+ <!-- xml-io with fast xml route loader -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-xml-io-dsl-starter</artifactId>
+ </dependency>
+ <!-- yaml route loader -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-yaml-dsl-starter</artifactId>
+ </dependency>
+ <!-- used to dump routes as XML -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-xml-jaxb-dsl-starter</artifactId>
+ </dependency>
+
+ <!-- test -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test-spring-junit5</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
+ <version>${spring-boot-version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>repackage</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/routes-configuration/readme.adoc b/routes-configuration/readme.adoc
new file mode 100644
index 0000000..9b84e52
--- /dev/null
+++ b/routes-configuration/readme.adoc
@@ -0,0 +1,31 @@
+== Camel Example Spring Boot Route Template
+
+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 separate
+error handling (and other cross routes functionality) from all your routes.
+
+This example has one route in Java, XML and YAML. Each of those routes refer to a
+specific route configuration, which is also _coded_ in the same language as the route.
+But this is not required, you can use Java to code your route configurations for
+advanced error handling, and then _code_ your routes in other languages such as XML or YAML.
+
+=== How to run
+
+You can run this example using
+
+ mvn spring-boot:run
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/support.html[let us know].
+
+We also love contributors, so
+https://camel.apache.org/contributing.html[get involved] :-)
+
+The Camel riders!
+
+
+
diff --git a/routes-configuration/src/main/java/sample/camel/MyCamelApplication.java b/routes-configuration/src/main/java/sample/camel/MyCamelApplication.java
new file mode 100644
index 0000000..6c91e07
--- /dev/null
+++ b/routes-configuration/src/main/java/sample/camel/MyCamelApplication.java
@@ -0,0 +1,37 @@
+/*
+ * 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 sample.camel;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+//CHECKSTYLE:OFF
+/**
+ * A sample Spring Boot application that starts the Camel routes.
+ */
+@SpringBootApplication
+public class MyCamelApplication {
+
+ /**
+ * A main method to start this application.
+ */
+ public static void main(String[] args) {
+ SpringApplication.run(MyCamelApplication.class, args);
+ }
+
+}
+//CHECKSTYLE:ON
diff --git a/routes-configuration/src/main/java/sample/camel/MyJavaErrorHandler.java b/routes-configuration/src/main/java/sample/camel/MyJavaErrorHandler.java
new file mode 100644
index 0000000..bf86a4b
--- /dev/null
+++ b/routes-configuration/src/main/java/sample/camel/MyJavaErrorHandler.java
@@ -0,0 +1,32 @@
+/*
+ * 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 sample.camel;
+
+import org.apache.camel.builder.RouteConfigurationBuilder;
+import org.springframework.stereotype.Component;
+
+// Use @Component to let spring boot discover this class automatic
+@Component
+public class MyJavaErrorHandler extends RouteConfigurationBuilder {
+
+ @Override
+ public void configuration() throws Exception {
+ routeConfiguration("javaError")
+ .onException(Exception.class).handled(true)
+ .log("Java WARN: ${exception.message}");
+ }
+}
diff --git a/routes-configuration/src/main/java/sample/camel/MyJavaRouteBuilder.java b/routes-configuration/src/main/java/sample/camel/MyJavaRouteBuilder.java
new file mode 100644
index 0000000..17cc029
--- /dev/null
+++ b/routes-configuration/src/main/java/sample/camel/MyJavaRouteBuilder.java
@@ -0,0 +1,41 @@
+/*
+ * 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 sample.camel;
+
+import java.util.Random;
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.stereotype.Component;
+
+// Use @Component to let spring boot discover this class automatic
+@Component
+public class MyJavaRouteBuilder extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ from("timer:java?period=2s")
+ // refer to the route configuration by the id to use for this route
+ .routeConfiguration("javaError")
+ .setBody(method(MyJavaRouteBuilder.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/routes-configuration/src/main/resources/application.yaml b/routes-configuration/src/main/resources/application.yaml
new file mode 100644
index 0000000..cd44573
--- /dev/null
+++ b/routes-configuration/src/main/resources/application.yaml
@@ -0,0 +1,29 @@
+#
+# 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.
+#
+
+camel:
+ springboot:
+ name: MyCamel
+
+ # keep Camel running
+ main-run-controller: true
+
+ # dump routes as XML (routes are coded in different DSLs but can be dumped as XML)
+ dump-routes: true
+
+ # which directory(s) to scan for routes/route-configurations which can be xml, yaml or java files
+ routes-include-pattern: classpath:myroutes/*,classpath:myerror/*
diff --git a/routes-configuration/src/main/resources/myerror/xml-error.xml b/routes-configuration/src/main/resources/myerror/xml-error.xml
new file mode 100644
index 0000000..8184b2b
--- /dev/null
+++ b/routes-configuration/src/main/resources/myerror/xml-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 id="xmlError">
+ <onException>
+ <exception>java.lang.Exception</exception>
+ <handled><constant>true</constant></handled>
+ <log message="XML WARN: ${exception.message}"/>
+ </onException>
+</routeConfiguration>
diff --git a/routes-configuration/src/main/resources/myerror/yaml-error.yaml b/routes-configuration/src/main/resources/myerror/yaml-error.yaml
new file mode 100644
index 0000000..44c0824
--- /dev/null
+++ b/routes-configuration/src/main/resources/myerror/yaml-error.yaml
@@ -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.
+## ---------------------------------------------------------------------------
+
+- route-configuration:
+ - id: "yamlError"
+ - on-exception:
+ handled:
+ constant: "true"
+ exception:
+ - "java.lang.Exception"
+ steps:
+ - log:
+ message: "YAML WARN ${exception.message}"
diff --git a/routes-configuration/src/main/resources/myroutes/my-xml-route.xml b/routes-configuration/src/main/resources/myroutes/my-xml-route.xml
new file mode 100644
index 0000000..f59a027
--- /dev/null
+++ b/routes-configuration/src/main/resources/myroutes/my-xml-route.xml
@@ -0,0 +1,31 @@
+<?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>
+-->
+
+<!-- refer to the route configuration by the id to use for this route -->
+<route routeConfiguration="xmlError">
+ <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/routes-configuration/src/main/resources/myroutes/my-yaml-route.yaml b/routes-configuration/src/main/resources/myroutes/my-yaml-route.yaml
new file mode 100644
index 0000000..6876dac
--- /dev/null
+++ b/routes-configuration/src/main/resources/myroutes/my-yaml-route.yaml
@@ -0,0 +1,32 @@
+## ---------------------------------------------------------------------------
+## 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:
+ # refer to the route configuration by the id to use for this route
+ route-configuration: "yamlError"
+ 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"
diff --git a/routes-configuration/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java b/routes-configuration/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java
new file mode 100644
index 0000000..21c7a2a
--- /dev/null
+++ b/routes-configuration/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java
@@ -0,0 +1,47 @@
+/*
+ * 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 sample.camel;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.NotifyBuilder;
+import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@CamelSpringBootTest
+@SpringBootTest(classes = MyCamelApplication.class)
+public class MyCamelApplicationJUnit5Test {
+
+ @Autowired
+ private CamelContext camelContext;
+
+ @Test
+ public void shouldProduceMessages() throws Exception {
+ // we expect that one or more messages is automatic done by the Camel
+ // route as it uses a timer to trigger
+ NotifyBuilder notify = new NotifyBuilder(camelContext).whenDone(1).create();
+
+ assertTrue(notify.matches(10, TimeUnit.SECONDS));
+ }
+
+}