Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

This component is provided out of the box in camel-core and hence no additional JARs is needed. You only have to include additional Camel components if the language of choice mandates it, such as using Groovy or JavaScript languages.

URI format

Code Block

language://languageName[:script][?options]

And from Camel 2.11 onwards you can refer to an external resource for the script using same notation as supported by the other Languages in Camel

Code Block

language://languageName:resource:scheme:location][?options]

...

The component supports the following options.

Div
classconfluenceTableSmall

Name

Default Value

Type

Description

languageName

null

String

The name of the Language to use, such as simple, groovy, javascript etc. This option is mandatory.

script

null

String

The script to execute.

transform

true

boolean

Whether or not the result of the script should be used as the new message body. By setting to false the script is executed but the result of the script is discarded.

contentCache

true

boolean

Camel 2.9: Whether to cache the script if loaded from a resource.
Note: from Camel 2.10.3 a cached script can be forced to reload at runtime via JMX using the clearContentCache operation.

cacheScript

false

boolean

Camel

Wiki Markup
{div:class=confluenceTableSmall} || Name || Default Value || Type || Description || | {{languageName}} | {{null}} | {{String}} | The name of the [Language|Languages] to use, such as {{simple}}, {{groovy}}, {{javascript}} etc. This option is mandatory. | | {{script}} | {{null}} | {{String}} | The script to execute. | | {{transform}} | {{true}} | {{boolean}} | Whether or not the result of the script should be used as the new message body. By setting to {{false}} the script is executed but the result of the script is discarded. | | {{contentCache}} | {{true}} | {{boolean}} | *Camel 2.9:* Whether to cache the script if loaded from a resource. Note: from *Camel 2.10.3* a cached script can be forced to reload at runtime via JMX using the clearContentCache operation. | | {{cacheScript}} | {{false}} | {{boolean}} | *Camel

2.13/2.12.2/2.11.3:

*

Whether

to

cache

the

compiled

script.

Turning

this

option

on

can

gain

performance

as

the

script

is

only

compiled/created

once,

and

reuse

when

processing

Camel

messages.

But

this

may

cause

side-effects

with

data

left

from

previous

evaluation

spills

into

the

next,

and

concurrency

issues

as

well.

If

the

script

being

evaluated

is

idempotent

then

this

option

can

be

turned

on. | {div}

on.

binary

false

boolean

Camel 2.14.1: Whether the script is binary content. This is intended to be used for loading resources using the Constant language, such as loading binary files.

Message Headers

The following message headers can be used to affect the behavior of the component

Div
classconfluenceTableSmall

Header

Description

CamelLanguageScript

The script to execute provided in the header. Takes precedence over script configured on the endpoint.

Wiki Markup
{div:class=confluenceTableSmall} || Header || Description || | {{CamelLanguageScript}} | The script to execute provided in the header. Takes precedence over script configured on the endpoint. | {div}

Examples

For example you can use the Simple language to Message Translator a message:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageRouteTest.java}
In case you want to convert the message body type you can do this as well:
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageRouteConvertBodyTest.java}
You can also use the Groovy language, such as this example where the input message will by multiplied with 2:
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/LanguageGroovyRouteTest.java}
You can also provide the script as a header as shown below. Here we use XPath language to extract the text from the <foo> tag.

Code Block
java
java

Object out = producer.requestBodyAndHeader("language:xpath", "<foo>Hello World</foo>", Exchange.LANGUAGE_SCRIPT, "/foo/text()");
assertEquals("Hello World", out);

...

For example to load a script from the classpath:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageLoadScriptFromClasspathTest.java}
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:
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageLoadScriptFromFileUpdateTest.java}
From Camel 2.11 onwards you can refer to the resource similar to the other Languages in Camel by prefixing with "resource:" as shown below:
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageResourceLoadScriptFromClasspathTest.java}
Include Page
Endpoint See Also
Endpoint See Also