Skip to content

Commit fbd5513

Browse files
committed
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
1 parent 593c08e commit fbd5513

File tree

11 files changed

+286
-3
lines changed

11 files changed

+286
-3
lines changed

README.adoc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ readme's instructions.
2727
=== Examples
2828

2929
// examples: START
30-
Number of Examples: 63 (0 deprecated)
30+
Number of Examples: 64 (0 deprecated)
3131

3232
[width="100%",cols="4,2,4",options="header"]
3333
|===
@@ -37,6 +37,8 @@ Number of Examples: 63 (0 deprecated)
3737

3838
| link:endpointdsl/readme.adoc[Endpointdsl] (endpointdsl) | Beginner | Using type-safe Endpoint DSL
3939

40+
| link:groovy/README.adoc[Groovy] (groovy) | Beginner | An example for showing Camel Spring Boot using YAML with Groovy scripts
41+
4042
| link:kamelet-chucknorris/readme.adoc[Kamelet Chucknorris] (kamelet-chucknorris) | Beginner | How easy it is to create your own Kamelets
4143

4244
| link:pojo/README.adoc[Pojo] (pojo) | Beginner | An example showing how to work with Camel POJO routing with Spring Boot
@@ -96,6 +98,7 @@ Number of Examples: 63 (0 deprecated)
9698
| link:fhir/readme.adoc[Fhir] (fhir) | Health Care | An example showing how to work with Camel, FHIR and Spring Boot
9799

98100
| link:fhir-auth-tx/readme.adoc[Fhir Auth Tx] (fhir-auth-tx) | Health Care | An example showing how to work with Camel, FHIR Authorization, FHIR Transaction and Spring Boot
101+
99102

100103
| link:validator/readme.adoc[Validator Spring Boot] (validator) | Input/Output Type Contract | An example showing how to work with declarative validation and Spring Boot
101104

@@ -111,6 +114,7 @@ Number of Examples: 63 (0 deprecated)
111114
| link:metrics/README.adoc[Metrics] (metrics) | Management and Monitoring | An example showing how to work with Camel and Spring Boot and report metrics to Graphite
112115

113116
| link:observation/README.adoc[Micrometer Observation] (observation) | Management and Monitoring | An example showing how to trace incoming and outgoing messages from Camel with Micrometer Observation
117+
114118

115119
| link:opentelemetry/README.adoc[OpenTelemetry] (opentelemetry) | Management and Monitoring | An example showing how to use Camel with OpenTelemetry
116120

@@ -137,15 +141,16 @@ Number of Examples: 63 (0 deprecated)
137141
| link:widget-gadget/README.adoc[Widget Gadget] (widget-gadget) | Messaging | The widget and gadget example from EIP book, running on Spring Boot
138142

139143
| link:reactive-streams/readme.adoc[Reactive Streams] (reactive-streams) | Reactive | An example that shows how Camel can exchange data using reactive streams with Spring Boot reactor
144+
140145

141146
| link:http-ssl/README.adoc[Http Ssl] (http-ssl) | Rest | An example showing the Camel HTTP component with Spring Boot and SSL
142147

148+
| link:http-streaming/README.adoc[Http Streaming] (http-streaming) | Rest | An example showing large data stream scenario using Camel Platform HTTP component
149+
143150
| link:openapi-contract-first/readme.adoc[Openapi Contract First] (openapi-contract-first) | Rest | Contract First OpenAPI example
144151

145152
| link:platform-http/README.adoc[Platform Http] (platform-http) | Rest | An example showing Camel REST DSL with platform HTTP
146153

147-
| link:http-streaming/README.adoc[Http Streaming] (http-streaming) | Rest | This example shows how to stream large files using platform HTTP component
148-
149154
| link:rest-cxf/README.adoc[Rest Cxf] (rest-cxf) | Rest | An example showing Camel REST using CXF with Spring Boot
150155

151156
| link:rest-openapi/README.adoc[Rest Openapi] (rest-openapi) | Rest | An example showing Camel REST DSL and OpenApi with Spring Boot

groovy/README.adoc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
== Spring Boot Example with Yaml DSL and Groovy
2+
3+
=== Introduction
4+
5+
This example shows how to run Camel Spring Boot (YAML routes) with Groovy scripts
6+
for small functions and DTO classes.
7+
8+
The Groovy source is located in `src/main/resources/camel-groovy`, which
9+
are pre-compiled on startup.
10+
11+
This allows Camel routes to use the Groovy classes (as if they were Java classes).
12+
The route will then transform a JSon file into a DTO class using Jackson, and then
13+
enrich the DTO based on some business logic that are inlined Groovy script in the Camel route.
14+
15+
=== Build
16+
17+
You can build this example using:
18+
19+
$ mvn package
20+
21+
=== Run
22+
23+
You can run this example using:
24+
25+
$ mvn spring-boot:run
26+
27+
And you should see output in the console.
28+
29+
=== Help and contributions
30+
31+
If you hit any problem using Camel or have some feedback, then please
32+
https://camel.apache.org/community/support/[let us know].
33+
34+
We also love contributors, so
35+
https://camel.apache.org/community/contributing/[get involved] :-)
36+
37+
The Camel riders!

