This is an automated email from the ASF dual-hosted git repository. ppalaga pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus-examples.git
commit f408ed143399c9d4a4f8416c36cd27a24727ed09 Author: MarĂa Arias de Reyna <dela...@gmail.com> AuthorDate: Fri Oct 1 11:07:10 2021 +0200 example(jdbc-datastore): #3056 Adding new simple example * example(jdbc-datastore): #3056 Adding new simple example * Removing JPA and fixing docs * Adding a new group for jdbc-datasource * Adding missing license header * Updating docs * Re-introducing quarkus-health microprofile * Fix README numbering and remove dangling files Co-authored-by: James Netherton <jamesnether...@gmail.com> --- .github/test-categories.yaml | 2 + docs/modules/ROOT/attachments/examples.json | 5 + jdbc-datasource/README.adoc | 69 ++++++ jdbc-datasource/pom.xml | 271 +++++++++++++++++++++ .../quarkus/component/jdbc/CamelResource.java | 56 +++++ .../src/main/resources/application.properties | 42 ++++ .../src/main/resources/routes/camel-routes.xml | 44 ++++ .../quarkus/component/jdbc/JdbcDataSourceIT.java | 24 ++ .../quarkus/component/jdbc/JdbcDataSourceTest.java | 47 ++++ 9 files changed, 560 insertions(+) diff --git a/.github/test-categories.yaml b/.github/test-categories.yaml index 00279ed..bdaec59 100644 --- a/.github/test-categories.yaml +++ b/.github/test-categories.yaml @@ -39,3 +39,5 @@ group-06: group-07: - kafka - kamelet-chucknorris +group-08: + - jdbc-datasource diff --git a/docs/modules/ROOT/attachments/examples.json b/docs/modules/ROOT/attachments/examples.json index a5ec778..5116648 100644 --- a/docs/modules/ROOT/attachments/examples.json +++ b/docs/modules/ROOT/attachments/examples.json @@ -5,6 +5,11 @@ "link": "https://github.com/apache/camel-quarkus-examples/tree/main/timer-log-cdi" }, { + "title": "Connecting to a JDBC DataSource", + "description": "Shows how to connect to a Database using Datastores.", + "link": "https://github.com/apache/camel-quarkus-examples/tree/main/jdbc-datasource" + }, + { "title": "Custom `main()`", "description": "Shows how to start Camel from a custom `main()` method", "link": "https://github.com/apache/camel-quarkus-examples/tree/main/timer-log-main" diff --git a/jdbc-datasource/README.adoc b/jdbc-datasource/README.adoc new file mode 100644 index 0000000..d3506a4 --- /dev/null +++ b/jdbc-datasource/README.adoc @@ -0,0 +1,69 @@ += Connecting to a JDBC DataSource: A Camel Quarkus example +:cq-example-description: An example that shows how to connect to a Database using Datastores. + +{cq-description} + +In particular, it demonstrates the following: + +1. Defining a DataSource +2. Querying the Database defined in the previous DataSource +3. Usage of properties defined in `application.properties` +4. No Java code required or used, the route defined in XML can still be compiled to native code. + +This example will connect to an H2 database with the connection details defined in `application.properties`. +If the example is run on Development mode and no database exists, Quarkus will create a matching database +https://quarkus.io/guides/datasource#dev-services[as described here]. + +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. + +=== 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 0.570s. +---- + +==== 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.011s. +... +---- + +== Feedback + +Please report bugs and propose improvements via https://github.com/apache/camel-quarkus/issues[GitHub issues of Camel Quarkus] project. diff --git a/jdbc-datasource/pom.xml b/jdbc-datasource/pom.xml new file mode 100644 index 0000000..42971e7 --- /dev/null +++ b/jdbc-datasource/pom.xml @@ -0,0 +1,271 @@ +<?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 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.camel.quarkus.examples</groupId> + <artifactId>camel-quarkus-examples-jdbc-datasource</artifactId> + <version>2.3.0-SNAPSHOT</version> + <name>Camel Quarkus :: Examples :: Jdbc - DatataSource - Log</name> + <description>Camel Quarkus Example :: Connect to Database using Datasource</description> + <properties> + <camel-quarkus.version>2.2.0</camel-quarkus.version> + <formatter-maven-plugin.version>2.11.0</formatter-maven-plugin.version> + <impsort-maven-plugin.version>1.3.2</impsort-maven-plugin.version> + <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version> + <maven-jar-plugin.version>3.2.0</maven-jar-plugin.version> + <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version> + <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> + <maven.compiler.source>11</maven.compiler.source> + <maven.compiler.target>11</maven.compiler.target> + <maven.compiler.testSource>${maven.compiler.source}</maven.compiler.testSource> + <maven.compiler.testTarget>${maven.compiler.target}</maven.compiler.testTarget> + <mycila-license.version>3.0</mycila-license.version> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <quarkus.version>2.2.0.Final</quarkus.version> + </properties> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-bom</artifactId> + <version>${camel-quarkus.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-bom-test</artifactId> + <version>${camel-quarkus.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-microprofile-health</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-xml-io-dsl</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-log</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-timer</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-jdbc</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-jdbc-h2</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-agroal</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-junit5</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-test-h2</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.awaitility</groupId> + <artifactId>awaitility</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>net.revelc.code.formatter</groupId> + <artifactId>formatter-maven-plugin</artifactId> + <version>${formatter-maven-plugin.version}</version> + <configuration> + <configFile>${maven.multiModuleProjectDirectory}/eclipse-formatter-config.xml</configFile> + </configuration> + </plugin> + <plugin> + <groupId>net.revelc.code</groupId> + <artifactId>impsort-maven-plugin</artifactId> + <version>${impsort-maven-plugin.version}</version> + <configuration> + <groups>java.,javax.,org.w3c.,org.xml.,junit.</groups> + <removeUnused>true</removeUnused> + <staticAfter>true</staticAfter> + <staticGroups>java.,javax.,org.w3c.,org.xml.,junit.</staticGroups> + </configuration> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>${maven-compiler-plugin.version}</version> + <configuration> + <showDeprecation>true</showDeprecation> + <showWarnings>true</showWarnings> + <compilerArgs> + <arg>-Xlint:unchecked</arg> + </compilerArgs> + </configuration> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>${maven-surefire-plugin.version}</version> + <configuration> + <failIfNoTests>false</failIfNoTests> + <systemProperties> + <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> + </systemProperties> + </configuration> + </plugin> + <plugin> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-maven-plugin</artifactId> + <version>${quarkus.version}</version> + </plugin> + <plugin> + <artifactId>maven-failsafe-plugin</artifactId> + <version>${maven-surefire-plugin.version}</version> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <version>${maven-jar-plugin.version}</version> + </plugin> + <plugin> + <groupId>com.mycila</groupId> + <artifactId>license-maven-plugin</artifactId> + <version>${mycila-license.version}</version> + <configuration> + <failIfUnknown>true</failIfUnknown> + <header>${maven.multiModuleProjectDirectory}/header.txt</header> + <excludes> + <exclude>**/*.adoc</exclude> + <exclude>**/*.csv</exclude> + <exclude>**/*.txt</exclude> + <exclude>**/LICENSE.txt</exclude> + <exclude>**/LICENSE</exclude> + <exclude>**/NOTICE.txt</exclude> + <exclude>**/NOTICE</exclude> + <exclude>**/README</exclude> + <exclude>**/pom.xml.versionsBackup</exclude> + </excludes> + <mapping> + <java>SLASHSTAR_STYLE</java> + <properties>CAMEL_PROPERTIES_STYLE</properties> + <kt>SLASHSTAR_STYLE</kt> + </mapping> + <headerDefinitions> + <headerDefinition>${maven.multiModuleProjectDirectory}/license-properties-headerdefinition.xml</headerDefinition> + </headerDefinitions> + </configuration> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <plugin> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-maven-plugin</artifactId> + <executions> + <execution> + <id>build</id> + <goals> + <goal>build</goal> + </goals> + </execution> + </executions> + <configuration> + <workingDir>${project.basedir}</workingDir> + </configuration> + </plugin> + <plugin> + <groupId>net.revelc.code.formatter</groupId> + <artifactId>formatter-maven-plugin</artifactId> + <executions> + <execution> + <id>format</id> + <phase>process-sources</phase> + <goals> + <goal>format</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>net.revelc.code</groupId> + <artifactId>impsort-maven-plugin</artifactId> + <executions> + <execution> + <id>sort-imports</id> + <phase>process-sources</phase> + <goals> + <goal>sort</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + <profiles> + <profile> + <id>native</id> + <activation> + <property> + <name>native</name> + </property> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-failsafe-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + <configuration> + <systemPropertyVariables> + <quarkus.package.type>${quarkus.package.type}</quarkus.package.type> + </systemPropertyVariables> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + <properties> + <quarkus.package.type>native</quarkus.package.type> + </properties> + </profile> + </profiles> +</project> diff --git a/jdbc-datasource/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java b/jdbc-datasource/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java new file mode 100644 index 0000000..808520b --- /dev/null +++ b/jdbc-datasource/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java @@ -0,0 +1,56 @@ +/* + * 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.quarkus.component.jdbc; + +import java.sql.Connection; +import java.sql.Statement; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; + +import io.agroal.api.AgroalDataSource; +import io.quarkus.agroal.DataSource; +import io.quarkus.runtime.StartupEvent; +import org.apache.camel.CamelContext; + +@ApplicationScoped +public class CamelResource { + + @Inject + @DataSource("camel-ds") + AgroalDataSource dataSource; + + void startup(@Observes StartupEvent event, CamelContext context) throws Exception { + context.getRouteController().startAllRoutes(); + } + + @PostConstruct + void postConstruct() throws Exception { + try (Connection con = dataSource.getConnection()) { + try (Statement statement = con.createStatement()) { + con.setAutoCommit(true); + try { + statement.execute("drop table camel"); + } catch (Exception ignored) { + } + statement.execute("create table camel (id serial primary key, timestamp varchar(255))"); + } + } + } +} diff --git a/jdbc-datasource/src/main/resources/application.properties b/jdbc-datasource/src/main/resources/application.properties new file mode 100644 index 0000000..f840633 --- /dev/null +++ b/jdbc-datasource/src/main/resources/application.properties @@ -0,0 +1,42 @@ +## --------------------------------------------------------------------------- +## 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.main.routes-include-pattern = file:src/main/resources/routes/camel-routes.xml + + +#Default datastore +quarkus.datasource.camel-ds.db-kind=h2 + +#If you want to have more than one datastore, you can use an identifier as this: +#quarkus.datasource.$identifier.db-kind=h2 +#Then use it on the route by name +#<to uri="jdbc:$identifier"/> + +#Configure the following section to use a maven profile (called prod) +#configured database (using postgresql on this case) +#Remember to edit the pom.xml to add the database driver needed +#<artifactId>quarkus-jdbc-postgresql on this case</artifactId> +#%prod.quarkus.datasource.db-kind=postgresql +#%prod.quarkus.datasource.username=${POSTGRESQL_USER} +#%prod.quarkus.datasource.password=${POSTGRESQL_PASSWORD} +#%prod.quarkus.datasource.jdbc.url=${POSTGRESQL_JDBC_URL} +#%prod.quarkus.datasource.jdbc.max-size=16 \ No newline at end of file diff --git a/jdbc-datasource/src/main/resources/routes/camel-routes.xml b/jdbc-datasource/src/main/resources/routes/camel-routes.xml new file mode 100644 index 0000000..dcb1162 --- /dev/null +++ b/jdbc-datasource/src/main/resources/routes/camel-routes.xml @@ -0,0 +1,44 @@ +<?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. + +--> + +<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://camel.apache.org/schema/spring" + xsi:schemaLocation=" + http://camel.apache.org/schema/spring + http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <route id="jdbc-datasource-route" + autoStartup="false"> + <from uri="timer://foo?period=1000"/> + <log message="Inserting Camel ${messageTimestamp}"/> + <setBody> + <simple>INSERT INTO Camel (timestamp) VALUES (${messageTimestamp})</simple> + </setBody> + <to uri="jdbc:default"/> + <log message="Inserted camel: ${messageTimestamp}"/> + <setBody> + <simple>SELECT * FROM Camel</simple> + </setBody> + <to uri="jdbc:default"/> + <log message="We have ${header[CamelJdbcRowCount]} camels in the database."/> + <log message="Camels found: ${body}"/> + </route> + +</routes> \ No newline at end of file diff --git a/jdbc-datasource/src/test/java/org/apache/camel/quarkus/component/jdbc/JdbcDataSourceIT.java b/jdbc-datasource/src/test/java/org/apache/camel/quarkus/component/jdbc/JdbcDataSourceIT.java new file mode 100644 index 0000000..5ddc0b2 --- /dev/null +++ b/jdbc-datasource/src/test/java/org/apache/camel/quarkus/component/jdbc/JdbcDataSourceIT.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.apache.camel.quarkus.component.jdbc; + +import io.quarkus.test.junit.NativeImageTest; + +@NativeImageTest +class JdbcDataSourceIT extends JdbcDataSourceTest { + +} diff --git a/jdbc-datasource/src/test/java/org/apache/camel/quarkus/component/jdbc/JdbcDataSourceTest.java b/jdbc-datasource/src/test/java/org/apache/camel/quarkus/component/jdbc/JdbcDataSourceTest.java new file mode 100644 index 0000000..2ab3527 --- /dev/null +++ b/jdbc-datasource/src/test/java/org/apache/camel/quarkus/component/jdbc/JdbcDataSourceTest.java @@ -0,0 +1,47 @@ +/* + * 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.quarkus.component.jdbc; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.TimeUnit; + +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.h2.H2DatabaseTestResource; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static org.awaitility.Awaitility.await; + +@QuarkusTest +@QuarkusTestResource(H2DatabaseTestResource.class) +public class JdbcDataSourceTest { + @Test + public void testCamelsInDatabase() throws Exception { + // Verify that camels are being inserted in the database: + 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("We have 2 camels in the database."); + }); + } +}