This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot-examples.git


The following commit(s) were added to refs/heads/main by this push:
     new fbd5513  CAMEL-22214: camel-groovy - Allow to pre-load groovy source 
files for shared functions and DTOs
fbd5513 is described below

commit fbd551353e74e56acf85c617d636dc930c545a63
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Jul 15 13:31:30 2025 +0200

    CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for 
shared functions and DTOs
---
 README.adoc                                        |  11 +-
 groovy/README.adoc                                 |  37 +++++++
 groovy/data/order.json                             |   4 +
 groovy/pom.xml                                     | 117 +++++++++++++++++++++
 .../src/main/java/sample/camel/MyApplication.java  |  35 ++++++
 groovy/src/main/resources/application.properties   |  22 ++++
 .../main/resources/camel-groovy/OrderDTO.groovy    |  10 ++
 .../resources/camel-groovy/OrderService.groovy     |   5 +
 .../src/main/resources/camel/my-route.camel.yaml   |  24 +++++
 groovy/src/main/resources/log4j2.properties        |  23 ++++
 pom.xml                                            |   1 +
 11 files changed, 286 insertions(+), 3 deletions(-)

diff --git a/README.adoc b/README.adoc
index eacf1fa..bf279ec 100644
--- a/README.adoc
+++ b/README.adoc
@@ -27,7 +27,7 @@ readme's instructions.
 === Examples
 
 // examples: START
-Number of Examples: 63 (0 deprecated)
+Number of Examples: 64 (0 deprecated)
 
 [width="100%",cols="4,2,4",options="header"]
 |===
@@ -37,6 +37,8 @@ Number of Examples: 63 (0 deprecated)
 
 | link:endpointdsl/readme.adoc[Endpointdsl] (endpointdsl) | Beginner | Using 
type-safe Endpoint DSL
 
+| link:groovy/README.adoc[Groovy] (groovy) | Beginner | An example for showing 
Camel Spring Boot using YAML with Groovy scripts
+
 | link:kamelet-chucknorris/readme.adoc[Kamelet Chucknorris] 
(kamelet-chucknorris) | Beginner | How easy it is to create your own Kamelets
 
 | 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)
 | link:fhir/readme.adoc[Fhir] (fhir) | Health Care | An example showing how to 
work with Camel, FHIR and Spring Boot
 
 | 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
+    
 
 | link:validator/readme.adoc[Validator Spring Boot] (validator) | Input/Output 
Type Contract | An example showing how to work with declarative validation and 
Spring Boot
 
@@ -111,6 +114,7 @@ Number of Examples: 63 (0 deprecated)
 | 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
 
 | 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
+    
 
 | link:opentelemetry/README.adoc[OpenTelemetry] (opentelemetry) | Management 
and Monitoring | An example showing how to use Camel with OpenTelemetry
 
@@ -137,15 +141,16 @@ Number of Examples: 63 (0 deprecated)
 | link:widget-gadget/README.adoc[Widget Gadget] (widget-gadget) | Messaging | 
The widget and gadget example from EIP book, running on Spring Boot
 
 | 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
+    
 
 | link:http-ssl/README.adoc[Http Ssl] (http-ssl) | Rest | An example showing 
the Camel HTTP component with Spring Boot and SSL
 
+| link:http-streaming/README.adoc[Http Streaming] (http-streaming) | Rest | An 
example showing large data stream scenario using Camel Platform HTTP component
+
 | link:openapi-contract-first/readme.adoc[Openapi Contract First] 
(openapi-contract-first) | Rest | Contract First OpenAPI example
 
 | link:platform-http/README.adoc[Platform Http] (platform-http) | Rest | An 
example showing Camel REST DSL with platform HTTP
 
-| link:http-streaming/README.adoc[Http Streaming] (http-streaming) | Rest | 
This example shows how to stream large files using platform HTTP component
-
 | link:rest-cxf/README.adoc[Rest Cxf] (rest-cxf) | Rest | An example showing 
Camel REST using CXF with Spring Boot
 
 | link:rest-openapi/README.adoc[Rest Openapi] (rest-openapi) | Rest | An 
