From 0dc370e8c3532a65607e46393b69b709cfed2304 Mon Sep 17 00:00:00 2001 From: JiriOndrusek Date: Thu, 4 Jan 2024 15:41:59 +0100 Subject: [PATCH] Variables example --- docs/modules/ROOT/attachments/examples.json | 5 + variables/README.adoc | 66 ++++ variables/eclipse-formatter-config.xml | 276 ++++++++++++++++ variables/pom.xml | 299 ++++++++++++++++++ .../main/java/org/acme/main/TimerRoute.java | 38 +++ .../src/main/resources/application.properties | 34 ++ .../java/org/acme/main/TimerLogMainIT.java | 24 ++ .../java/org/acme/main/VariablesTest.java | 50 +++ 8 files changed, 792 insertions(+) create mode 100644 variables/README.adoc create mode 100644 variables/eclipse-formatter-config.xml create mode 100644 variables/pom.xml create mode 100644 variables/src/main/java/org/acme/main/TimerRoute.java create mode 100644 variables/src/main/resources/application.properties create mode 100644 variables/src/test/java/org/acme/main/TimerLogMainIT.java create mode 100644 variables/src/test/java/org/acme/main/VariablesTest.java diff --git a/docs/modules/ROOT/attachments/examples.json b/docs/modules/ROOT/attachments/examples.json index d2e7c049..95da3285 100644 --- a/docs/modules/ROOT/attachments/examples.json +++ b/docs/modules/ROOT/attachments/examples.json @@ -98,5 +98,10 @@ "title": "Tokenize a CSV file", "description": "Shows how to define a Camel route in XML for tokenizing a CSV a file.", "link": "https://github.com/apache/camel-quarkus-examples/tree/main/file-split-log-xml" + }, + { + "title": "Variables", + "description": "Uses the Camel variables which can be configured from application.properties.", + "link": "https://github.com/apache/camel-quarkus-examples/tree/main/variables" } ] \ No newline at end of file diff --git a/variables/README.adoc b/variables/README.adoc new file mode 100644 index 00000000..35f6c82c --- /dev/null +++ b/variables/README.adoc @@ -0,0 +1,66 @@ += Variables: A Camel Quarkus example +:cq-example-description: An example that uses the Camel variables which can be configured from application.properties. + +{cq-description} + +TIP: Check the https://camel.apache.org/camel-quarkus/latest/first-steps.html[Camel Quarkus User guide] for prerequisites +and other general information. + +== Start in the Development mode + +[source,shell] +---- +$ mvn clean compile quarkus:dev +---- + +The above command compiles the project, starts the application and lets the Quarkus tooling watch for changes in your +workspace. Any modifications in your project will automatically take effect in the running application. + +TIP: Please refer to the Development mode section of +https://camel.apache.org/camel-quarkus/latest/first-steps.html#_development_mode[Camel Quarkus User guide] for more details. + +Then look at the log output in the console. There are log messages generated from a second route. First several message show random number `N/A` (as is defined in application.properties). Following messages show random number generated by the first route, which starts delayed. + +As we run the example in Quarkus Dev Mode, you can edit the source code and have live updates. +For example, try to change the default greeting message in `application.properties` to `Greeting`. + +The delay of the random member generation and period at which the timer fires can be changed by modifying configuration properties `timer.period` and +`timer.delay` in `application.properties`. You can also override the default values via the command line with JVM arguments +`-Dtimer.period=100 -Dtimer.delay=1000' + +=== Package and run the application + +Once you are done with developing you may want to package and run the application. + +TIP: Find more details about the JVM mode and Native mode in the Package and run section of +https://camel.apache.org/camel-quarkus/latest/first-steps.html#_package_and_run_the_application[Camel Quarkus User guide] + +==== JVM mode + +[source,shell] +---- +$ mvn clean package +$ java -jar target/quarkus-app/quarkus-run.jar +... +[io.quarkus] (main) camel-quarkus-examples-... started in 1.163s. +---- + +==== Native mode + +IMPORTANT: Native mode requires having GraalVM and other tools installed. Please check the Prerequisites section +of https://camel.apache.org/camel-quarkus/latest/first-steps.html#_prerequisites[Camel Quarkus User guide]. + +To prepare a native executable using GraalVM, run the following command: + +[source,shell] +---- +$ mvn clean package -Pnative +$ ./target/*-runner +... +[io.quarkus] (main) camel-quarkus-examples-... started in 0.013s. +... +---- + +== Feedback + +Please report bugs and propose improvements via https://github.com/apache/camel-quarkus/issues[GitHub issues of Camel Quarkus] project. diff --git a/variables/eclipse-formatter-config.xml b/variables/eclipse-formatter-config.xml new file mode 100644 index 00000000..2248b2b8 --- /dev/null +++ b/variables/eclipse-formatter-config.xml @@ -0,0 +1,276 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/variables/pom.xml b/variables/pom.xml new file mode 100644 index 00000000..d965790b --- /dev/null +++ b/variables/pom.xml @@ -0,0 +1,299 @@ + + + + 4.0.0 + + camel-quarkus-examples-variables + org.apache.camel.quarkus.examples + 3.7.0-SNAPSHOT + + Camel Quarkus :: Examples :: Variables + Camel Quarkus Example :: Variables + + + 3.9.0 + 3.10.0-SNAPSHOT + + + ${camel-quarkus.platform.version} + + io.quarkus + quarkus-bom + org.apache.camel.quarkus + camel-quarkus-bom + + UTF-8 + UTF-8 + 17 + 17 + ${maven.compiler.target} + ${maven.compiler.source} + + 2.23.0 + 1.9.0 + 4.2 + 3.11.0 + 3.3.0 + 3.3.1 + 3.1.2 + + ${project.build.directory}/quarkus-app/quarkus-run.jar + + + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + ${camel-quarkus.platform.group-id} + ${camel-quarkus.platform.artifact-id} + ${camel-quarkus.platform.version} + pom + import + + + org.apache.camel.quarkus + camel-quarkus-bom-test + ${camel-quarkus.version} + pom + import + + + + + + + org.apache.camel.quarkus + camel-quarkus-log + + + org.apache.camel.quarkus + camel-quarkus-timer + + + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + org.apache.camel.quarkus + camel-quarkus-integration-tests-process-executor-support + ${camel-quarkus.version} + test + + + org.awaitility + awaitility + test + + + + + + + + + net.revelc.code.formatter + formatter-maven-plugin + ${formatter-maven-plugin.version} + + ${maven.multiModuleProjectDirectory}/eclipse-formatter-config.xml + LF + + + + + net.revelc.code + impsort-maven-plugin + ${impsort-maven-plugin.version} + + java.,javax.,org.w3c.,org.xml.,junit. + true + true + java.,javax.,org.w3c.,org.xml.,junit. + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + true + true + + -Xlint:unchecked + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + false + + org.jboss.logmanager.LogManager + + + + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus.platform.version} + + + + org.apache.maven.plugins + maven-failsafe-plugin + ${maven-surefire-plugin.version} + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + + com.mycila + license-maven-plugin + ${license-maven-plugin.version} + + true +
${maven.multiModuleProjectDirectory}/header.txt
+ + **/*.adoc + **/*.txt + **/LICENSE.txt + **/LICENSE + **/NOTICE.txt + **/NOTICE + **/README + **/pom.xml.versionsBackup + + + SLASHSTAR_STYLE + CAMEL_PROPERTIES_STYLE + SLASHSTAR_STYLE + + + ${maven.multiModuleProjectDirectory}/license-properties-headerdefinition.xml + +
+
+
+
+ + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + + + build + + build + + + + + + + net.revelc.code.formatter + formatter-maven-plugin + + + format + + format + + process-sources + + + + + + net.revelc.code + impsort-maven-plugin + + + sort-imports + + sort + + process-sources + + + + +
+ + + + native + + + native + + + + native + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + integration-test + verify + + + + ${quarkus.package.type} + + + + + + + + + + +
diff --git a/variables/src/main/java/org/acme/main/TimerRoute.java b/variables/src/main/java/org/acme/main/TimerRoute.java new file mode 100644 index 00000000..92537c17 --- /dev/null +++ b/variables/src/main/java/org/acme/main/TimerRoute.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.acme.main; + +import jakarta.enterprise.context.ApplicationScoped; +import org.apache.camel.builder.RouteBuilder; + +/** + * A simple {@link RouteBuilder}. + */ +@ApplicationScoped +public class TimerRoute extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("timer:random?period={{timer.period:1000}}").startupOrder(1) + .setBody(simple("${variable.global:greeting} ${variable.global:fromApp}: ${variable.global:random}")) + .log("${body}"); + + from("timer:java?delay={{timer.delay:5000}}").startupOrder(2) + .setVariable("global:fromApp", simple("from ${camelId}")) + .setVariable("global:random", simple("${random(1,10)}")); + } +} diff --git a/variables/src/main/resources/application.properties b/variables/src/main/resources/application.properties new file mode 100644 index 00000000..9e5172be --- /dev/null +++ b/variables/src/main/resources/application.properties @@ -0,0 +1,34 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +# +# Quarkus +# +quarkus.banner.enabled = false +quarkus.log.file.enable = true + +# +# Camel +# +camel.context.name = quarkus-camel-example-variables + +# +# variables can be pre-configured on startup +# +camel.variable.greeting = Greeting + +camel.variable.random = N/A diff --git a/variables/src/test/java/org/acme/main/TimerLogMainIT.java b/variables/src/test/java/org/acme/main/TimerLogMainIT.java new file mode 100644 index 00000000..49f309bc --- /dev/null +++ b/variables/src/test/java/org/acme/main/TimerLogMainIT.java @@ -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. + */ +package org.acme.main; + +import io.quarkus.test.junit.QuarkusIntegrationTest; + +@QuarkusIntegrationTest +class TimerLogMainIT extends VariablesTest { + +} diff --git a/variables/src/test/java/org/acme/main/VariablesTest.java b/variables/src/test/java/org/acme/main/VariablesTest.java new file mode 100644 index 00000000..e861f65f --- /dev/null +++ b/variables/src/test/java/org/acme/main/VariablesTest.java @@ -0,0 +1,50 @@ +/* + * 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.acme.main; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; + +import io.quarkus.test.junit.QuarkusTest; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.junit.jupiter.api.Test; + +import static org.awaitility.Awaitility.await; + +@QuarkusTest +public class VariablesTest { + + @Test + public void testTimerLog() { + Config config = ConfigProvider.getConfig(); + String greeting = config.getValue("camel.variable.greeting", String.class); + String contextName = config.getValue("camel.context.name", String.class); + String initRandom = config.getValue("camel.variable.random", String.class); + + Pattern p = Pattern.compile(String.format(".* %s from %s: \\d.*", greeting, contextName), Pattern.DOTALL); + + await().atMost(10L, TimeUnit.SECONDS).pollDelay(1, TimeUnit.SECONDS).until(() -> { + String log = new String(Files.readAllBytes(Paths.get("target/quarkus.log")), StandardCharsets.UTF_8); + return log.contains(String.format("%s : %s", greeting, initRandom)) && + p.matcher(log).matches(); + }); + } +}