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/05/05 15:11:34 UTC

[camel] 02/05: CAMEL-16394: Route Template local beans. WIP

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

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

commit f9b620fd20135935240de3c46a1e98feca448632
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed May 5 16:23:49 2021 +0200

    CAMEL-16394: Route Template local beans. WIP
---
 .../camel/builder/RouteTemplateLocalBeanTest.java  | 12 ++--
 .../modules/ROOT/pages/route-template.adoc         | 64 ++++++++++++++--------
 2 files changed, 49 insertions(+), 27 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
index 6693ebf..7bbce3c 100644
--- a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
@@ -16,13 +16,13 @@
  */
 package org.apache.camel.builder;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.junit.jupiter.api.Test;
 
-import java.util.concurrent.atomic.AtomicInteger;
-
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -224,7 +224,8 @@ public class RouteTemplateLocalBeanTest extends ContextTestSupport {
             @Override
             public void configure() throws Exception {
                 routeTemplate("myTemplate").templateParameter("foo").templateParameter("bar")
-                        .configure(rtc -> rtc.bind("myBar", (Processor) ex -> ex.getMessage().setBody("Builder " + ex.getMessage().getBody())))
+                        .configure(rtc -> rtc.bind("myBar",
+                                (Processor) ex -> ex.getMessage().setBody("Builder " + ex.getMessage().getBody())))
                         .from("direct:{{foo}}")
                         .to("bean:{{bar}}");
             }
@@ -258,7 +259,10 @@ public class RouteTemplateLocalBeanTest extends ContextTestSupport {
             public void configure() throws Exception {
                 routeTemplate("myTemplate").templateParameter("foo").templateParameter("bar")
                         .configure(rtc -> rtc.bind("myBar", (Processor) ex -> ex.getMessage().setBody("Builder" +
-                                counter.incrementAndGet() + " " + ex.getMessage().getBody())))
+                                                                                                      counter.incrementAndGet()
+                                                                                                      + " "
+                                                                                                      + ex.getMessage()
+                                                                                                              .getBody())))
                         .from("direct:{{foo}}")
                         .to("bean:{{bar}}");
             }
diff --git a/docs/user-manual/modules/ROOT/pages/route-template.adoc b/docs/user-manual/modules/ROOT/pages/route-template.adoc
index a73aec4..556e816 100644
--- a/docs/user-manual/modules/ROOT/pages/route-template.adoc
+++ b/docs/user-manual/modules/ROOT/pages/route-template.adoc
@@ -77,19 +77,9 @@ Template parameters take precedence over regular property placeholders.
 
 == Creating a route from a route template
 
-To create routes from route templates, then you must use the Java API from `CamelContext`.
+To create routes from route templates, then you should use `org.apache.camel.builder.TemplatedRouteBuilder`.
 
-In the following you can see how this is done with the `addRouteFromTemplate` method
-
-[source,java]
-----
-    Map<String, Object> parameters = new HashMap<>();
-    parameters.put("name", "one");
-    parameters.put("greeting", "hello");
-    context.addRouteFromTemplate(null, "myTemplate", parameters);
-----
-
-There is a fluent builder variation as shown below:
+In the following you can see how this is done with the builder as shown below:
 
 [source,java]
 ----
@@ -113,27 +103,55 @@ not started yet.
 If no route id is provided, then Camel will auto assign a route id. In the example above then Camel would
 assign route ids such as `route1`, `route2` to these routes.
 
-If you want to specify a route id, then do as follows, where the name is myCoolRoute:
+If you want to specify a route id, then use `routeId` as follows, where the name is myCoolRoute:
 
 [source,java]
 ----
-    Map<String, Object> parameters = new HashMap<>();
-    parameters.put("name", "one");
-    parameters.put("greeting", "hello");
-    context.addRouteFromTemplate("myCoolRoute", "myTemplate", parameters);
+    TemplatedRouteBuilder.builder(context, "myTemplate")
+        .routeId("myCoolRoute")
+        .parameter("name", "one
+        .parameter("greeting", "hello")
+        .parameter("myPeriod", "5s")
+        .add();
 ----
 
-Or with the fluent builder:
+
+== Binding beans to route template
+
+The route template allows to bind beans which is local scoped and only used as part of creating routes from the template.
+This allows to use the same template to create multiple routes, where beans are local (private) for each created route.
+
+For example given the following route template, then we can configure the bindings (and other things) as shown:
 
 [source,java]
 ----
-    TemplatedRouteBuilder.builder(context, "myTemplate")
-        .routeId("myCoolRoute")
-        .parameter("name", "one")
-        .parameter("greeting", "Hello")
-        .add();
+routeTemplate()
+    .templateParameter("region")
+    .templateParameter("bucket")
+    .configure((RouteTemplateContext rtc) ->
+        rtc.bind("myClient", S3Client.class,
+                S3Client.builder()
+                    .region(rtc.getProperty("region", Region.class))
+                    .build()
+        )
+    )
+    .from("direct:s3-store")
+    .to("aws2-s3:{{bucket}}?amazonS3Client=#myClient")
 ----
 
+The template has two parameters to specify the AWS region and the S3 bucket. To connect to S3
+then a `software.amazon.awssdk.services.s3.S3Client` bean is needed.
+
+To create this bean we can use bind a bean with the id `myClient` that is bound as `java.util.Supplier`.
+The bind operation needs to know the class type that the supplier would return, and therefore you must
+specify `S3Client.class` as parameter.
+
+This ensures that the code creating the bean is executed later when Camel is creating a route from the template.
+This makes the bean with id `myClient` a local bean that is only in the scope (namespace) in the route template.
+
+If multiple routes is created from this template, then each of the created routes, have their own
+S3Client bean created.
+
 == JMX management
 
 The route templates can be dumped as XML from the `ManagedCamelContextMBean` MBean via the `dumpRouteTemplatesAsXml` operation.