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-examples.git


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

commit 7099e66fc178bad2b98f2e6156ed2ec25fda9a09
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Jul 14 16:57:32 2025 +0200

    CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for 
shared functions and DTOs
---
 README.adoc                                        |   4 +-
 main-groovy/README.adoc                            |  55 ++++++++
 main-groovy/data/order.json                        |   4 +
 main-groovy/pom.xml                                | 139 +++++++++++++++++++++
 .../org/apache/camel/example/MyApplication.java    |  33 +++++
 .../main/resources/camel-groovy/OrderDTO.groovy    |  10 ++
 .../resources/camel-groovy/OrderService.groovy     |   5 +
 .../src/main/resources/camel/my-route.camel.yaml   |  24 ++++
 main-groovy/src/main/resources/log4j2.properties   |  23 ++++
 9 files changed, 296 insertions(+), 1 deletion(-)

diff --git a/README.adoc b/README.adoc
index 8e2826c4..bc1af871 100644
--- a/README.adoc
+++ b/README.adoc
@@ -28,7 +28,7 @@ readme's instructions.
 == Examples
 
 // examples: START
-Number of Examples: 72 (0 deprecated)
+Number of Examples: 73 (0 deprecated)
 
 [width="100%",cols="4,2,4",options="header"]
 |===
@@ -70,6 +70,8 @@ Number of Examples: 72 (0 deprecated)
 
 | link:main-endpointdsl/README.adoc[Main Endpointdsl] (main-endpointdsl) | 
Camel Standalone | An example for showing standalone Camel with Endpoint DSL
 
+| link:main-groovy/README.adoc[Main Groovy] (main-groovy) | Camel Standalone | 
An example for showing standalone Camel using YAML with Groovy scripts
+
 | link:main-joor/README.adoc[Main Joor] (main-joor) | Camel Standalone | An 
example for showing standalone Camel with jOOR runtime Java compiled
 
 | link:main-lambda/README.adoc[Main Lambda] (main-lambda) | Camel Standalone | 
Camel routes lambda style
diff --git a/main-groovy/README.adoc b/main-groovy/README.adoc
new file mode 100644
index 00000000..c5939869
--- /dev/null
+++ b/main-groovy/README.adoc
@@ -0,0 +1,55 @@
+== Camel Example Main Groovy
+
+This example shows how to run Camel standalone (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
+
+First compile the example by executing:
+
+[source,sh]
+----
+$ mvn compile
+----
+
+=== How to run
+
+You can run this example using
+
+[source,sh]
+----
+$ mvn camel:run
+----
+
+=== How to configure for Camel Textual Route debugging
+
+Several IDEs are providing support for Camel Textual Route debugging. To 
enable this possibility, you need to launch this example with the profile 
`camel.debug`.
+
+[source,sh]
+----
+$ mvn camel:run -Pcamel.debug
+----
+
+This profile can also be activated with camel.debug property set to true. For 
instance, by setting the property from command-line:
+
+[source,sh]
+----
+$ mvn camel:run -Dcamel.debug=true
+----
+
+=== 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/main-groovy/data/order.json b/main-groovy/data/order.json
new file mode 100644
index 00000000..c66544bb
--- /dev/null
+++ b/main-groovy/data/order.json
@@ -0,0 +1,4 @@
+{
+  "id": 432,
+  "name": "Acme"
+}
\ No newline at end of file
diff --git a/main-groovy/pom.xml b/main-groovy/pom.xml
new file mode 100644
index 00000000..b7a547ed
--- /dev/null
+++ b/main-groovy/pom.xml
@@ -0,0 +1,139 @@
+<?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.example</groupId>
+        <artifactId>camel-examples</artifactId>
+        <version>4.14.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-main-groovy</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Example :: Main :: Groovy</name>
+    <description>An example for showing standalone Camel using YAML with 
Groovy scripts</description>
+
+    <properties>
+        <category>Camel Standalone</category>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Add Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-bom</artifactId>
+                <version>${camel.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+
+        <!-- use camel-main to bootstrap camel -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-main</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-yaml-dsl</artifactId>
+        </dependency>
+
+        <!-- and we use these components -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-groovy</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-file</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jackson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-log</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-timer</artifactId>
+        </dependency>
+
+        <!-- logging -->
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j2-impl</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>runtime</scope>
+        </dependency>
+        <!-- for testing -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-test-main-junit5</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-maven-plugin</artifactId>
+                <version>${camel.version}</version>
+                <configuration>
+                    <!-- use built-in logging, instead of logback -->
+                    <logLevel>INFO</logLevel>
+                    
<mainClass>org.apache.camel.example.MyApplication</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    
+    <profiles>
+        <profile>
+            <id>camel.debug</id>
+            <activation>
+                <property>
+                    <name>camel.debug</name>
+                    <value>true</value>
+                </property>
+            </activation>
+            <dependencies>
+                <dependency>
+                    <groupId>org.apache.camel</groupId>
+                    <artifactId>camel-debug</artifactId>
+                </dependency>
+            </dependencies>
+        </profile>
+    </profiles>
+</project>
diff --git 
a/main-groovy/src/main/java/org/apache/camel/example/MyApplication.java 
b/main-groovy/src/main/java/org/apache/camel/example/MyApplication.java
new file mode 100644
index 00000000..28cdcddf
--- /dev/null
+++ b/main-groovy/src/main/java/org/apache/camel/example/MyApplication.java
@@ -0,0 +1,33 @@
+/*
+ * 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 org.apache.camel.example;
+
+import org.apache.camel.main.Main;
+
+/**
+ * Main class that boot the Camel application
+ */
+public class MyApplication {
+
+    public static void main(String[] args) throws Exception {
+        // use Camels Main class
+        Main main = new Main(MyApplication.class);
+        // now keep the application running until the JVM is terminated (ctrl 
+ c or sigterm)
+        main.run(args);
+    }
+
+}
diff --git a/main-groovy/src/main/resources/camel-groovy/OrderDTO.groovy 
b/main-groovy/src/main/resources/camel-groovy/OrderDTO.groovy
new file mode 100644
index 00000000..a3a371ed
--- /dev/null
+++ b/main-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/main-groovy/src/main/resources/camel-groovy/OrderService.groovy 
b/main-groovy/src/main/resources/camel-groovy/OrderService.groovy
new file mode 100644
index 00000000..7eed31a4
--- /dev/null
+++ b/main-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/main-groovy/src/main/resources/camel/my-route.camel.yaml 
b/main-groovy/src/main/resources/camel/my-route.camel.yaml
new file mode 100644
index 00000000..42d7a914
--- /dev/null
+++ b/main-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/main-groovy/src/main/resources/log4j2.properties 
b/main-groovy/src/main/resources/log4j2.properties
new file mode 100644
index 00000000..d9f0508a
--- /dev/null
+++ b/main-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

Reply via email to