This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-16593 in repository https://gitbox.apache.org/repos/asf/camel.git
commit a0988b7e2e275ae99d80afe5442ac4a2889c2409 Author: Claus Ibsen <[email protected]> AuthorDate: Mon May 10 11:13:15 2021 +0200 CAMEL-16596: ScriptingLanguage SPI. --- .../modules/ROOT/pages/route-template.adoc | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/docs/user-manual/modules/ROOT/pages/route-template.adoc b/docs/user-manual/modules/ROOT/pages/route-template.adoc index 4a22706..1606390 100644 --- a/docs/user-manual/modules/ROOT/pages/route-template.adoc +++ b/docs/user-manual/modules/ROOT/pages/route-template.adoc @@ -215,6 +215,118 @@ You should prefer to create the local beans directly from within the template (i 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 scripting languages + +You can use scripting languages like groovy, joor, mvel which creates the bean. +This allows to define route templates with the scripting language built-in (such as groovy). + +For example creating the AWS S3 client can be done as shown in Java (with inlined groovy code): + +[source,java] +---- +routeTemplate("s3template") + .templateParameter("region") + .templateParameter("bucket") + .templateBean("myClient", "groovy", + "software.amazon.awssdk.services.s3.S3Client.S3Client.builder() + .region(rtc.getProperty("region", Region.class)) + .build()" + ) + .from("direct:s3-store") + // must refer to the bean with {{myClient}} + .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: + +[source,xml] +---- +<camelContext> + <routeTemplate id="s3template"> + <templateParameter name="region"/> + <templateParameter name="bucket"/> + <templateBean name="myClient"> + <templateBeanFactory language="groovy"> + import software.amazon.awssdk.services.s3.S3Client + S3Client.builder() + .region(rtc.getProperty("region", Region.class)) + .build() + </templateBeanFactory> + </templateBean> + <route> + <from uri="direct:s3-store"/> + <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: + +[source,xml] +---- +<camelContext> + <routeTemplate id="s3template"> + <templateParameter name="region"/> + <templateParameter name="bucket"/> + <templateBean name="myClient"> + <templateBeanFactory language="groovy"> + resource:classpath:s3bean.groovy + </templateBeanFactory> + </templateBean> + <route> + <from uri="direct:s3-store"/> + <to uri="aws2-s3:{{bucket}}?amazonS3Client=#{{myClient}}"/> + </route> + </routeTemplate> +</camelContext> +---- + +The languages supported are: + +[width="100%",cols="2s,8",options="header"] +|=== +| Language | Description +| bean | Calling a method on a Java class. +| groovy | Using groovy script to create the bean. +| joor | Using joor (Java code which are runtime compiled) to create the bean. +| language | To use a 3rd party language to create the bean. +| mvel | To use Mvel template script to create the bean. +| ognl | To use OGNL template script to create the bean. +|=== + +The most powerful languages to use are groovy and joor. The other languages are limited in flexibility +as they are not complete programming languages, but are more suited for templating needs. + +It is recommended to either use groovy or joor, if creating the local bean requires coding, +and the route templates are not defined using Java code. + +If you are using pure Java code, then you can create the local bean using Java lambda style as previously documented. + == Configuring route templates when creating route There may be some special situations where you want to be able to do some custom configuration/code when
