blob: c674bcd1ff9b906f62aafaed50621d79fa6ece84 [file] [log] [blame]
[[language-component]]
= Language Component
:page-source: components/camel-language/src/main/docs/language-component.adoc
*Since Camel 2.5*
// HEADER START
*Only producer is supported*
// HEADER END
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:manual::dynamic-router.adoc[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
xref:groovy-language.adoc[Groovy] or xref:groovy-language.adoc[JavaScript] languages.
== URI format
[source]
----
language://languageName[:script][?options]
----
You can refer to an external resource for
the script using same notation as supported by the other
xref:language-component.adoc[Language]s in Camel
[source]
----
language://languageName:resource:scheme:location][?options]
----
== URI Options
// component options: START
The Language component supports 3 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
|===
// component options: END
// endpoint options: START
The Language endpoint is configured using URI syntax:
----
language:languageName:resourceUri
----
with the following path and query parameters:
=== Path Parameters (2 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *languageName* | *Required* Sets the name of the language to use | | String
| *resourceUri* | Path to the resource, or a reference to lookup a bean in the Registry to use as the resource | | String
|===
=== Query Parameters (8 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *binary* (producer) | Whether the script is binary content or text content. By default the script is read as text content (eg java.lang.String) | false | boolean
| *cacheScript* (producer) | 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. | false | boolean
| *contentCache* (producer) | Sets whether to use resource content cache or not | true | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean
| *script* (producer) | Sets the script to execute | | String
| *transform* (producer) | Whether or not the result of the script should be used as message body. This options is default true. | true | boolean
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
// spring-boot-auto-configure options: START
== Spring Boot Auto-Configuration
When using Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
[source,xml]
----
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-language-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
----
The component supports 4 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *camel.component.language.basic-property-binding* | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | Boolean
| *camel.component.language.bridge-error-handler* | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | Boolean
| *camel.component.language.enabled* | Whether to enable auto configuration of the language component. This is enabled by default. | | Boolean
| *camel.component.language.lazy-start-producer* | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | Boolean
|===
// spring-boot-auto-configure options: END
== Message Headers
The following message headers can be used to affect the behavior of the
component
[width="100%",cols="10%,90%",options="header",]
|===
|Header |Description
|`CamelLanguageScript` |The script to execute provided in the header. Takes precedence over
script configured on the endpoint.
|===
== Examples
For example you can use the xref:manual::simple-language.adoc[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 xref:groovy-language.adoc[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
xref: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
*Since 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:
You can refer to the resource similar to the
other xref:language-component.adoc[Language]s in Camel by prefixing with
`"resource:"` as shown below: