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-examples.git
The following commit(s) were added to refs/heads/master by this push: new 3849e69 CAMEL-14963: Route Template example. 3849e69 is described below commit 3849e696aca2840ec5c297d715ee99421961585b Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Jul 2 19:05:52 2020 +0200 CAMEL-14963: Route Template example. --- examples/camel-example-routetemplate/readme.adoc | 8 +++++- .../org/apache/camel/example/MyApplication.java | 4 +-- .../{MyMainListener.java => MyConfiguration.java} | 33 +++++++++++++--------- .../org/apache/camel/example/MyRouteTemplates.java | 10 +++++++ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/examples/camel-example-routetemplate/readme.adoc b/examples/camel-example-routetemplate/readme.adoc index 4aa2f92..3cb8476 100644 --- a/examples/camel-example-routetemplate/readme.adoc +++ b/examples/camel-example-routetemplate/readme.adoc @@ -1,6 +1,12 @@ == Camel Example Route Template -TODO: Update +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 `MyConfiguration.java` is used to create two routes from the template using different set of parameters. + +The example runs standalone via Camel Main in the `MyApplication.java` source file. === How to run diff --git a/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyApplication.java b/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyApplication.java index d39aad9..955b0a2 100644 --- a/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyApplication.java +++ b/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyApplication.java @@ -29,10 +29,10 @@ public final class MyApplication { public static void main(String[] args) throws Exception { // use Camels Main class Main main = new Main(); - // add listener that create routes from the template - main.addMainListener(new MyMainListener()); // and add route templates via routes builder main.configure().addRoutesBuilder(MyRouteTemplates.class); + // add configuration class which setup the routes from the route templates + main.configure().addConfigurationClass(MyConfiguration.class); // now keep the application running until the JVM is terminated (ctrl + c or sigterm) main.run(args); } diff --git a/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyMainListener.java b/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyConfiguration.java similarity index 52% rename from examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyMainListener.java rename to examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyConfiguration.java index 959b234..a3aa603 100644 --- a/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyMainListener.java +++ b/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyConfiguration.java @@ -16,23 +16,28 @@ */ package org.apache.camel.example; -import org.apache.camel.main.BaseMainSupport; -import org.apache.camel.main.MainListenerSupport; +import org.apache.camel.CamelContext; -public class MyMainListener extends MainListenerSupport { +public class MyConfiguration { - @Override - public void beforeStart(BaseMainSupport main) { + /** + * Configure and adds routes from route templates. + * + * The method name <tt>configureRouteTemplates</tt> is detected from camel-main + * and invoked after routes and route templates has been loaded which allows + * to create and add routes from the route templates during bootstrap + */ + public void configureRouteTemplates(CamelContext context) { // create two routes from the template - main.getCamelContext().addRouteFromTemplate("myTemplate") - .parameter("name", "one") - .parameter("greeting", "Hello") - .build(); + context.addRouteFromTemplate("myTemplate") + .parameter("name", "one") + .parameter("greeting", "Hello") + .build(); - main.getCamelContext().addRouteFromTemplate("myTemplate") - .parameter("name", "two") - .parameter("greeting", "Bonjour") - .parameter("myPeriod", "5s") - .build(); + context.addRouteFromTemplate("myTemplate") + .parameter("name", "two") + .parameter("greeting", "Bonjour") + .parameter("myPeriod", "5s") + .build(); } } diff --git a/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyRouteTemplates.java b/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyRouteTemplates.java index 79cba25..6040577 100644 --- a/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyRouteTemplates.java +++ b/examples/camel-example-routetemplate/src/main/java/org/apache/camel/example/MyRouteTemplates.java @@ -18,14 +18,24 @@ package org.apache.camel.example; import org.apache.camel.builder.RouteBuilder; +/** + * Route templates using {@link RouteBuilder} which allows + * us to define a number of templates (parameterized routes) + * which we can create routes from. + */ 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}");