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-spring-boot-examples.git
The following commit(s) were added to refs/heads/main by this push:
new 69ab132 CAMEL-17611: Add examples to show how to use templatedRoute
(#50)
69ab132 is described below
commit 69ab132c12d744a8729732d9395cb0af7e6233a1
Author: Nicolas Filotto <[email protected]>
AuthorDate: Thu Feb 10 12:54:54 2022 +0100
CAMEL-17611: Add examples to show how to use templatedRoute (#50)
---
README.adoc | 6 +-
pom.xml | 1 +
routetemplate-xml/README.adoc | 37 ++++++
routetemplate-xml/pom.xml | 124 +++++++++++++++++++++
.../main/java/sample/camel/MyCamelApplication.java | 37 ++++++
.../src/main/resources/application.yaml | 14 +--
.../src/main/resources/builders/my-builder.xml | 28 ++---
.../src/main/resources/templates}/my-template.xml | 2 +-
.../sample/camel/MyCamelApplicationJUnit5Test.java | 47 ++++++++
routetemplate/{readme.adoc => README.adoc} | 19 +++-
.../main/java/sample/camel/MyRouteTemplates.java | 7 +-
.../main/java/sample/camel/MyTemplateBuilder.java | 28 ++---
routetemplate/src/main/resources/application.yaml | 2 +-
.../src/main/resources/mycamel/my-template.xml | 2 +-
14 files changed, 295 insertions(+), 59 deletions(-)
diff --git a/README.adoc b/README.adoc
index 975057c..579f7cb 100644
--- a/README.adoc
+++ b/README.adoc
@@ -27,7 +27,7 @@ readme's instructions.
=== Examples
// examples: START
-Number of Examples: 55 (0 deprecated)
+Number of Examples: 56 (0 deprecated)
[width="100%",cols="4,2,4",options="header"]
|===
@@ -57,7 +57,9 @@ Number of Examples: 55 (0 deprecated)
| 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:routetemplate/README.adoc[Routetemplate] (routetemplate) | Beginner |
How to use route templates (parameterized routes)
+
+| link:routetemplate-xml/README.adoc[Routetemplate Xml] (routetemplate-xml) |
Beginner | How to use route templates (parameterized routes) in XML
| 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 8bcac21..3af2ad1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -73,6 +73,7 @@
<module>rest-openapi-simple</module>
<module>rest-openapi-springdoc</module>
<module>routetemplate</module>
+ <module>routetemplate-xml</module>
<module>route-reload</module>
<module>routes-configuration</module>
<module>servicecall</module>
diff --git a/routetemplate-xml/README.adoc b/routetemplate-xml/README.adoc
new file mode 100644
index 0000000..b80c326
--- /dev/null
+++ b/routetemplate-xml/README.adoc
@@ -0,0 +1,37 @@
+== Camel Example Spring Boot Route Template in XML
+
+This examples shows how to use Route Templates (parameterized routes) to
specify a skeleton route
+which can be used for creating and adding new routes via parameters in pure
XML.
+
+The route template is defined via XML DSL (RouteBuilder) in the resource
`templates/my-route-templates.xml`.
+
+The resource `builders/my-template-builder.xml` is used to create two routes
from the template using different set of parameters by leveraging the XML DSL.
+
+=== Build
+
+You will need to compile this example first:
+
+----
+$ mvn compile
+----
+
+=== 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/routetemplate-xml/pom.xml b/routetemplate-xml/pom.xml
new file mode 100644
index 0000000..82753d1
--- /dev/null
+++ b/routetemplate-xml/pom.xml
@@ -0,0 +1,124 @@
+<?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.16.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-example-spring-boot-routetemplate-xml</artifactId>
+ <name>Camel SB Examples :: Spring Boot :: Route Template :: XML</name>
+ <description>How to use route templates (parameterized routes) in
XML</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>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-undertow</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-actuator</artifactId>
+ </dependency>
+
+ <!-- Camel -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-quartz-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/routetemplate-xml/src/main/java/sample/camel/MyCamelApplication.java
b/routetemplate-xml/src/main/java/sample/camel/MyCamelApplication.java
new file mode 100644
index 0000000..6c91e07
--- /dev/null
+++ b/routetemplate-xml/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/routetemplate/src/main/resources/application.yaml
b/routetemplate-xml/src/main/resources/application.yaml
similarity index 72%
copy from routetemplate/src/main/resources/application.yaml
copy to routetemplate-xml/src/main/resources/application.yaml
index cd1b49c..d6395e1 100644
--- a/routetemplate/src/main/resources/application.yaml
+++ b/routetemplate-xml/src/main/resources/application.yaml
@@ -18,16 +18,4 @@
camel:
springboot:
name: MyCamel
- routes-include-pattern: classpath:mycamel/*.xml
-
- route-template:
- config:
- -0.template-id: myXmlTemplate
- -0.route-id: first
- -0.name: one
- -0.greeting: Hello
- -1.template-id: myXmlTemplate
- -1.route-id: second
- -1.name: two
- -1.greeting: Bonjour
- -1.my-period: 5s
+ routes-include-pattern: classpath:templates/*.xml,classpath:builders/*.xml
diff --git a/routetemplate/src/main/resources/mycamel/my-template.xml
b/routetemplate-xml/src/main/resources/builders/my-builder.xml
similarity index 61%
copy from routetemplate/src/main/resources/mycamel/my-template.xml
copy to routetemplate-xml/src/main/resources/builders/my-builder.xml
index 0ab44ac..eb26eda 100644
--- a/routetemplate/src/main/resources/mycamel/my-template.xml
+++ b/routetemplate-xml/src/main/resources/builders/my-builder.xml
@@ -17,19 +17,15 @@
limitations under the License.
-->
-<routeTemplates xmlns="http://camel.apache.org/schema/spring">
-
- <routeTemplate id="myXmlTemplate">
- <templateParameter name="name"/>
- <templateParameter name="greeting"/>
- <templateParameter name="myPeriod" defaultValue="3s"/>
- <route>
- <from uri="timer:{{name}}?period={{myPeriod}}"/>
- <setBody>
- <simple>{{greeting}} ${body}</simple>
- </setBody>
- <log message="XML says ${body}"/>
- </route>
- </routeTemplate>
-
-</routeTemplates>
+<templatedRoutes id="camel" xmlns="http://camel.apache.org/schema/spring">
+ <!-- create two routes from the template -->
+ <templatedRoute routeTemplateRef="myXmlTemplate">
+ <parameter name="name" value="one"/>
+ <parameter name="greeting" value="Hello"/>
+ </templatedRoute>
+ <templatedRoute routeTemplateRef="myXmlTemplate">
+ <parameter name="name" value="deux"/>
+ <parameter name="greeting" value="Bonjour"/>
+ <parameter name="myPeriod" value="5s"/>
+ </templatedRoute>
+</templatedRoutes>
diff --git a/routetemplate/src/main/resources/mycamel/my-template.xml
b/routetemplate-xml/src/main/resources/templates/my-template.xml
similarity index 96%
copy from routetemplate/src/main/resources/mycamel/my-template.xml
copy to routetemplate-xml/src/main/resources/templates/my-template.xml
index 0ab44ac..a689971 100644
--- a/routetemplate/src/main/resources/mycamel/my-template.xml
+++ b/routetemplate-xml/src/main/resources/templates/my-template.xml
@@ -26,7 +26,7 @@
<route>
<from uri="timer:{{name}}?period={{myPeriod}}"/>
<setBody>
- <simple>{{greeting}} ${body}</simple>
+ <simple>{{greeting}} {{name}}</simple>
</setBody>
<log message="XML says ${body}"/>
</route>
diff --git
a/routetemplate-xml/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java
b/routetemplate-xml/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java
new file mode 100644
index 0000000..1d6ae2d
--- /dev/null
+++
b/routetemplate-xml/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)
+class MyCamelApplicationJUnit5Test {
+
+ @Autowired
+ private CamelContext camelContext;
+
+ @Test
+ 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));
+ }
+
+}
diff --git a/routetemplate/readme.adoc b/routetemplate/README.adoc
similarity index 63%
rename from routetemplate/readme.adoc
rename to routetemplate/README.adoc
index 7a85373..a0ca5fe 100644
--- a/routetemplate/readme.adoc
+++ b/routetemplate/README.adoc
@@ -10,14 +10,27 @@ The Java is in the `MyRouteTemplates.java` source file.
The `application.properties` is used to create two routes from the template
using different set of parameters.
-An alternative to create routes is from Java which is done in the
`MyConfiguration.java` class.
-See the `MyApplication.java` where you can change the source to use java
instead of property file for the template parameters.
+An alternative to create routes is from Java which is done in the
`MyTemplateBuilder.java` class.
+
+See the `MyCamelApplication.java` where you can change the source to use java
instead of property file for the template parameters.
+
+In case you would like to see how to create rules from a route template in
Java, simply uncomment the annotation `@Component` in the classes
`MyTemplateBuilder` and `MyRouteTemplates` to allow Spring Boot to
automatically discover them.
+
+=== Build
+
+You will need to compile this example first:
+
+----
+$ mvn compile
+----
=== How to run
You can run this example using
- mvn spring-boot:run
+----
+$ mvn spring-boot:run
+----
=== Help and contributions
diff --git a/routetemplate/src/main/java/sample/camel/MyRouteTemplates.java
b/routetemplate/src/main/java/sample/camel/MyRouteTemplates.java
index 931b399..dff6fa3 100644
--- a/routetemplate/src/main/java/sample/camel/MyRouteTemplates.java
+++ b/routetemplate/src/main/java/sample/camel/MyRouteTemplates.java
@@ -17,20 +17,18 @@
package sample.camel;
import org.apache.camel.builder.RouteBuilder;
-import org.springframework.stereotype.Component;
/**
* Route templates using {@link RouteBuilder} which allows
* us to define a number of templates (parameterized routes)
* which we can create routes from.
*/
-@Component
+//@org.springframework.stereotype.Component
public class MyRouteTemplates extends RouteBuilder {
@Override
public void configure() throws Exception {
// in this example we have created the template in XML and this is
disabled
- /*
// create a route template with the given name
routeTemplate("myTemplate")
// here we define the required input parameters (can have default
values)
@@ -41,8 +39,7 @@ public class MyRouteTemplates extends RouteBuilder {
// notice how we use {{name}} to refer to the template parameters
// we can also use {{propertyName}} to refer to property
placeholders
.from("timer:{{name}}?period={{myPeriod}}")
- .setBody(simple("{{greeting}} ${body}"))
+ .setBody(simple("{{greeting}} {{name}}"))
.log("Java says ${body}");
- */
}
}
diff --git a/routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java
b/routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java
index b440b26..cf6e5d7 100644
--- a/routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java
+++ b/routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java
@@ -16,18 +16,16 @@
*/
package sample.camel;
-import org.apache.camel.CamelContext;
-import org.apache.camel.main.ConfigureRouteTemplates;
-import org.springframework.context.annotation.Configuration;
+import org.apache.camel.builder.RouteBuilder;
-@Configuration
-public class MyTemplateBuilder implements ConfigureRouteTemplates {
+//@org.springframework.stereotype.Component
+public class MyTemplateBuilder extends RouteBuilder {
/**
* Configure and adds routes from route templates.
*/
@Override
- public void configure(CamelContext context) {
+ public void configure() throws Exception {
// to configure route templates we can use java code as below from a
template builder class,
// gives more power as its java code.
// or we can configure as well from application.yaml,
@@ -35,17 +33,13 @@ public class MyTemplateBuilder implements
ConfigureRouteTemplates {
// and you can also use both java and yaml together
// in this example we use properties by default and have disabled java
- /*
- TemplatedRouteBuilder.builder(context, "myTemplate")
- .parameter("name", "one")
- .parameter("greeting", "Hello")
- .add();
+ templatedRoute("myTemplate")
+ .parameter("name", "one")
+ .parameter("greeting", "Hello");
- TemplatedRouteBuilder.builder(context, "myTemplate")
- .parameter("name", "two")
- .parameter("greeting", "Bonjour")
- .parameter("myPeriod", "5s")
- .add();
- */
+ templatedRoute("myTemplate")
+ .parameter("name", "deux")
+ .parameter("greeting", "Bonjour")
+ .parameter("myPeriod", "5s");
}
}
diff --git a/routetemplate/src/main/resources/application.yaml
b/routetemplate/src/main/resources/application.yaml
index cd1b49c..690d146 100644
--- a/routetemplate/src/main/resources/application.yaml
+++ b/routetemplate/src/main/resources/application.yaml
@@ -28,6 +28,6 @@ camel:
-0.greeting: Hello
-1.template-id: myXmlTemplate
-1.route-id: second
- -1.name: two
+ -1.name: deux
-1.greeting: Bonjour
-1.my-period: 5s
diff --git a/routetemplate/src/main/resources/mycamel/my-template.xml
b/routetemplate/src/main/resources/mycamel/my-template.xml
index 0ab44ac..a689971 100644
--- a/routetemplate/src/main/resources/mycamel/my-template.xml
+++ b/routetemplate/src/main/resources/mycamel/my-template.xml
@@ -26,7 +26,7 @@
<route>
<from uri="timer:{{name}}?period={{myPeriod}}"/>
<setBody>
- <simple>{{greeting}} ${body}</simple>
+ <simple>{{greeting}} {{name}}</simple>
</setBody>
<log message="XML says ${body}"/>
</route>