Language Component

Table of Contents

URI format
URI Options
Message Headers
Examples
Loading scripts from resources
See Also

Available as of Camel 2.5

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 Dynamic Router EIPs you can send messages to language endpoints where the script is dynamic defined as well.

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

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

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

URI Options

The Language component has no options.

The Language component supports 8 endpoint options which are listed below:

{% raw %}

NameGroupDefaultJava TypeDescription

languageName

producer

 

String

Required Sets the name of the language to use

resourceUri

producer

 

String

Path to the resource or a reference to lookup a bean in the Registry to use as the resource

binary

producer

false

boolean

Whether the script is binary content or text content. By default the script is read as text content (eg java.lang.String)

cacheScript

producer

false

boolean

Whether to cache the compiled script and reuse Notice reusing the script can cause side effects from processing one Camel org.apache.camel.Exchange to the next org.apache.camel.Exchange.

contentCache

producer

false

boolean

Sets whether to use resource content cache or not.

script

producer

 

String

Sets the script to execute

transform

producer

true

boolean

Whether or not the result of the script should be used as message body. This options is default true.

synchronous

advanced

false

boolean

Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported).

{% endraw %}

Message Headers

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

HeaderDescription

CamelLanguageScript

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

Examples

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

In case you want to convert the message body type you can do this as well:

You can also use the Groovy language, such as this example where the input message will by multiplied with 2:

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.

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

Loading scripts from resources

Available as of Camel 2.9

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:

For example to load a script from the classpath:

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:

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:

See Also