이 콘텐츠는 선택한 언어로 제공되지 않습니다.
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>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-groovy-starter</artifactId>
</dependency>
40.2. URI Format 링크 복사링크가 클립보드에 복사되었습니다!
The camel-groovy language component uses the following URI notation:
groovy("someGroovyExpression")
groovy("someGroovyExpression")
40.3. Groovy Options 링크 복사링크가 클립보드에 복사되었습니다!
The Groovy language supports 2 options, which are listed below.
| Name | Default | Java Type | Description |
|---|---|---|---|
| resultType |
| Sets the class of the result type (type from output). | |
| trim |
|
| 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")
from("queue:foo")
.filter(groovy("request.lineItems.any { i -> i.value > 100 }"))
.to("queue:bar")
- XML DSL
40.5. Groovy Context 링크 복사링크가 클립보드에 복사되었습니다!
Camel provides an exchange information in the Groovy context (just a Map). The Exchange is transferred as:
| key | value |
|---|---|
|
|
The |
|
|
The |
|
| The variables |
|
| The headers of the In message. |
|
| The Camel Context. |
|
|
The |
|
|
The |
|
|
The |
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
bar = "baz";
# some other statements ...
# camel take the result value as the script evaluation result
result = body * 2 + 1
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.
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"`,
`"resource:scheme:location"`,
For example, to refer to a file on the classpath you can use the following:
.setHeader("myHeader").groovy("resource:classpath:mygroovy.groovy")
.setHeader("myHeader").groovy("resource:classpath:mygroovy.groovy")
40.9. Spring Boot Auto-Configuration 링크 복사링크가 클립보드에 복사되었습니다!
The component supports 2 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
| 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 |