groovy/data/order.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": 432,
3+
"name": "Acme"
4+
}

groovy/pom.xml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.apache.camel.springboot.example</groupId>
26+
<artifactId>examples</artifactId>
27+
<version>4.14.0-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>camel-example-spring-boot-groovy</artifactId>
31+
<name>Camel SB Examples :: Groovy</name>
32+
<description>An example for showing Camel Spring Boot using YAML with Groovy scripts</description>
33+
34+
<properties>
35+
<category>Beginner</category>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
38+
</properties>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<!-- Camel BOM -->
43+
<dependency>
44+
<groupId>org.apache.camel.springboot</groupId>
45+
<artifactId>camel-spring-boot-bom</artifactId>
46+
<version>${project.version}</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
<!-- Spring Boot BOM -->
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-dependencies</artifactId>
54+
<version>${spring-boot-version}</version>
55+
<type>pom</type>
56+
<scope>import</scope>
57+
</dependency>
58+
</dependencies>
59+
</dependencyManagement>
60+
61+
<dependencies>
62+
63+
<!-- Spring Boot -->
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter</artifactId>
67+
</dependency>
68+
69+
<!-- Camel -->
70+
<dependency>
71+
<groupId>org.apache.camel.springboot</groupId>
72+
<artifactId>camel-spring-boot-starter</artifactId>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.apache.camel.springboot</groupId>
76+
<artifactId>camel-yaml-dsl-starter</artifactId>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.apache.camel.springboot</groupId>
80+
<artifactId>camel-groovy-starter</artifactId>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.apache.camel.springboot</groupId>
84+
<artifactId>camel-file-starter</artifactId>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.apache.camel.springboot</groupId>
88+
<artifactId>camel-jackson-starter</artifactId>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.apache.camel.springboot</groupId>
92+
<artifactId>camel-log-starter</artifactId>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.apache.camel.springboot</groupId>
96+
<artifactId>camel-timer-starter</artifactId>
97+
</dependency>
98+
99+
</dependencies>
100+
101+
<build>
102+
<plugins>
103+
<plugin>
104+
<groupId>org.springframework.boot</groupId>
105+
<artifactId>spring-boot-maven-plugin</artifactId>
106+
<version>${spring-boot-version}</version>
107+
<executions>
108+
<execution>
109+
<goals>
110+
<goal>repackage</goal>
111+
</goals>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package sample.camel;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
/**
23+
* A sample Spring Boot application that starts the Camel routes.
24+
*/
25+
@SpringBootApplication
26+
public class MyApplication {
27+
28+
/**
29+
* A main method to start this application.
30+
*/
31+
public static void main(String[] args) {
32+
SpringApplication.run(MyApplication.class, args);
33+
}
34+
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## ---------------------------------------------------------------------------
2+
## Licensed to the Apache Software Foundation (ASF) under one or more
3+
## contributor license agreements. See the NOTICE file distributed with
4+
## this work for additional information regarding copyright ownership.
5+
## The ASF licenses this file to You under the Apache License, Version 2.0
6+
## (the "License"); you may not use this file except in compliance with
7+
## the License. You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
## ---------------------------------------------------------------------------
17+
18+
# give Camel a name
19+
camel.main.name = GroovyExample
20+
21+
# keep camel running
22+
camel.main.run-controller = true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class OrderDTO {
2+
3+
int id
4+
String name
5+
String level;
6+
7+
String toString() {
8+
return "Order " + id + " from " + name + " (" + level + ")"
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class OrderService {
2+
static boolean isGold(dto) {
3+
return dto.id > 1000
4+
}
5+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
- route:
2+
from:
3+
uri: file
4+
parameters:
5+
directoryName: data
6+
noop: true
7+
steps:
8+
- unmarshal:
9+
json:
10+
library: Jackson
11+
unmarshalType: OrderDTO
12+
- setVariable:
13+
name: level
14+
expression:
15+
groovy:
16+
expression: |-
17+
gold = OrderService.isGold(body)
18+
return gold ? "Gold" : "Silver"
19+
- script:
20+
expression:
21+
groovy:
22+
expression: body.level = variable.level
23+
- log:
24+
message: ${body}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## ---------------------------------------------------------------------------
2+
## Licensed to the Apache Software Foundation (ASF) under one or more
3+
## contributor license agreements. See the NOTICE file distributed with
4+
## this work for additional information regarding copyright ownership.
5+
## The ASF licenses this file to You under the Apache License, Version 2.0
6+
## (the "License"); you may not use this file except in compliance with
7+
## the License. You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
## ---------------------------------------------------------------------------
17+
18+
appender.out.type = Console
19+
appender.out.name = out
20+
appender.out.layout.type = PatternLayout
21+
appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
22+
rootLogger.level = INFO
23+
rootLogger.appenderRef.out.ref = out

0 commit comments

Comments
 (0)