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.git
The following commit(s) were added to refs/heads/main by this push:
new 4dea9576241f CAMEL-16861: Update docs
4dea9576241f is described below
commit 4dea9576241f9f1bb7eeec022fe1870d53c18ead
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Feb 24 16:08:25 2026 +0100
CAMEL-16861: Update docs
---
.../modules/ROOT/pages/route-template.adoc | 750 +++++++++++++++------
1 file changed, 543 insertions(+), 207 deletions(-)
diff --git a/docs/user-manual/modules/ROOT/pages/route-template.adoc
b/docs/user-manual/modules/ROOT/pages/route-template.adoc
index e4b182301ec6..a23f99afb9bf 100644
--- a/docs/user-manual/modules/ROOT/pages/route-template.adoc
+++ b/docs/user-manual/modules/ROOT/pages/route-template.adoc
@@ -38,11 +38,10 @@ public class MyRouteTemplates extends RouteBuilder {
}
----
-Spring XML DSL::
+XML::
+
[source,xml]
----
-<camelContext>
<routeTemplate id="myTemplate">
<templateParameter name="name"/>
<templateParameter name="greeting"/>
@@ -53,25 +52,28 @@ Spring XML DSL::
<log message="${body}"/>
</route>
</routeTemplate>
-</camelContext>
----
-XML DSL::
+YAML::
+
-[source,xml]
+[source,yaml]
----
-<routeTemplates xmlns="http://camel.apache.org/schema/spring">
- <routeTemplate id="myTemplate">
- <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="${body}"/>
- </route>
- </routeTemplate>
-</routeTemplates>
+- routeTemplate:
+ id: "myTemplate"
+ parameters:
+ - name: "name"
+ - name: "greeting"
+ - name: "myPeriod"
+ defaultValue: "3s"
+ from:
+ uri: "timer:{{name}}?period={{myPeriod}}"
+ steps:
+ - setBody:
+ expression:
+ simple:
+ expression: "{{greeting}} ${body}"
+ - log:
+ message: "${body}"
----
====
@@ -89,6 +91,7 @@ Notice how we use `{\{name}}` and `{\{greeting}}` in the
timer endpoint and the
The route can use regular property placeholders from a properties file as well:
+.Application Properties
[source,properties]
----
greeting = Davs
@@ -104,20 +107,28 @@ Template parameters take precedence over regular property
placeholders.
Template property placeholders are placed between two curly braces:
-[source]
+[source,text]
----
{{myProperty}}
----
-To indicate that a template parameter can have a null value, a question mark
can be used.
+To indicate that a template parameter is optional, and can have a `null`
value, a question mark (`?`) must be used, as shown:
-[source]
+[source,text]
----
{{?myProperty}}
----
-For example:
+For example in the following route the `replyTo` queue for the JMS endpoint is
optional. So if the value is `null` then Camel
+will not assign `null` as `replyTo` in the JMS endpoint, and instead leave it
as if was never configured; for example this allows the option to use its
default setting.
+
+[tabs]
+====
+Java::
++
+Notice how we use `?` in the replyTo option below:
++
[source,java]
----
from("timer:{{name}}?period={{myPeriod}}")
@@ -125,26 +136,114 @@ from("timer:{{name}}?period={{myPeriod}}")
.to("jms:myqueue?replyTo={{?replyToQueue}}");
----
-In case no replyToQueue property is provided when creating the template the
option replyTo is just ignored.
+XML::
++
+Notice how we use `?` in the replyTo option below:
++
+[source,xml]
+----
+<route>
+ <from uri="timer:{{name}}?period={{myPeriod}}"/>
+ <setBody>
+ <simple>{{greeting}} ${body}</simple>
+ </setBody>
+ <to uri="jms:myqueue?replyTo={{?replyToQueue}}"/>
+</route>
+----
+
+YAML::
++
+Notice how we use `?` in the replyTo option below:
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: "timer:{{name}}?period={{myPeriod}}"
+ steps:
+ - setBody:
+ expression:
+ simple:
+ expression: "{{greeting}} ${body}"
+ - to:
+ uri: "jms:myqueue?replyTo={{?replyToQueue}}"
+----
+====
+
+IMPORTANT: In case no replyToQueue property is provided when creating the
template the option replyTo is just ignored.
+
+A property can also have a logical negation using the exclamation mark (`!`):
+
+[source,text]
+----
+{{!myProperty}}
+----
+
+In the following example we have the parameter named `disableTest` which we
then need to negate when configuring
+this on the JMS endpoint for its `testConnectionOnStartup` option. This means
that `disableTest=true` resolves as `testConnectionOnStartup=false`,
+and visa-versa. To support this then we must use the negation using the `!`
mark:
-A property can also have a logical negation using the exclamation mark:
+[tabs]
+====
+Java::
++
[source,java]
----
from("timer:{{name}}?period={{myPeriod}}")
.setBody(simple("{{greeting}} ${body}"))
-
.to("jms:myqueue?replyTo={{?replyToQueue}}&testConnectionOnStartup={{!testConnectionOnStartup}}");
+
.to("jms:myqueue?replyTo={{?replyToQueue}}&testConnectionOnStartup={{!disableTest}}");
----
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="timer:{{name}}?period={{myPeriod}}"/>
+ <setBody>
+ <simple>{{greeting}} ${body}</simple>
+ </setBody>
+ <to
uri="jms:myqueue?replyTo={{?replyToQueue}}&testConnectionOnStartup={{!disableTest}}"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: timer
+ parameters:
+ timerName: "{{name}}"
+ period: "{{myPeriod}}"
+ steps:
+ - setBody:
+ expression:
+ simple:
+ expression: "{{greeting}} ${body}"
+ - to:
+ uri: jms
+ parameters:
+ destinationName: myqueue
+ replyTo: "{{?replyToQueue}}"
+ testConnectionOnStartup: "{{!disableTest}}"
+----
+====
+
== Creating a route from a route template
-To create routes from route templates, then you should use
`org.apache.camel.builder.TemplatedRouteBuilder`.
+To create routes from route templates, then this is possible from standard
Java code, and all the Camel DSLs.
-In the following code snippet, you can see how this is done with the builder:
+In the following code snippet, we create 2 routes from the same template, but
with different parameters.
[tabs]
====
-Java builder::
+Java::
++
+Here we create routes from standard Java using the Camel Java API using
`org.apache.camel.builder.TemplatedRouteBuilder`
+that uses a fluent builder style.
+
[source,java]
----
@@ -152,63 +251,49 @@ Java builder::
TemplatedRouteBuilder.builder(context, "myTemplate")
.parameter("name", "one")
.parameter("greeting", "Hello")
- .add();
+ .add(); // adds a new route from the given input parameters
TemplatedRouteBuilder.builder(context, "myTemplate")
.parameter("name", "two")
.parameter("greeting", "Bonjour")
.parameter("myPeriod", "5s")
- .add();
+ .add(); // adds a new route from the given input parameters
----
Java DSL::
+
+Here we create routes from inside a regular `RouteBuilder` using Java DSL
where each `templatedRoute`
+will create and add a new route from the given input parameters.
++
[source,java]
----
templatedRoute("myTemplate")
.parameter("name", "one")
.parameter("greeting", "Hello");
+
templatedRoute("myTemplate")
.parameter("name", "two")
.parameter("greeting", "Bonjour")
.parameter("myPeriod", "5s");
----
-Spring XML DSL::
+XML::
+
[source,xml]
----
-<camelContext>
<templatedRoute routeTemplateRef="myTemplate">
<parameter name="name" value="one"/>
<parameter name="greeting" value="Hello"/>
</templatedRoute>
- <templatedRoute routeTemplateRef="myTemplate">
- <parameter name="name" value="two"/>
- <parameter name="greeting" value="Bonjour"/>
- <parameter name="myPeriod" value="5s"/>
- </templatedRoute>
-</camelContext>
-----
-XML DSL::
-+
-[source,xml]
-----
-<templatedRoutes xmlns="http://camel.apache.org/schema/spring">
- <templatedRoute routeTemplateRef="myTemplate">
- <parameter name="name" value="one"/>
- <parameter name="greeting" value="Hello"/>
- </templatedRoute>
<templatedRoute routeTemplateRef="myTemplate">
<parameter name="name" value="two"/>
<parameter name="greeting" value="Bonjour"/>
<parameter name="myPeriod" value="5s"/>
</templatedRoute>
-</templatedRoutes>
----
-YAML DSL::
+YAML::
+
[source,yaml]
----
@@ -219,6 +304,7 @@ YAML DSL::
value: "one"
- name: "greeting"
value: "Hello"
+
- templatedRoute:
routeTemplateRef: "myTemplate"
parameters:
@@ -231,18 +317,18 @@ YAML DSL::
----
====
-The returned value from `add` is the route id of the new route that was added.
+TIP: In Java code then the returned value from `add` method is the route id of
the new route that was added.
However `null` is returned if the route is not yet created and added, which
can happen if `CamelContext` is
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 use `routeId` as follows, where the id
is set to myCoolRoute:
+If you want to specify a route id, then use `routeId` as follows, where the id
is set to `myCoolRoute`:
[tabs]
====
-Java builder::
+Java::
+
[source,java]
----
@@ -265,39 +351,24 @@ templatedRoute("myTemplate")
.parameter("myPeriod", "5s");
----
-Spring XML DSL::
-+
-[source,xml]
-----
-<camelContext>
- <templatedRoute routeTemplateRef="myTemplate" routeId="myCoolRoute">
- <parameter name="name" value="one"/>
- <parameter name="greeting" value="hello"/>
- <parameter name="myPeriod" value="5s"/>
- </templatedRoute>
-</camelContext>
-----
-
-XML DSL::
+XML::
+
[source,xml]
----
-<templatedRoutes xmlns="http://camel.apache.org/schema/spring">
<templatedRoute routeTemplateRef="myTemplate" routeId="myCoolRoute">
<parameter name="name" value="one"/>
<parameter name="greeting" value="hello"/>
<parameter name="myPeriod" value="5s"/>
</templatedRoute>
-</templatedRoutes>
----
-YAML DSL::
+YAML::
+
[source,yaml]
----
- templatedRoute:
routeTemplateRef: "myTemplate"
- route-id: "myCoolRoute"
+ routeId: "myCoolRoute"
parameters:
- name: "name"
value: "one"
@@ -308,9 +379,9 @@ YAML DSL::
----
====
-=== Using template parameters with Java DSL simple builder
+=== Using template parameters with Java DSL Simple builder
-When using Java DSL and simple language, then beware that you should
+When using Java DSL and xref:components:languages:simple-language.adoc[Simple]
language, then beware that you should
not use the _simple fluent builder_ when defining the simple
expressions/predicates.
For example, given the following route template in Java DSL:
@@ -326,7 +397,7 @@ public class MyRouteTemplates extends RouteBuilder {
.templateParameter("color")
.from("direct:{{name}}")
.choice()
- .when(simple("{{color}}").isEqualTo("red"))
+ .when(simple("{{color}}").isEqualTo("red")) // <1>
.to("direct:red")
.otherwise()
.to("color:other")
@@ -334,15 +405,43 @@ public class MyRouteTemplates extends RouteBuilder {
}
}
----
+<1> This is not supported (see explanation below)
-Then notice how the simple predicate is using _simple fluent builder_
`simple("{\{color}}").isEqualTo("red")`.
+
+IMPORTANT: Then notice how the simple predicate is using _simple fluent
builder_ `simple("{\{color}}").isEqualTo("red")`.
This is **not supported** with route templates and would not work when
creating multiple routes from the template.
Instead, the simple expression should be a literal String value _only_ as
follows:
+
+[source,java]
----
- .when(simple("'{{color}}' == 'red'")
+.when(simple("'{{color}}' == 'red'")
----
+So the correct solution would be as follows:
+
+[source,java]
+----
+public class MyRouteTemplates extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ routeTemplate("myTemplate")
+ .templateParameter("name")
+ .templateParameter("color")
+ .from("direct:{{name}}")
+ .choice()
+ .when(simple("'{{color}}' == 'red'") // <1>
+ .to("direct:red")
+ .otherwise()
+ .to("color:other")
+ .end();
+ }
+}
+----
+<1> This is supported and the simple expression as a String literal can be
used as-is in all the Camel DSL.
+
+
=== Using hardcoded node IDs in route templates
If route templates contain hardcoded node IDs, then routes created from
templates will use the same IDs.
@@ -350,20 +449,55 @@ Therefore, if two or more routes are created from the
same template, you will ha
Given the route template below, then it has hardcoded ID (`_new-order_`) in
node calling the http services.
+[tabs]
+====
+
+Java::
++
[source,java]
----
-public class MyRouteTemplates extends RouteBuilder {
+routeTemplate("orderTemplate")
+ .templateParameter("queue")
+ .from("jms:{{queue}}")
+ .to("http:orderserver.acme.com/neworder").id("new-order") // <1>
+ .log("Processing order");
+----
+<1> Hardcoded ID on the To EIP
- @Override
- public void configure() throws Exception {
- routeTemplate("orderTemplate")
- .templateParameter("queue")
- .from("jms:{{queue}}")
- .to("http:orderserver.acme.com/neworder").id("new-order")
- .log("Processing order");
- }
-}
+XML::
++
+[source,xml]
+----
+<routeTemplate id="orderTemplate">
+ <templateParameter name="queue"/>
+ <route>
+ <from uri="jms:{{queue}}"/>
+ <to id="new-order" uri="http:orderserver.acme.com/neworder"/> <!--1-->
+ <log message="Processing order"/>
+ </route>
+</routeTemplate>
+----
+<1> Hardcoded ID on the To EIP
+
+YAML::
++
+[source,yaml]
----
+- routeTemplate:
+ id: "myTemplate"
+ parameters:
+ - name: "queue"
+ from:
+ uri: "jms:{{queue}}"
+ steps:
+ - to:
+ id: new-order # <1>
+ uri: http:orderserver.acme.com/neworder
+ - log:
+ message: Processing order
+----
+<1> Hardcoded ID on the To EIP
+====
When creating routes from templates, you can then provide a _prefix_ which is
used for all node IDs.
This allows to create 2 or more routes without _duplicate id_ errors.
@@ -371,8 +505,11 @@ This allows to create 2 or more routes without _duplicate
id_ errors.
For example in the following, we create a new route `_myCoolRoute_` from the
`_myTemplate_` template, and
use a prefix of `_web_`.
-And in Java DSL
+[tabs]
+====
+Java::
++
[source,java]
----
templatedRoute("orderTemplate")
@@ -380,9 +517,9 @@ templatedRoute("orderTemplate")
.prefixId("web")
.parameter("queue", "order.web");
----
-
-Then we can create a 2nd route:
-
++
+Then we can create a 2nd route with a different _prefixId`:
++
[source,java]
----
templatedRoute("orderTemplate")
@@ -391,42 +528,52 @@ templatedRoute("orderTemplate")
.parameter("queue", "order.ftp");
----
-And in Spring XML DSL:
-
+XML::
++
[source,xml]
----
-<camelContext>
<templatedRoute routeTemplateRef="orderTemplate" routeId="webOrder"
prefixId="web">
<parameter name="queue" value="web"/>
</templatedRoute>
-</camelContext>
----
-
-And in XML DSL:
-
++
+Then we can create a 2nd route with a different _prefixId`:
++
[source,xml]
----
-<templatedRoutes xmlns="http://camel.apache.org/schema/spring">
- <templatedRoute routeTemplateRef="orderTemplate" routeId="webOrder"
prefixId="web">
- <parameter name="queue" value="web"/>
+ <templatedRoute routeTemplateRef="orderTemplate" routeId="ftpOrder"
prefixId="ftp">
+ <parameter name="queue" value="order.ftp"/>
</templatedRoute>
-</templatedRoutes>
----
-And in YAML DSL:
-
+YAML::
++
[source,yaml]
----
- templatedRoute:
routeTemplateRef: "orderTemplate"
- route-id: "webOrder"
- prefix-id: "web"
+ routeId: "webOrder"
+ prefixId: "web"
parameters:
- name: "queue"
value: "web"
----
++
+Then we can create a 2nd route with a different _prefixId`:
++
+[source,yaml]
+----
+- templatedRoute:
+ routeTemplateRef: "orderTemplate"
+ routeId: "ftpOrder"
+ prefixId: "ftp"
+ parameters:
+ - name: "queue"
+ value: "order.ftp"
+----
+====
-== Binding beans to route template
+== Binding beans to route template (advanced)
The route template allows binding beans that are locally scoped and only used
as part of creating routes from the template.
This allows using the same template to create multiple routes, where beans are
local (private) for each created route.
@@ -450,8 +597,8 @@ The template has two parameters to specify the AWS region
and the S3 bucket. To
then a `software.amazon.awssdk.services.s3.S3Client` bean is necessary.
To create this bean, we specify this with the `templateBean` DSL where we
specify the bean id as `myClient`.
-The type of the bean can be specified (`S3Client.class`), however, it is
optional
-(can be used if you need to let beans be discovered by type and not by name).
+The type of the bean can be specified (`S3Client.class`), however, it is
optional, and
+can be used if you need to let beans be discovered by type and not by name.
This ensures that the code creating the bean is executed later (when Camel is
creating a route from the template),
then the code must be specified as a _supplier_. Because we want during
creation of the bean access to template parameters,
@@ -471,9 +618,9 @@ The `TemplatedRouteBuilder` also allows to bind local beans
(which allows specif
creating routes from existing templates.
Suppose the route template below is defined in XML:
+
[source,xml]
----
-<camelContext>
<routeTemplate id="s3template">
<templateParameter name="region"/>
<templateParameter name="bucket"/>
@@ -482,7 +629,6 @@ Suppose the route template below is defined in XML:
<to uri="aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"/>
</route>
</routeTemplate>
-</camelContext>
----
The template has no bean bindings for `#{\{myClient}}` which would be required
for creating the template.
@@ -490,6 +636,11 @@ The template has no bean bindings for `#{\{myClient}}`
which would be required f
When creating routes form the template via `TemplatedRouteBuilder` then you
can provide the bean binding
if you desire the bean to be locally scoped (not shared with others):
+[tabs]
+====
+
+Java::
++
[source,java]
----
TemplatedRouteBuilder.builder(context, "s3template")
@@ -502,11 +653,11 @@ TemplatedRouteBuilder.builder(context, "s3template")
.routeId("mys3route")
.add();
----
-
++
As you can see the binding is similar to when using `templateBean` directly in
the route template.
-And in Java DSL:
-
+Java DSL::
++
[source,java]
----
templatedRoute("s3template")
@@ -520,8 +671,8 @@ templatedRoute("s3template")
----
<1> Note that the third parameter of the `bean` method is not directly the
bean but rather a factory method that will be used to create the bean, here we
use a lambda expression as factory method.
-And in XML DSL:
-
+XML::
++
[source,xml]
----
<templatedRoute routeTemplateRef="s3template" routeId="mys3route">
@@ -540,8 +691,8 @@ And in XML DSL:
----
<1> For non-Java DSL, in case of a complex bean factory, you can still rely on
a language like `groovy` to define your bean factory inside a `script` element.
-And in YAML DSL:
-
+YAML::
++
[source,yaml]
----
- templatedRoute:
@@ -563,13 +714,18 @@ And in YAML DSL:
.build()
----
<1> For non-Java DSL, in case of a complex bean factory, you can still rely on
a language like `groovy` to define your bean factory as value of the `script`
key.
+====
Instead of binding the beans from the template builder, you could also create
the bean outside the template,
and bind it by reference.
+[tabs]
+====
+
+Java::
++
[source,java]
----
-
final S3Client myClient = S3Client.builder().region(Region.US_EAST_1).build();
TemplatedRouteBuilder.builder(context, "s3template")
@@ -580,21 +736,25 @@ TemplatedRouteBuilder.builder(context, "s3template")
.add();
----
-And in Java DSL:
-
+Java DSL::
++
[source,java]
----
+final S3Client myClient = S3Client.builder().region(Region.US_EAST_1).build();
+
templatedRoute("s3template")
.parameter("region", "US-EAST-1")
.parameter("bucket", "myBucket")
.bean("myClient", S3Client.class, rtc -> myClient)
.routeId("mys3route");
----
+====
-You should prefer to create the local beans directly from within the template
(if possible) because this
+NOTE: You should prefer to create the local beans directly from within the
template (if possible) because this
ensures the route template has this out of the box. Otherwise, the bean must
be created or provided every time
a new route is created from the route template. However, the latter gives
freedom to create the bean in any other custom way.
+
=== Binding beans to route templates using bean types
You can create a local bean by referring to a fully qualified class name which
Camel will use to create
@@ -623,6 +783,11 @@ public class MyBar {
Then we can use the `MyBar` class as a local bean in a route template as
follows:
+[tabs]
+====
+
+Java::
++
[source,java]
----
routeTemplate("barTemplate")
@@ -634,49 +799,85 @@ routeTemplate("barTemplate")
.property("address", "{{street}}")
.end()
.from("direct:going-out")
- .to("bean:{{myBar}}")
+ .to("bean:{{myBar}}");
----
-
-With Java DSL, you can also refer to the bean class using type safe way:
-
++
+With Java DSL, you can also refer to the bean class using type safe way in
`typeClass` by referring to the `.class` as follows:
++
[source,java]
----
-.templateBean("myBar")
- .typeClass(MyBar.class)
- .property("name", "{{bar}}")
- .property("address", "{{street}}")
-.end()
+routeTemplate("barTemplate")
+ .templateParameter("bar")
+ .templateParameter("street")
+ .templateBean("myBar")
+ .typeClass(MyBar.class)
+ .property("name", "{{bar}}")
+ .property("address", "{{street}}")
+ .end()
+ .from("direct:going-out")
+ .to("bean:{{myBar}}");
----
-In XML DSL you would do:
-
+XML::
++
+In XML, we specify the class FQN name using `#class:` syntax as shown:
++
[source,xml]
----
-<camelContext xmlns="http://camel.apache.org/schema/spring">
- <routeTemplate id="myBar">
- <templateParameter name="bar"/>
- <templateParameter name="street"/>
- <templateBean name="myBean" type="#class:com.foo.MyBar">
- <properties>
- <property key="name" value="{{bar}}"/>
- <property key="address" value="{{street}}"/>
- </properties>
- </templateBean>
- <route>
- <from uri="direct:going-out"/>
- <to uri="bean:{{myBar}}"/>
- </route>
- </routeTemplate>
-</camelContext>
+<routeTemplate id="myBar">
+ <templateParameter name="bar"/>
+ <templateParameter name="street"/>
+ <templateBean name="myBean" type="#class:com.foo.MyBar">
+ <properties>
+ <property key="name" value="{{bar}}"/>
+ <property key="address" value="{{street}}"/>
+ </properties>
+ </templateBean>
+ <route>
+ <from uri="direct:going-out"/>
+ <to uri="bean:{{myBar}}"/>
+ </route>
+</routeTemplate>
+----
+
+YAML::
++
+In YAML, we specify the class FQN name using `#class:` syntax as shown:
++
+[source,yaml]
+----
+- routeTemplate:
+ id: "myBar"
+ parameters:
+ - name: "bar"
+ - name: "street"
+ beans:
+ - name: "myBean"
+ type: "#class:com.foo.MyBar"
+ properties:
+ name: "{{bar}}"
+ address: "{{street}}"
+ from:
+ uri: direct:going-out
+ steps:
+ - to:
+ uri: "bean:{{myBar}}"
----
+====
-=== Binding beans to route templates using scripting languages
-You can use scripting languages like groovy, java, mvel to create the bean.
-This allows defining route templates with the scripting language built-in
(such as groovy).
+=== Binding beans to route templates using scripting languages (advanced)
-For example, creating the AWS S3 client can be done as shown in Java (with
inlined groovy code):
+You can use scripting languages like Groovy, Java, to create the bean.
+This allows defining route templates with the scripting language supported by
Camel such as Groovy.
+For example, creating the AWS S3 client can be done as shown in Java (with
inlined Groovy code):
+
+[tabs]
+====
+
+Java::
++
[source,java]
----
routeTemplate("s3template")
@@ -692,38 +893,16 @@ routeTemplate("s3template")
.to("aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}")
----
-The groovy code can be externalized into a file on the classpath or file
system, by using `resource:` as prefix, such as:
-
-[source,java]
-----
-routeTemplate("s3template")
- .templateParameter("region")
- .templateParameter("bucket")
- .templateBean("myClient", "groovy", "resource:classpath:s3bean.groovy")
- .from("direct:s3-store")
- // must refer to the bean with {{myClient}}
- .to("aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}")
-----
-
-Then create the file `s3bean.groovy` in the classpath root:
-
-[source,groovy]
-----
-import software.amazon.awssdk.services.s3.S3Client
-S3Client.builder()
- .region(rtc.getProperty("region", Region.class))
- .build()
-----
-
-The route template in XML DSL can then also use groovy language to create the
bean as follows:
-
+XML::
++
+Notice how the Groovy code can be inlined directly in the route template in
XML also.
++
[source,xml]
----
-<camelContext>
<routeTemplate id="s3template">
<templateParameter name="region"/>
<templateParameter name="bucket"/>
- <templateBean name="myClient" type="groovy">
+ <templateBean name="myClient" scriptLanguage="groovy">
<script>
import software.amazon.awssdk.services.s3.S3Client
S3Client.builder()
@@ -736,19 +915,66 @@ The route template in XML DSL can then also use groovy
language to create the be
<to uri="aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"/>
</route>
</routeTemplate>
-</camelContext>
----
-Notice how the groovy code can be inlined directly in the route template in
XML also. Of course, you can also externalize
-the bean creation code to an external file, by using `resource:` as prefix:
+YAML::
++
+Notice how the Groovy code can be inlined directly in the route template in
DSL also.
++
+[source,yaml]
+----
+- routeTemplate:
+ id: "s3template"
+ parameters:
+ - name: "region"
+ - name: "bucket"
+ beans:
+ - name: "myClient"
+ scriptLanguage: "groovy"
+ script: |
+ import software.amazon.awssdk.services.s3.S3Client
+ S3Client.builder()
+ .region(rtc.getProperty("region", Region.class))
+ .build()
+ from:
+ uri: direct:s3-store
+ steps:
+ - to:
+ uri: "aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"
+----
+====
+
+The groovy code can be externalized into a file on the classpath or file
system, by using `resource:` as prefix, such as:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+routeTemplate("s3template")
+ .templateParameter("region")
+ .templateParameter("bucket")
+ .templateBean("myClient", "groovy", "resource:classpath:s3bean.groovy")
+ .from("direct:s3-store")
+ // must refer to the bean with {{myClient}}
+ .to("aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}")
+----
+
+XML::
++
+Notice how we can tell Camel the type of the bean is
`software.amazon.awssdk.services.s3.S3Client` which allows
+Camel to better understand, and also makes the bean able for autowiring and
binding via type. The `type` is optional
+and can be omitted. For example in this example as we inject the bean via its
bean id `myClient`.
++
[source,xml]
----
-<camelContext>
<routeTemplate id="s3template">
<templateParameter name="region"/>
<templateParameter name="bucket"/>
- <templateBean name="myClient" type="groovy">
+ <templateBean name="myClient" scriptLanguage="groovy"
+ type="software.amazon.awssdk.services.s3.S3Client">
<script>resource:classpath:s3bean.groovy</script>
</templateBean>
<route>
@@ -756,10 +982,47 @@ the bean creation code to an external file, by using
`resource:` as prefix:
<to uri="aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"/>
</route>
</routeTemplate>
-</camelContext>
----
-The languages supported are:
+YAML::
++
+Notice how we can tell Camel the type of the bean is
`software.amazon.awssdk.services.s3.S3Client` which allows
+Camel to better understand, and also makes the bean able for autowiring and
binding via type. The `type` is optional
+and can be omitted. For example in this example as we inject the bean via its
bean id `myClient`.
++
+[source,yaml]
+----
+- routeTemplate:
+ id: "s3template"
+ parameters:
+ - name: "region"
+ - name: "bucket"
+ beans:
+ - name: "myClient"
+ scriptLanguage: "groovy"
+ type: "software.amazon.awssdk.services.s3.S3Client"
+ script: "resource:classpath:s3bean.groovy"
+ from:
+ uri: direct:s3-store
+ steps:
+ - to:
+ uri: "aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"
+----
+====
+
+Then create the file `s3bean.groovy` in the classpath root:
+
+.s3bean.groovy
+[source,groovy]
+----
+import software.amazon.awssdk.services.s3.S3Client
+
+S3Client.builder()
+ .region(rtc.getProperty("region", Region.class))
+ .build()
+----
+
+The languages supported you can specify as `scriptLanguage` are:
[width="100%",cols="2s,8",options="header"]
|===
@@ -767,7 +1030,7 @@ The languages supported are:
| bean | Calling a method on a Java class to create the bean.
| groovy | Using a groovy script to create the bean.
| java | Java code which is runtime compiled (using jOOR library) to create
the bean.
-| mvel | To use a Mvel template script to create the bean.
+| mvel | To use a MVEL template script to create the bean.
| ognl | To use OGNL template script to create the bean.
| _name_ | To use a third-party language by the given `_name_` to create the
bean.
|===
@@ -779,14 +1042,14 @@ This is what we have done in the scripts in the previous
examples where we get h
[source,groovy]
----
- rtc.getProperty('region', String.class)
+rtc.getProperty('region', String.class)
----
To get access to `CamelContext` you can do:
[source,groovy]
----
- var cn = rtc.getCamelContext().getName()
+var cn = rtc.getCamelContext().getName()
----
The most powerful languages to use are groovy and java. The other languages
are limited in flexibility
@@ -799,11 +1062,28 @@ The bean language can be used when creating the local
bean from an existing Java
and the route templates are not defined using Java code.
For example suppose there is a class named `com.foo.MyAwsHelper` that has a
method called `createS3Client`
-then you can call this method from the route template in XML DSL:
+then you can call this method from the route template as shown in the
following:
+
+[tabs]
+====
+
+Java::
++
+[source,java]
+----
+routeTemplate("s3template")
+ .templateParameter("region")
+ .templateParameter("bucket")
+ .templateBean("myClient", "beam",
"com.foo.MyAwsHelper?method=createS3Client")
+ .from("direct:s3-store")
+ // must refer to the bean with {{myClient}}
+ .to("aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}")
+----
+XML::
++
[source,xml]
----
-<camelContext>
<routeTemplate id="s3template">
<templateParameter name="region"/>
<templateParameter name="bucket"/>
@@ -815,10 +1095,30 @@ then you can call this method from the route template in
XML DSL:
<to uri="aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"/>
</route>
</routeTemplate>
-</camelContext>
----
-The method signature of createS3Client must then have one parameter for the
`RouteTemplateContext` as shown:
+YAML::
++
+[source,yaml]
+----
+- routeTemplate:
+ id: "s3template"
+ parameters:
+ - name: "region"
+ - name: "bucket"
+ beans:
+ - name: "myClient"
+ scriptLanguage: "bean"
+ script: "com.foo.MyAwsHelper?method=createS3Client"
+ from:
+ uri: direct:s3-store
+ steps:
+ - to:
+ uri: "aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"
+----
+====
+
+The method signature of `createS3Client` method **MUST** then have one
parameter for the `RouteTemplateContext` as shown:
[source,java]
----
@@ -832,21 +1132,41 @@ public static S3Client
createS3Client(RouteTemplateContext rtc) {
If you are using pure Java code (both template and creating local bean),
then you can create the local bean using Java lambda style as previously
documented.
+
==== Configuring the type of the created bean
The `type` must be set to define what FQN class the created bean.
+In the following route template we want to tell Camel that the bean is a
`software.amazon.awssdk.services.s3.S3Client` type.
+
+
[tabs]
====
+
+Java::
++
+In Java DSL you can refer to the type via its class (ie `S3Client.java` which
is a type safe way:
++
+[source,java]
+----
+routeTemplate("s3template")
+ .templateParameter("region")
+ .templateParameter("bucket")
+ .templateBean("myClient", S3Client.class, "bean",
"com.foo.MyAwsHelper?method=createS3Client")
+ .from("direct:s3-store")
+ // must refer to the bean with {{myClient}}
+ .to("aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}")
+----
+
XML::
+
[source,xml]
----
-<camelContext>
<routeTemplate id="s3template">
<templateParameter name="region"/>
<templateParameter name="bucket"/>
- <templateBean name="myClient" scriptLanguage="bean"
type="software.amazon.awssdk.services.s3.S3Client">
+ <templateBean name="myClient" scriptLanguage="bean"
+ type="software.amazon.awssdk.services.s3.S3Client">
<script>com.foo.MyAwsHelper?method=createS3Client</script>
</templateBean>
<route>
@@ -854,27 +1174,39 @@ XML::
<to uri="aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"/>
</route>
</routeTemplate>
-</camelContext>
----
-Java::
+YAML::
+
-[source,java]
+[source,yaml]
----
-routeTemplate("s3template")
- .templateParameter("region")
- .templateParameter("bucket")
- .templateBean("myClient", S3Client.class, "bean",
"com.foo.MyAwsHelper?method=createS3Client")
- .from("direct:s3-store")
- // must refer to the bean with {{myClient}}
- .to("aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}")
+- routeTemplate:
+ id: "s3template"
+ parameters:
+ - name: "region"
+ - name: "bucket"
+ beans:
+ - name: "myClient"
+ scriptLanguage: "bean"
+ type: "software.amazon.awssdk.services.s3.S3Client"
+ script: "com.foo.MyAwsHelper?method=createS3Client"
+ from:
+ uri: direct:s3-store
+ steps:
+ - to:
+ uri: "aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"
----
====
-== Configuring route templates when creating route
+
+== Configuring route templates when creating route (advanced)
There may be some special situations where you want to be able to do some
custom configuration/code when
-a route is about to be created from a route template. To support this you can
use the `configure` in the route template DSL
+a route is about to be created from a route template.
+
+NOTE: This is only available in Java DSL
+
+To support this you can use the `configure` in the route template DSL
where you can specify the code to execute as show:
[source,java]
@@ -933,4 +1265,8 @@ To let Camel discover custom sources, then register the
source into the Camel re
== See Also
-See the example
https://github.com/apache/camel-examples/tree/main/routetemplate[camel-examples/examples/routetemplate/].
+See these examples:
+
+- https://github.com/apache/camel-examples/tree/main/routetemplate[Standalone
Route Template]
+-
https://github.com/apache/camel-spring-boot-examples/tree/main/routetemplate[Spring
Boot Route Template]
+-
https://github.com/apache/camel-spring-boot-examples/tree/main/routetemplate-xml[Spring
Boot Route Template XML DSL]