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

Chapter 17. Groovy


Overview

Groovy is a Java-based scripting language that allows quick parsing of object. The Groovy support is part of the camel-groovy module.

Adding the script module

To use Groovy in your routes you need to add a dependency on camel-groovy to your project as shown in Example 17.1, “Adding the camel-groovy dependency”.

Example 17.1. Adding the camel-groovy dependency

<!-- Maven POM File -->
<properties>
  <camel-version>2.17.0.redhat-630xxx</camel-version>
  ...
</properties>

<dependencies>
  ...
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-groovy</artifactId>
    <version>${camel-version}</version>
  </dependency>
</dependencies>

Static import

To use the groovy() static method in your application code, include the following import statement in your Java source files:
import static org.apache.camel.builder.script.ScriptBuilder.*;

Built-in attributes

Table 17.1, “Groovy attributes” lists the built-in attributes that are accessible when using Groovy.
Table 17.1. Groovy attributes
AttributeTypeValue
contextorg.apache.camel.CamelContextThe Camel Context
exchangeorg.apache.camel.ExchangeThe current Exchange
requestorg.apache.camel.MessageThe IN message
responseorg.apache.camel.MessageThe OUT message
propertiesorg.apache.camel.builder.script.PropertiesFunctionFunction with a resolve method to make it easier to use the properties component inside scripts.
The attributes all set at ENGINE_SCOPE.

Example

Example 17.2, “Routes using Groovy” shows two routes that use Groovy scripts.

Example 17.2. Routes using Groovy

<camelContext>
  <route>
    <from uri="direct:items" />
    <filter>
      <language language="groovy">request.lineItems.any { i -> i.value > 100 }</language>
      <to uri="mock:mock1" />
    </filter>
  </route>
  <route>
    <from uri="direct:in"/>
    <setHeader headerName="firstName">
      <language language="groovy">$user.firstName $user.lastName</language>
    </setHeader>
    <to uri="seda:users"/>
  </route>
</camelContext>

Using the properties component

To access a property value from the properties component, invoke the resolve method on the built-in properties attribute, as follows:
.setHeader("myHeader").groovy("properties.resolve(PropKey)")
Where PropKey is the key of the property you want to resolve, where the key value is of String type.
For more details about the properties component, see Properties in the Apache Camel Component Reference Guide.

Customizing Groovy Shell

Sometimes, you might need to use the custom GroovyShell instance, in your Groovy expressions. To provide custom GroovyShell, add an implementation of the org.apache.camel.language.groovy.GroovyShellFactory SPI interface to your Camel registry.
For example, when you add the following bean to your Spring context, Apache Camel will use the custom GroovyShell instance that includes the custom static imports, instead of the default one.
public class CustomGroovyShellFactory implements GroovyShellFactory {
 
  public GroovyShell createGroovyShell(Exchange exchange) {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStaticStars("com.example.Utils");
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    return new GroovyShell(configuration);
  }
 }

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.