blob: 435519cea521f225d5b2b1ed5a23b5977d3a4b75 [file]
= 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:
*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:
[tabs]
====
Java::
+
[source,java]
----
from("direct:hello")
.to("language:simple:Hello ${body}");
----
XML::
+
[source,xml]
----
<route>
<from uri="direct:hello"/>
<to uri="language:simple:Hello ${body}"/>
</route>
----
YAML::
+
[source,yaml]
----
- route:
from:
uri: direct:hello
steps:
- to:
uri: "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]:
[tabs]
====
Java::
+
[source,java]
----
from("direct:toString")
.to("language:simple:${bodyAs(String.class)}");
----
XML::
+
[source,xml]
----
<route>
<from uri="direct:toString"/>
<to uri="language:simple:${bodyAs(String.class)}"/>
</route>
----
YAML::
+
[source,yaml]
----
- route:
from:
uri: direct:toString
steps:
- to:
uri: "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:
[tabs]
====
Java::
+
[source,java]
----
from("direct:double")
.to("language:groovy:${body} * 2}");
----
XML::
+
[source,xml]
----
<route>
<from uri="direct:double"/>
<to uri="language:groovy:${body} * 2}"/>
</route>
----
YAML::
+
[source,yaml]
----
- route:
from:
uri: direct:double
steps:
- to:
uri: "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.
._Java-only: providing the script as a message header_
[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:`
[tabs]
====
Java::
+
[source,java]
----
from("direct:start")
.to("language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt")
.to("mock:result");
----
XML::
+
[source,xml]
----
<route>
<from uri="direct:start"/>
<to uri="language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt"/>
<to uri="mock:result"/>
</route>
----
YAML::
+
[source,yaml]
----
- route:
from:
uri: direct:start
steps:
- to:
uri: language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt
- to:
uri: 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:
[tabs]
====
Java::
+
[source,java]
----
from("direct:start")
.to("language:simple:myscript.txt?contentCache=false")
.to("mock:result");
----
XML::
+
[source,xml]
----
<route>
<from uri="direct:start"/>
<to uri="language:simple:myscript.txt?contentCache=false"/>
<to uri="mock:result"/>
</route>
----
YAML::
+
[source,yaml]
----
- route:
from:
uri: direct:start
steps:
- to:
uri: language:simple:myscript.txt
parameters:
contentCache: false
- to:
uri: 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:
[tabs]
====
Java::
+
[source,java]
----
from("direct:start")
.to("language:constant:resource:classpath:org/apache/camel/component/language/hello.txt")
.to("mock:result");
----
XML::
+
[source,xml]
----
<route>
<from uri="direct:start"/>
<to uri="language:constant:resource:classpath:org/apache/camel/component/language/hello.txt"/>
<to uri="mock:result"/>
</route>
----
YAML::
+
[source,yaml]
----
- route:
from:
uri: direct:start
steps:
- to:
uri: language:constant:resource:classpath:org/apache/camel/component/language/hello.txt
- to:
uri: mock:result
----
====