| = Language Component |
| :doctitle: Language |
| :shortname: language |
| :artifactid: camel-language |
| :description: Execute scripts in any of the languages supported by Camel. |
| :since: 2.5 |
| :supportlevel: Stable |
| :tabs-sync-option: |
| :component-header: Only producer is supported |
| :core: |
| //Manually maintained attributes |
| :camel-spring-boot-name: language |
| |
| *Since Camel {since}* |
| |
| *{component-header}* |
| |
| The Language component allows you to send `Exchange` |
| to an endpoint which executes a script by any of the supported |
| Languages in Camel. |
| |
| By having a component to execute language scripts, it allows more |
| dynamic routing capabilities. |
| For example, by using the Routing Slip or xref:eips:dynamicRouter-eip.adoc[Dynamic Router] EIPs |
| you can send messages to `language` endpoints where the |
| script is dynamically defined as well. |
| |
| You only have to include additional Camel |
| components if the language of choice mandates it, such as using |
| xref:languages:groovy-language.adoc[Groovy] or xref:languages:groovy-language.adoc[JavaScript] languages. |
| |
| == URI format |
| |
| ---- |
| language://languageName[:script][?options] |
| ---- |
| |
| You can refer to an external resource for |
| the script using the same notation as supported by the other |
| xref:language-component.adoc[Language]s in Camel |
| |
| ---- |
| language://languageName:resource:scheme:location][?options] |
| ---- |
| |
| // component options: START |
| include::partial$component-configure-options.adoc[] |
| include::partial$component-endpoint-options.adoc[] |
| include::partial$component-endpoint-headers.adoc[] |
| // component options: END |
| |
| == Examples |
| |
| For example, you can use the xref:languages:simple-language.adoc[Simple] language as |
| xref:eips:message-translator.adoc[Message Translator] EIP: |
| |
| [source,java] |
| ---- |
| from("direct:hello") |
| .to("language:simple:Hello ${body}") |
| ---- |
| |
| In case you want to convert the message body type, you can do this as |
| well. However, it is better to use xref:eips:convertBodyTo-eip.adoc[Convert Body To]: |
| |
| [source,java] |
| ---- |
| from("direct:toString") |
| .to("language:simple:${bodyAs(String.class)}") |
| ---- |
| |
| You can also use the xref:languages:groovy-language.adoc[Groovy] language, such as this |
| example where the input message will be multiplied with 2: |
| |
| [source,groovy] |
| ---- |
| from("direct:double") |
| .to("language:groovy:${body} * 2}") |
| ---- |
| |
| You can also provide the script as a header as shown below. Here we use |
| xref:languages:xpath-language.adoc[XPath] language to extract the text from the `<foo>` |
| tag. |
| |
| [source,java] |
| ---- |
| Object out = producer.requestBodyAndHeader("language:xpath", "<foo>Hello World</foo>", Exchange.LANGUAGE_SCRIPT, "/foo/text()"); |
| assertEquals("Hello World", out); |
| ---- |
| |
| == Loading scripts from resources |
| |
| You can specify a resource uri for a script to load in either the |
| endpoint uri, or in the `Exchange.LANGUAGE_SCRIPT` header. |
| The uri must start with one of the following schemes: `file:`, `classpath:`, or `http:` |
| |
| [source,java] |
| ---- |
| from("direct:start") |
| // load the script from the classpath |
| .to("language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt") |
| .to("mock:result"); |
| ---- |
| |
| By default, the script is loaded once and cached. However, you can disable |
| the `contentCache` option and have the script loaded on each |
| evaluation. For example, if the file `myscript.txt` is changed on disk, then the |
| updated script is used: |
| |
| [source,java] |
| ---- |
| from("direct:start") |
| // the script will be loaded on each message, as we disabled cache |
| .to("language:simple:myscript.txt?contentCache=false") |
| .to("mock:result"); |
| ---- |
| |
| You can also refer to the script as a resource similar to how all the |
| other xref:language-component.adoc[Language]s in Camel functions, by prefixing with |
| `resource:` as shown below: |
| |
| [source,java] |
| ---- |
| from("direct:start") |
| .to("language:constant:resource:classpath:org/apache/camel/component/language/hello.txt") |
| .to("mock:result"); |
| ---- |
| |
| include::spring-boot:partial$starter.adoc[] |