You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/02/06 15:29:30 UTC

[camel-examples] 01/02: add routes loader java example

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

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

commit d1b8cfcee5c2ac057d02feb0dc2e6c70fb68635d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Feb 6 15:54:06 2021 +0100

    add routes loader java example
---
 examples/camel-example-routeloader/pom.xml         | 114 +++++++++++++++++++++
 examples/camel-example-routeloader/readme.adoc     |  29 ++++++
 .../org/apache/camel/example/MyApplication.java    |  39 +++++++
 .../src/main/resources/application.properties      |  24 +++++
 .../src/main/resources/logback.xml                 |  30 ++++++
 .../main/resources/myroutes/MyRouteBuilder.java    |  17 +++
 examples/pom.xml                                   |   1 +
 7 files changed, 254 insertions(+)

diff --git a/examples/camel-example-routeloader/pom.xml b/examples/camel-example-routeloader/pom.xml
new file mode 100644
index 0000000..9d612bd
--- /dev/null
+++ b/examples/camel-example-routeloader/pom.xml
@@ -0,0 +1,114 @@
+<?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>examples</artifactId>
+        <version>3.8.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-routeloader</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Example :: Route Loader</name>
+    <description>Example loading and compiling Java routes on startup</description>
+
+    <properties>
+        <category>Beginner</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>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-main</artifactId>
+        </dependency>
+        <!-- joor with java route loader -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-joor</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-timer</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-log</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-bean</artifactId>
+        </dependency>
+
+        <!-- logging -->
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <version>${log4j2-version}</version>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-core</artifactId>
+            <version>${logback-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>${logback-version}</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-maven-plugin</artifactId>
+                <version>${camel.version}</version>
+                <configuration>
+                    <logClasspath>false</logClasspath>
+                    <mainClass>org.apache.camel.main.Main</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/examples/camel-example-routeloader/readme.adoc b/examples/camel-example-routeloader/readme.adoc
new file mode 100644
index 0000000..c62aed1
--- /dev/null
+++ b/examples/camel-example-routeloader/readme.adoc
@@ -0,0 +1,29 @@
+== Camel Example Route Loader with Java
+
+This example shows how Camel is capable of loading routes during startup using the new route loader system.
+The route loader has support for loading routes in XML and Java (other languages to be added).
+
+This is the feature that was first developed as part of Camel K which has evolved to be a core
+part of Camel.
+
+The Java route loader is using `camel-joor` component which has the support for dynamic compiling Java source.
+
+The example has a Java route in the `src/main/resources/myroutes` directory. This route is a java file,
+but the file is not part of the regular source code and is therefore not compiled with the regular Java compiler.
+The route is loaded by Camels route loader and dynamic compiled via `camel-joor` during startup of Camel.
+
+=== How to run
+
+You can run this example using
+
+    mvn camel:run
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/support.html[let us know].
+
+We also love contributors, so
+https://camel.apache.org/contributing.html[get involved] :-)
+
+The Camel riders!
diff --git a/examples/camel-example-routeloader/src/main/java/org/apache/camel/example/MyApplication.java b/examples/camel-example-routeloader/src/main/java/org/apache/camel/example/MyApplication.java
new file mode 100644
index 0000000..6de6397
--- /dev/null
+++ b/examples/camel-example-routeloader/src/main/java/org/apache/camel/example/MyApplication.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ *
+ * This class has been added to make it easy to run the application from a Java editor.
+ * However you can start this application by running org.apache.camel.main.Main directory (see Maven pom.xml)
+ */
+public final class MyApplication {
+
+    private MyApplication() {
+    }
+
+    public static void main(String[] args) throws Exception {
+        // use Camels Main class
+        Main main = new Main();
+        // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
+        main.run(args);
+    }
+
+}
diff --git a/examples/camel-example-routeloader/src/main/resources/application.properties b/examples/camel-example-routeloader/src/main/resources/application.properties
new file mode 100644
index 0000000..6e2e9cf
--- /dev/null
+++ b/examples/camel-example-routeloader/src/main/resources/application.properties
@@ -0,0 +1,24 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+# to configure camel main
+# here you can configure options on camel main (see MainConfigurationProperties class)
+camel.main.name = MyJavaLoader
+
+# which directory(s) to scan for routes which can be xml or java files
+camel.main.routes-include-pattern=classpath:myroutes/*.java
+
diff --git a/examples/camel-example-routeloader/src/main/resources/logback.xml b/examples/camel-example-routeloader/src/main/resources/logback.xml
new file mode 100644
index 0000000..a798d0b
--- /dev/null
+++ b/examples/camel-example-routeloader/src/main/resources/logback.xml
@@ -0,0 +1,30 @@
+<?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.
+
+-->
+<configuration>
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+
+    <root level="INFO">
+        <appender-ref ref="STDOUT" />
+    </root>
+</configuration>
diff --git a/examples/camel-example-routeloader/src/main/resources/myroutes/MyRouteBuilder.java b/examples/camel-example-routeloader/src/main/resources/myroutes/MyRouteBuilder.java
new file mode 100644
index 0000000..b4d1666
--- /dev/null
+++ b/examples/camel-example-routeloader/src/main/resources/myroutes/MyRouteBuilder.java
@@ -0,0 +1,17 @@
+import java.util.Random;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("timer:foo?period=2s")
+            .setBody(method(MyRouteBuilder.class, "randomNumber"))
+            .log("Random number ${body}");
+    }
+
+    public static int randomNumber() {
+        return new Random().nextInt(100);
+    }
+}
diff --git a/examples/pom.xml b/examples/pom.xml
index c7558a5..48e0056 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -144,6 +144,7 @@
         <module>camel-example-pojo-messaging</module>
         <module>camel-example-reactive-executor-vertx</module>
         <module>camel-example-route-throttling</module>
+        <module>camel-example-routeloader</module>
         <module>camel-example-routetemplate</module>
         <module>camel-example-salesforce-consumer</module>
         <module>camel-example-servlet-tomcat</module>