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 2020/11/26 08:46:46 UTC
[camel-examples] 01/02: CAMEL-15704: added 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 d2cbe21527bdbd411569c3c0682b7ff6185a245a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Nov 25 14:49:56 2020 +0100
CAMEL-15704: added example
---
examples/camel-example-csimple/pom.xml | 140 +++++++++++++++++++++
examples/camel-example-csimple/readme.adoc | 24 ++++
.../org/apache/camel/example/CSimpleScript1.java | 28 +++++
.../org/apache/camel/example/CSimpleScript2.java | 28 +++++
.../org/apache/camel/example/CSimpleScript3.java | 28 +++++
.../services/org/apache/camel/csimple.properties | 4 +
.../src/main/data/foo.properties | 18 +++
.../org/apache/camel/example/MyApplication.java | 38 ++++++
.../org/apache/camel/example/MyRouteBuilder.java | 35 ++++++
.../src/main/resources/application.properties | 33 +++++
.../src/main/resources/logback.xml | 30 +++++
examples/pom.xml | 1 +
12 files changed, 407 insertions(+)
diff --git a/examples/camel-example-csimple/pom.xml b/examples/camel-example-csimple/pom.xml
new file mode 100644
index 0000000..6cf168e
--- /dev/null
+++ b/examples/camel-example-csimple/pom.xml
@@ -0,0 +1,140 @@
+<?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.7.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-example-csimple</artifactId>
+ <packaging>jar</packaging>
+ <name>Camel :: Example :: Compiled Simple</name>
+ <description>Shows using compiled simple language</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-main</artifactId>
+ </dependency>
+ <!-- we use these camel components in this example -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-bean</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-timer</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-log</artifactId>
+ </dependency>
+
+ <!-- logging -->
+ <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>
+ <!-- to run the application -->
+ <plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-maven-plugin</artifactId>
+ <version>${camel.version}</version>
+ <configuration>
+ <mainClass>org.apache.camel.example.MyApplication</mainClass>
+ </configuration>
+ </plugin>
+ <!-- generate source code for csimple languages -->
+ <plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-csimple-maven-plugin</artifactId>
+ <version>${camel.version}</version>
+ <executions>
+ <execution>
+ <id>generate</id>
+ <goals>
+ <goal>generate</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- include source code generated to maven sources paths -->
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>3.1.0</version>
+ <executions>
+ <execution>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>add-source</goal>
+ <goal>add-resource</goal>
+ </goals>
+ <configuration>
+ <sources>
+ <source>src/generated/java</source>
+ </sources>
+ <resources>
+ <resource>
+ <directory>src/generated/resources</directory>
+ </resource>
+ </resources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git a/examples/camel-example-csimple/readme.adoc b/examples/camel-example-csimple/readme.adoc
new file mode 100644
index 0000000..3a59275
--- /dev/null
+++ b/examples/camel-example-csimple/readme.adoc
@@ -0,0 +1,24 @@
+== Camel Example Compiled Simple
+
+This example shows using csimple (compiled simple) scripting language in your Camel routes.
+
+A maven plugin (configured in pom.xml) detects the csimple scripts from the project source code and then generate Java source code in `src/generated/java` folder which then gets compiled with the regular Java compiler.
+
+This makes the csimple language native Java compiled, with no runtime overhead.
+The generated source code in `src/generated/java` is just regular Java source code and you can therefore debug the code. You can try this by loading the project into a Java IDE, and then launch the application in debug mode from the `MyApplication` class. You can then set breakpoints in the generated source code in the `src/generated/java` folder.
+
+=== 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-csimple/src/generated/java/org/apache/camel/example/CSimpleScript1.java b/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript1.java
new file mode 100644
index 0000000..3aa5a49
--- /dev/null
+++ b/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript1.java
@@ -0,0 +1,28 @@
+package org.apache.camel.example;
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.stream.*;
+
+import org.apache.camel.*;
+import org.apache.camel.util.*;
+import static org.apache.camel.language.csimple.CSimpleHelper.*;
+
+
+public class CSimpleScript1 extends org.apache.camel.language.csimple.CSimpleSupport {
+
+ public CSimpleScript1(CamelContext context) {
+ init(context);
+ }
+
+ @Override
+ public String getText() {
+ return "${random(20)}";
+ }
+
+ @Override
+ public Object evaluate(CamelContext context, Exchange exchange, Message message, Object body) throws Exception {
+ return random(exchange, 0, 20);
+ }
+}
+
diff --git a/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript2.java b/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript2.java
new file mode 100644
index 0000000..c14997d
--- /dev/null
+++ b/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript2.java
@@ -0,0 +1,28 @@
+package org.apache.camel.example;
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.stream.*;
+
+import org.apache.camel.*;
+import org.apache.camel.util.*;
+import static org.apache.camel.language.csimple.CSimpleHelper.*;
+
+
+public class CSimpleScript2 extends org.apache.camel.language.csimple.CSimpleSupport {
+
+ public CSimpleScript2(CamelContext context) {
+ init(context);
+ }
+
+ @Override
+ public String getText() {
+ return "${body} > 10";
+ }
+
+ @Override
+ public Object evaluate(CamelContext context, Exchange exchange, Message message, Object body) throws Exception {
+ return isGreaterThan(exchange, body, 10);
+ }
+}
+
diff --git a/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript3.java b/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript3.java
new file mode 100644
index 0000000..486692d
--- /dev/null
+++ b/examples/camel-example-csimple/src/generated/java/org/apache/camel/example/CSimpleScript3.java
@@ -0,0 +1,28 @@
+package org.apache.camel.example;
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.stream.*;
+
+import org.apache.camel.*;
+import org.apache.camel.util.*;
+import static org.apache.camel.language.csimple.CSimpleHelper.*;
+
+
+public class CSimpleScript3 extends org.apache.camel.language.csimple.CSimpleSupport {
+
+ public CSimpleScript3(CamelContext context) {
+ init(context);
+ }
+
+ @Override
+ public String getText() {
+ return "${body} > 5";
+ }
+
+ @Override
+ public Object evaluate(CamelContext context, Exchange exchange, Message message, Object body) throws Exception {
+ return isGreaterThan(exchange, body, 5);
+ }
+}
+
diff --git a/examples/camel-example-csimple/src/generated/resources/META-INF/services/org/apache/camel/csimple.properties b/examples/camel-example-csimple/src/generated/resources/META-INF/services/org/apache/camel/csimple.properties
new file mode 100644
index 0000000..f76904b
--- /dev/null
+++ b/examples/camel-example-csimple/src/generated/resources/META-INF/services/org/apache/camel/csimple.properties
@@ -0,0 +1,4 @@
+# Generated by camel build tools - do NOT edit this file!
+org.apache.camel.example.CSimpleScript1
+org.apache.camel.example.CSimpleScript2
+org.apache.camel.example.CSimpleScript3
diff --git a/examples/camel-example-csimple/src/main/data/foo.properties b/examples/camel-example-csimple/src/main/data/foo.properties
new file mode 100644
index 0000000..b43e6bc
--- /dev/null
+++ b/examples/camel-example-csimple/src/main/data/foo.properties
@@ -0,0 +1,18 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+bye = Bye
\ No newline at end of file
diff --git a/examples/camel-example-csimple/src/main/java/org/apache/camel/example/MyApplication.java b/examples/camel-example-csimple/src/main/java/org/apache/camel/example/MyApplication.java
new file mode 100644
index 0000000..496b37d
--- /dev/null
+++ b/examples/camel-example-csimple/src/main/java/org/apache/camel/example/MyApplication.java
@@ -0,0 +1,38 @@
+/*
+ * 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 final class MyApplication {
+
+ private MyApplication() {
+ }
+
+ public static void main(String[] args) throws Exception {
+ // use Camels Main class
+ Main main = new Main();
+ // and add the routes (you can specify multiple classes)
+ main.configure().addRoutesBuilder(MyRouteBuilder.class);
+ // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
+ main.run(args);
+ }
+
+}
diff --git a/examples/camel-example-csimple/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-csimple/src/main/java/org/apache/camel/example/MyRouteBuilder.java
new file mode 100644
index 0000000..dfff5fb
--- /dev/null
+++ b/examples/camel-example-csimple/src/main/java/org/apache/camel/example/MyRouteBuilder.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 org.apache.camel.example;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyRouteBuilder extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ from("timer:foo?period={{myPeriod}}")
+ .transform().csimple("${random(20)}")
+ .choice()
+ .when().csimple("${body} > 10")
+ .log("high ${body}")
+ .when().csimple("${body} > 5")
+ .log("med ${body}")
+ .otherwise()
+ .log("low ${body}");
+ }
+}
diff --git a/examples/camel-example-csimple/src/main/resources/application.properties b/examples/camel-example-csimple/src/main/resources/application.properties
new file mode 100644
index 0000000..87f73cf
--- /dev/null
+++ b/examples/camel-example-csimple/src/main/resources/application.properties
@@ -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.
+## ---------------------------------------------------------------------------
+
+# to configure camel main
+# here you can configure options on camel main (see MainConfigurationProperties class)
+camel.main.name = MyCSimpleCamel
+
+# enable tracing
+### camel.main.tracing = true
+
+# bean introspection to log reflection based configuration
+camel.main.beanIntrospectionExtendedStatistics=true
+camel.main.beanIntrospectionLoggingLevel=INFO
+
+# run in lightweight mode to be tiny as possible
+camel.main.lightweight = true
+
+# properties used in the route
+myPeriod = 1000
diff --git a/examples/camel-example-csimple/src/main/resources/logback.xml b/examples/camel-example-csimple/src/main/resources/logback.xml
new file mode 100644
index 0000000..a798d0b
--- /dev/null
+++ b/examples/camel-example-csimple/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/pom.xml b/examples/pom.xml
index 684818e..ff7c85d 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -107,6 +107,7 @@
<module>camel-example-cxf-proxy</module>
<module>camel-example-cxf-tomcat</module>
<module>camel-example-console</module>
+ <module>camel-example-csimple</module>
<module>camel-example-debezium</module>
<module>camel-example-debezium-eventhubs-blob</module>
<module>camel-example-ftp</module>