Rechercher

Ce contenu n'est pas disponible dans la langue sélectionnée.

Chapter 82. Language

download PDF

Only producer is supported

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.

82.1. Dependencies

When using language with Red Hat build of Camel Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-language-starter</artifactId>
</dependency>

82.2. URI format

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

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]

82.3. Configuring Options

Camel components are configured on two levels:

  • Component level
  • Endpoint level

82.3.1. Component Level Options

The component level is the highest level. The configurations you define at this level are inherited by all the endpoints. For example, a component can have security settings, credentials for authentication, urls for network connection, and so on.

Since components typically have pre-configured defaults for the most common cases, you may need to only configure a few component options, or maybe none at all.

You can configure components with Component DSL in a configuration file (application.properties|yaml), or directly with Java code.

82.3.2. Endpoint Level Options

At the Endpoint level you have many options, which you can use to configure what you want the endpoint to do. The options are categorized according to whether the endpoint is used as a consumer (from) or as a producer (to) or used for both.

You can configure endpoints directly in the endpoint URI as path and query parameters. You can also use Endpoint DSL and DataFormat DSL as type safe ways of configuring endpoints and data formats in Java.

When configuring options, use Property Placeholders for urls, port numbers, sensitive information, and other settings.

Placeholders allows you to externalize the configuration from your code, giving you more flexible and reusable code.

82.4. Component Options

The Language component supports 2 options, which are listed below.

NameDescriptionDefaultType

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

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

82.5. Endpoint Options

The Language endpoint is configured using URI syntax:

language:languageName:resourceUri

with the following path and query parameters:

82.5.1. Path Parameters (2 parameters)

NameDescriptionDefaultType

languageName (producer)

Required Sets the name of the language to use.

Enum values:

  • bean
  • constant
  • exchangeProperty
  • file
  • groovy
  • header
  • javascript
  • jsonpath
  • mvel
  • ognl
  • ref
  • simple
  • spel
  • sql
  • terser
  • tokenize
  • xpath
  • xquery
  • xtokenize
 

String

resourceUri (producer)

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

 

String

82.5.2. Query Parameters (7 parameters)

NameDescriptionDefaultType

allowContextMapAll (producer)

Sets whether the context map should allow access to all details. By default only the message body and headers can be accessed. This option can be enabled for full access to the current Exchange and CamelContext. Doing so impose a potential security risk as this opens access to the full power of CamelContext API.

false

boolean

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

82.6. 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.

82.7. Examples

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

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);

82.8. 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:

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 Languages in Camel by prefixing with "resource:" as shown below.

82.9. Spring Boot Auto-Configuration

The component supports 3 options, which are listed below.

NameDescriptionDefaultType

camel.component.language.autowired-enabled

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

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

Red Hat logoGithubRedditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez leBlog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

© 2024 Red Hat, Inc.