example showing Camel REST DSL and OpenApi with Spring Boot
diff --git a/groovy/README.adoc b/groovy/README.adoc
new file mode 100644
index 0000000..ded73db
--- /dev/null
+++ b/groovy/README.adoc
@@ -0,0 +1,37 @@
+== Spring Boot Example with Yaml DSL and Groovy
+
+=== Introduction
+
+This example shows how to run Camel Spring Boot (YAML routes) with Groovy 
scripts
+for small functions and DTO classes.
+
+The Groovy source is located in `src/main/resources/camel-groovy`, which
+are pre-compiled on startup.
+
+This allows Camel routes to use the Groovy classes (as if they were Java 
classes).
+The route will then transform a JSon file into a DTO class using Jackson, and 
then
+enrich the DTO based on some business logic that are inlined Groovy script in 
the Camel route.
+
+=== Build
+
+You can build this example using:
+
+    $ mvn package
+
+=== Run
+
+You can run this example using:
+
+    $ mvn spring-boot:run
+
+And you should see output in the console.
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/groovy/data/order.json b/groovy/data/order.json
new file mode 100644
index 0000000..c66544b
--- /dev/null
+++ b/groovy/data/order.json
@@ -0,0 +1,4 @@
+{
+  "id": 432,
+  "name": "Acme"
+}
\ No newline at end of file
diff --git a/groovy/pom.xml b/groovy/pom.xml
new file mode 100644
index 0000000..7028f68
--- /dev/null
+++ b/groovy/pom.xml
@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<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";>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel.springboot.example</groupId>
+        <artifactId>examples</artifactId>
+        <version>4.14.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-spring-boot-groovy</artifactId>
+    <name>Camel SB Examples :: Groovy</name>
+    <description>An example for showing Camel Spring Boot using YAML with 
Groovy scripts</description>
+
+    <properties>
+        <category>Beginner</category>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel.springboot</groupId>
+                <artifactId>camel-spring-boot-bom</artifactId>
+                <version>${project.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+            <!-- Spring Boot BOM -->
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring-boot-version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+
+        <!-- Spring Boot -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+
+        <!-- Camel -->
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-yaml-dsl-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-groovy-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-file-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-jackson-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-log-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-timer-starter</artifactId>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring-boot-version}</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/groovy/src/main/java/sample/camel/MyApplication.java 
b/groovy/src/main/java/sample/camel/MyApplication.java
new file mode 100644
index 0000000..eacdf24
--- /dev/null
+++ b/groovy/src/main/java/sample/camel/MyApplication.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sample.camel;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * A sample Spring Boot application that starts the Camel routes.
+ */
+@SpringBootApplication
+public class MyApplication {
+
+    /**
+     * A main method to start this application.
+     */
+    public static void main(String[] args) {
+        SpringApplication.run(MyApplication.class, args);
+    }
+
+}
diff --git a/groovy/src/main/resources/application.properties 
b/groovy/src/main/resources/application.properties
new file mode 100644
index 0000000..4483727
--- /dev/null
+++ b/groovy/src/main/resources/application.properties
@@ -0,0 +1,22 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+
+# give Camel a name
+camel.main.name = GroovyExample
+
+# keep camel running
+camel.main.run-controller = true
diff --git a/groovy/src/main/resources/camel-groovy/OrderDTO.groovy 
b/groovy/src/main/resources/camel-groovy/OrderDTO.groovy
new file mode 100644
index 0000000..a3a371e
--- /dev/null
+++ b/groovy/src/main/resources/camel-groovy/OrderDTO.groovy
@@ -0,0 +1,10 @@
+class OrderDTO {
+
+    int id
+    String name
+    String level;
+
+    String toString() {
+        return "Order " + id + " from " + name + " (" + level + ")"
+    }
+}
diff --git a/groovy/src/main/resources/camel-groovy/OrderService.groovy 
b/groovy/src/main/resources/camel-groovy/OrderService.groovy
new file mode 100644
index 0000000..7eed31a
--- /dev/null
+++ b/groovy/src/main/resources/camel-groovy/OrderService.groovy
@@ -0,0 +1,5 @@
+class OrderService {
+    static boolean isGold(dto) {
+        return dto.id > 1000
+    }
+}
diff --git a/groovy/src/main/resources/camel/my-route.camel.yaml 
b/groovy/src/main/resources/camel/my-route.camel.yaml
new file mode 100644
index 0000000..42d7a91
--- /dev/null
+++ b/groovy/src/main/resources/camel/my-route.camel.yaml
@@ -0,0 +1,24 @@
+- route:
+    from:
+      uri: file
+      parameters:
+        directoryName: data
+        noop: true
+      steps:
+        - unmarshal:
+            json:
+              library: Jackson
+              unmarshalType: OrderDTO
+        - setVariable:
+            name: level
+            expression:
+              groovy:
+                expression: |-
+                  gold = OrderService.isGold(body)
+                  return gold ? "Gold" : "Silver"
+        - script:
+            expression:
+              groovy:
+                expression: body.level = variable.level
+        - log:
+            message: ${body}
diff --git a/groovy/src/main/resources/log4j2.properties 
b/groovy/src/main/resources/log4j2.properties
new file mode 100644
index 0000000..d9f0508
--- /dev/null
+++ b/groovy/src/main/resources/log4j2.properties
@@ -0,0 +1,23 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+
+appender.out.type = Console
+appender.out.name = out
+appender.out.layout.type = PatternLayout
+appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
+rootLogger.level = INFO
+rootLogger.appenderRef.out.ref = out
diff --git a/pom.xml b/pom.xml
index 1329ee9..a209f56 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,6 +45,7 @@
                <module>aws-secrets-manager</module>
                <module>aws2-s3</module>
                <module>endpointdsl</module>
+               <module>groovy</module>
                <module>fhir</module>
                <module>fhir-auth-tx</module>
                <module>health-checks</module>

Reply via email to