이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 40. Groovy


Since Camel 1.3

Camel has support for using Groovy. For example, you can use Groovy in a Predicate with the Message Filter EIP.

40.1. Dependencies

When using camel-groovy with Red Hat build of Camel Spring Boot, add the following Maven dependency to your pom.xml to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-groovy-starter</artifactId>
</dependency>
Copy to Clipboard Toggle word wrap

40.2. URI Format

The camel-groovy language component uses the following URI notation:

groovy("someGroovyExpression")
Copy to Clipboard Toggle word wrap

40.3. Groovy Options

The Groovy language supports 2 options, which are listed below.

Expand
NameDefaultJava TypeDescription

resultType

 

String

Sets the class of the result type (type from output).

trim

true

Boolean

Whether to trim the value to remove leading and trailing whitespaces and line breaks.

40.4. Examples

Following example uses a groovy script as predicate in the message filter, to determine if any line items are over $100:

Java
from("queue:foo")
    .filter(groovy("request.lineItems.any { i -> i.value > 100 }"))
        .to("queue:bar")
Copy to Clipboard Toggle word wrap
XML DSL
<route>
    <from uri="queue:foo"/>
    <filter>
        <groovy>request.lineItems.any { i -> i.value > 100 }</groovy>
        <to uri="queue:bar"/>
    </filter>
</route>
Copy to Clipboard Toggle word wrap

40.5. Groovy Context

Camel provides an exchange information in the Groovy context (just a Map). The Exchange is transferred as:

Expand
keyvalue

exchange

The Exchange itself.

exchangeProperties

The Exchange properties.

variables

The variables

headers

The headers of the In message.

camelContext

The Camel Context.

request

The In message.

body

The In message body.

response

The Out message (only for InOut message exchange pattern).

40.6. How to get the result from multiple statements script

As the Groovy script engine evaluate method returns a Null if it runs a multiple statements script. Camel looks up the value of script result by using the key of result from the value set. If you have multiple statements scripts, make sure to set the value of result variable as the script return value.

bar = "baz";
# some other statements ...
# camel take the result value as the script evaluation result
result = body * 2 + 1
Copy to Clipboard Toggle word wrap

40.7. Customizing Groovy Shell

For very special use cases you may need to use a custom GroovyShell instance in your Groovy expressions. To provide the custom GroovyShell, add an implementation of the org.apache.camel.language.groovy.GroovyShellFactory SPI interface to the Camel registry.

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

}
Copy to Clipboard Toggle word wrap

Camel will then use your custom GroovyShell instance (containing your custom static imports), instead of the default one.

40.8. Loading script from external resource

You can externalize the script and have Camel load it from a resource such as "classpath:", "file:", or "http:". You can achieve this by using the following syntax:

`"resource:scheme:location"`,
Copy to Clipboard Toggle word wrap

For example, to refer to a file on the classpath you can use the following:

.setHeader("myHeader").groovy("resource:classpath:mygroovy.groovy")
Copy to Clipboard Toggle word wrap

40.9. Spring Boot Auto-Configuration

The component supports 2 options, which are listed below.

Expand
NameDescriptionDefaultType

camel.language.groovy.enabled

Whether to enable auto configuration of the groovy language. This is enabled by default.

 

Boolean

camel.language.groovy.trim

Whether to trim the value to remove leading and trailing whitespaces and line breaks.

true

Boolean

Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat
맨 위로 이동