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-spring-boot-examples.git
The following commit(s) were added to refs/heads/master by this push: new 59528d2 CAMEL-15926: spring boot now support route templates 59528d2 is described below commit 59528d24927f22871309be5a4185fd84e17c60c4 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Dec 8 10:33:37 2020 +0100 CAMEL-15926: spring boot now support route templates --- camel-example-spring-boot-routetemplate/pom.xml | 124 +++++++++++++++++++++ .../readme.adoc | 30 +++++ .../main/java/sample/camel/MyCamelApplication.java | 37 ++++++ .../main/java/sample/camel/MyRouteTemplates.java | 45 ++++++++ .../main/java/sample/camel/MyTemplateBuilder.java | 51 +++++++++ .../src/main/resources/application.properties | 30 +++++ .../sample/camel/MyCamelApplicationJUnit5Test.java | 47 ++++++++ pom.xml | 1 + 8 files changed, 365 insertions(+) diff --git a/camel-example-spring-boot-routetemplate/pom.xml b/camel-example-spring-boot-routetemplate/pom.xml new file mode 100644 index 0000000..af0fd46 --- /dev/null +++ b/camel-example-spring-boot-routetemplate/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.7.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-routetemplate</artifactId> + <name>Camel SB Examples :: Spring Boot :: Route Template</name> + <description>How to use route templates (parameterized routes)</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/camel-example-spring-boot-routetemplate/readme.adoc b/camel-example-spring-boot-routetemplate/readme.adoc new file mode 100644 index 0000000..2a04c34 --- /dev/null +++ b/camel-example-spring-boot-routetemplate/readme.adoc @@ -0,0 +1,30 @@ +== Camel Example Spring Boot Route Template + +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. + +The route template is defined via Java or XML DSL (RouteBuilder) 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. + +=== 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/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyCamelApplication.java b/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyCamelApplication.java new file mode 100644 index 0000000..6c91e07 --- /dev/null +++ b/camel-example-spring-boot-routetemplate/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/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyRouteTemplates.java b/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyRouteTemplates.java new file mode 100644 index 0000000..b655e07 --- /dev/null +++ b/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyRouteTemplates.java @@ -0,0 +1,45 @@ +/* + * 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.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 +public class MyRouteTemplates extends RouteBuilder { + + @Override + public void configure() throws Exception { + // create a route template with the given name + routeTemplate("myTemplate") + // here we define the required input parameters (can have default values) + .templateParameter("name") + .templateParameter("greeting") + .templateParameter("myPeriod", "3s") + // here comes the route in the template + // 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}")) + .log("${body}"); + } +} diff --git a/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java b/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java new file mode 100644 index 0000000..f0327f2 --- /dev/null +++ b/camel-example-spring-boot-routetemplate/src/main/java/sample/camel/MyTemplateBuilder.java @@ -0,0 +1,51 @@ +/* + * 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.CamelContext; +import org.apache.camel.main.ConfigureRouteTemplates; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MyTemplateBuilder implements ConfigureRouteTemplates { + + /** + * Configure and adds routes from route templates. + */ + @Override + public void configure(CamelContext context) { + // 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.properties, + // less power as its key value pair properties + // and you can also use both java and properties together + + // in this example we use properties by default and have disabled java + /* + TemplatedRouteBuilder.builder(context, "myTemplate") + .parameter("name", "one") + .parameter("greeting", "Hello") + .add(); + + TemplatedRouteBuilder.builder(context, "myTemplate") + .parameter("name", "two") + .parameter("greeting", "Bonjour") + .parameter("myPeriod", "5s") + .add(); + */ + } +} diff --git a/camel-example-spring-boot-routetemplate/src/main/resources/application.properties b/camel-example-spring-boot-routetemplate/src/main/resources/application.properties new file mode 100644 index 0000000..c766913 --- /dev/null +++ b/camel-example-spring-boot-routetemplate/src/main/resources/application.properties @@ -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. +## --------------------------------------------------------------------------- + +# the name of Camel +camel.springboot.name = MyCamel + +# create routes from the route template +# this can also be done in Java code, see MyTemplateBuilder.java +camel.route-template.config[0].template-id = myTemplate +camel.route-template.config[0].name = one +camel.route-template.config[0].greeting = Hello +camel.route-template.config[1].template-id = myTemplate +camel.route-template.config[1].name = two +camel.route-template.config[1].greeting = Bonjour +camel.route-template.config[1].my-period = 5s + diff --git a/camel-example-spring-boot-routetemplate/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java b/camel-example-spring-boot-routetemplate/src/test/java/sample/camel/MyCamelApplicationJUnit5Test.java new file mode 100644 index 0000000..21c7a2a --- /dev/null +++ b/camel-example-spring-boot-routetemplate/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)); + } + +} diff --git a/pom.xml b/pom.xml index 96f6b2a..a9c6caa 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,7 @@ <module>camel-example-spring-boot-rest-swagger-simple</module> <module>camel-example-spring-boot-rest-openapi</module> <module>camel-example-spring-boot-rest-openapi-simple</module> + <module>camel-example-spring-boot-routetemplate</module> <module>camel-example-spring-boot-servicecall</module> <module>camel-example-spring-boot-supervising-route-controller</module> <module>camel-example-spring-boot-twitter-salesforce</module>