Language
Available as of Camel
...
2.5
...
The
...
language
...
component
...
allows
...
you
...
to
...
send
...
...
to
...
an
...
endpoint
...
which
...
executes
...
a
...
script
...
by
...
any
...
of
...
the
...
supported
...
...
in
...
Camel.
...
By
...
having
...
a
...
component
...
to
...
execute
...
language
...
scripts,
...
it
...
allows
...
more
...
dynamic
...
routing
...
capabilities.
...
For
...
example
...
by
...
using
...
the
...
...
...
or
...
...
...
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
...
...
or
...
...
languages.
...
URI
...
format
Code Block |
---|
{code} language://languageName[:script][?options] {code} h3. URI Options The component supports the following options. {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. | {div} h3. Message Headers The following message headers can be used to affect the behavior of the component {div:class=confluenceTableSmall} || Header || Description || | {{CamelLanguageScript}} | The script to execute provided in the header. Takes precedence over script configured on the endpoint. | {div} h3. Examples For example you can use the [Simple] language to [Message Translator] a message: |
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]
|
URI Options
The component supports the following options.
Div | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||
|
Message Headers
The following message headers can be used to affect the behavior of the component
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} |
...
...
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} |
...
...
can
...
also
...
use
...
the
...
...
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} |
...
...
can
...
also
...
provide
...
the
...
script
...
as
...
a
...
header
...
as
...
shown
...
below.
...
Here
...
we
...
use
...
...
language
...
to
...
extract
...
the
...
text
...
from
...
the
...
<foo>
...
tag.
Code Block | ||||
---|---|---|---|---|
| ||||
{code:java} Object out = producer.requestBodyAndHeader("language:xpath", "<foo>Hello World</foo>", Exchange.LANGUAGE_SCRIPT, "/foo/text()"); assertEquals("Hello World", out); {code} h3. 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 {{ |
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:
...
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageLoadScriptFromClasspathTest.java} |
...
...
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} |
...
"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 | ||||
---|---|---|---|---|
|