You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2020/11/02 14:43:07 UTC

[camel-quarkus] branch master updated: PostgresSQL Event : add usage of Quarkus AgroalDatasource fixes #1909

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 03a9b37  PostgresSQL Event : add usage of Quarkus AgroalDatasource fixes #1909
03a9b37 is described below

commit 03a9b37b41bc58e747729f5e894046bad0261ec0
Author: Zineb Bendhiba <be...@gmail.com>
AuthorDate: Fri Oct 30 16:43:44 2020 +0100

    PostgresSQL Event : add usage of Quarkus AgroalDatasource fixes #1909
---
 .../ROOT/pages/reference/extensions/pgevent.adoc   | 28 ++++++++++++++++
 extensions/pgevent/deployment/pom.xml              |  4 +++
 .../pgevent/deployment/PgeventProcessor.java       | 14 ++++++++
 extensions/pgevent/runtime/pom.xml                 |  5 +++
 .../runtime/src/main/doc/configuration.adoc        | 24 ++++++++++++++
 integration-tests/pgevent/pom.xml                  | 14 +++++---
 .../camel/quarkus/component/pgevent/it/MyBean.java | 26 ---------------
 .../component/pgevent/it/PgeventRoutes.java        | 38 ++++++++--------------
 .../src/main/resources/application.properties      | 13 ++++++--
 .../component/pgevent/it/PgEventTestResource.java  |  4 ++-
 10 files changed, 111 insertions(+), 59 deletions(-)

diff --git a/docs/modules/ROOT/pages/reference/extensions/pgevent.adoc b/docs/modules/ROOT/pages/reference/extensions/pgevent.adoc
index 3a393d3..1b8dc46 100644
--- a/docs/modules/ROOT/pages/reference/extensions/pgevent.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/pgevent.adoc
@@ -31,3 +31,31 @@ Please refer to the above link for usage and configuration details.
 ----
 
 Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
+
+== Additional Camel Quarkus configuration
+
+You can use pgevent extension with Agroal Datasource.
+
+Add the quarkus-agroal dependency :
+[source,xml]
+------------------------------------------------------------
+<dependency>
+    <groupId>io.quarkus</groupId>
+    <artifactId>quarkus-agroal</artifactId>
+</dependency>
+------------------------------------------------------------
+
+Set Agroal properties, example for named DataSource `pgDatasource` :
+-----------
+quarkus.datasource.pgDatasource.db-kind=pgsql
+quarkus.datasource.pgDatasource.jdbc.url=jdbc:pgsql://localhost:5432/myDB
+quarkus.datasource.pgDatasource.username=postgres
+quarkus.datasource.pgDatasource.password=mysecretpassword
+quarkus.datasource.pgDatasource.jdbc.max-size=16
+-----------
+
+Inject the DataSource name in the camel Route, example :
+-----------
+pgevent:///postgres/testchannel?datasource=#pgDatasource
+-----------
+
diff --git a/extensions/pgevent/deployment/pom.xml b/extensions/pgevent/deployment/pom.xml
index 91b0d56..6a468e4 100644
--- a/extensions/pgevent/deployment/pom.xml
+++ b/extensions/pgevent/deployment/pom.xml
@@ -42,6 +42,10 @@
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-netty-deployment</artifactId>
         </dependency>
+        <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-agroal-spi</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/extensions/pgevent/deployment/src/main/java/org/apache/camel/quarkus/component/pgevent/deployment/PgeventProcessor.java b/extensions/pgevent/deployment/src/main/java/org/apache/camel/quarkus/component/pgevent/deployment/PgeventProcessor.java
index bc841fb..658ca26 100644
--- a/extensions/pgevent/deployment/src/main/java/org/apache/camel/quarkus/component/pgevent/deployment/PgeventProcessor.java
+++ b/extensions/pgevent/deployment/src/main/java/org/apache/camel/quarkus/component/pgevent/deployment/PgeventProcessor.java
@@ -22,9 +22,12 @@ import java.util.Set;
 import java.util.stream.Stream;
 
 import com.impossibl.postgres.system.procs.ProcProvider;
+import io.quarkus.agroal.spi.JdbcDriverBuildItem;
+import io.quarkus.datasource.common.runtime.DatabaseKind;
 import io.quarkus.deployment.annotations.BuildProducer;
 import io.quarkus.deployment.annotations.BuildStep;
 import io.quarkus.deployment.builditem.FeatureBuildItem;
+import io.quarkus.deployment.builditem.SslNativeConfigBuildItem;
 import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
 import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
 import io.quarkus.deployment.util.ServiceUtil;
@@ -65,4 +68,15 @@ class PgeventProcessor {
                 });
     }
 
+    /**
+     * Setting Agroal Datasource driver for pgjdbc-ng driver
+     *
+     */
+    @BuildStep
+    void registerDriver(BuildProducer<JdbcDriverBuildItem> jdbcDriver,
+            SslNativeConfigBuildItem sslNativeConfigBuildItem) {
+        jdbcDriver.produce(new JdbcDriverBuildItem(DatabaseKind.POSTGRESQL, "com.impossibl.postgres.jdbc.PGDriver",
+                "com.impossibl.postgres.jdbc.xa.PGXADataSource"));
+    }
+
 }
diff --git a/extensions/pgevent/runtime/pom.xml b/extensions/pgevent/runtime/pom.xml
index 970ddc5..b03fcd1 100644
--- a/extensions/pgevent/runtime/pom.xml
+++ b/extensions/pgevent/runtime/pom.xml
@@ -60,6 +60,11 @@
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-netty</artifactId>
         </dependency>
+        <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-agroal</artifactId>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/extensions/pgevent/runtime/src/main/doc/configuration.adoc b/extensions/pgevent/runtime/src/main/doc/configuration.adoc
new file mode 100644
index 0000000..a11a799
--- /dev/null
+++ b/extensions/pgevent/runtime/src/main/doc/configuration.adoc
@@ -0,0 +1,24 @@
+You can use pgevent extension with Agroal Datasource.
+
+Add the quarkus-agroal dependency :
+[source,xml]
+------------------------------------------------------------
+<dependency>
+    <groupId>io.quarkus</groupId>
+    <artifactId>quarkus-agroal</artifactId>
+</dependency>
+------------------------------------------------------------
+
+Set Agroal properties, example for named DataSource `pgDatasource` :
+-----------
+quarkus.datasource.pgDatasource.db-kind=pgsql
+quarkus.datasource.pgDatasource.jdbc.url=jdbc:pgsql://localhost:5432/myDB
+quarkus.datasource.pgDatasource.username=postgres
+quarkus.datasource.pgDatasource.password=mysecretpassword
+quarkus.datasource.pgDatasource.jdbc.max-size=16
+-----------
+
+Inject the DataSource name in the camel Route, example :
+-----------
+pgevent:///postgres/testchannel?datasource=#pgDatasource
+-----------
\ No newline at end of file
diff --git a/integration-tests/pgevent/pom.xml b/integration-tests/pgevent/pom.xml
index 9da598d..72142bc 100644
--- a/integration-tests/pgevent/pom.xml
+++ b/integration-tests/pgevent/pom.xml
@@ -56,7 +56,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
-            <artifactId>camel-quarkus-bean</artifactId>
+            <artifactId>camel-quarkus-mock</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-agroal</artifactId>
         </dependency>
 
         <!-- test dependencies -->
@@ -75,11 +79,9 @@
             <artifactId>camel-quarkus-integration-testcontainers-support</artifactId>
             <scope>test</scope>
         </dependency>
-
-        <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
-            <artifactId>camel-quarkus-pgevent-deployment</artifactId>
+            <artifactId>camel-quarkus-mock-deployment</artifactId>
             <version>${project.version}</version>
             <type>pom</type>
             <scope>test</scope>
@@ -94,7 +96,7 @@
         <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
-            <artifactId>camel-quarkus-bean-deployment</artifactId>
+            <artifactId>camel-quarkus-pgevent-deployment</artifactId>
             <version>${project.version}</version>
             <type>pom</type>
             <scope>test</scope>
@@ -105,6 +107,8 @@
                 </exclusion>
             </exclusions>
         </dependency>
+
+        <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-direct-deployment</artifactId>
diff --git a/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/MyBean.java b/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/MyBean.java
deleted file mode 100644
index bed46a6..0000000
--- a/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/MyBean.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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.pgevent.it;
-
-import io.quarkus.runtime.annotations.RegisterForReflection;
-
-@RegisterForReflection
-public class MyBean {
-    public static String getSubscribedEvent(String body) {
-        return body;
-    }
-}
diff --git a/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/PgeventRoutes.java b/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/PgeventRoutes.java
index 61fdc82..ed5f9c9 100644
--- a/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/PgeventRoutes.java
+++ b/integration-tests/pgevent/src/main/java/org/apache/camel/quarkus/component/pgevent/it/PgeventRoutes.java
@@ -16,55 +16,45 @@
  */
 package org.apache.camel.quarkus.component.pgevent.it;
 
-import javax.inject.Named;
-import javax.ws.rs.Produces;
+import javax.enterprise.context.ApplicationScoped;
 
-import com.impossibl.postgres.jdbc.PGDataSource;
 import org.apache.camel.builder.RouteBuilder;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
 
+@ApplicationScoped
 public class PgeventRoutes extends RouteBuilder {
+
+    public static final String MOCK_ENDPOINT_CLASSIC_CONF = "mock:classic";
+    public static final String MOCK_ENDPOINT_AGROALDATASOURCE = "mock:datasource";
+
     @ConfigProperty(name = "database.host")
     String host;
     @ConfigProperty(name = "database.port")
     Integer port;
     @ConfigProperty(name = "database.name")
     String databaseName;
-    @ConfigProperty(name = "database.user")
+    @ConfigProperty(name = "quarkus.datasource.pgDatasource.username")
     String user;
-    @ConfigProperty(name = "database.password")
+    @ConfigProperty(name = "quarkus.datasource.pgDatasource.password")
     String password;
 
     @Override
     public void configure() throws Exception {
         // producer for simple pub-sub
         from("direct:pgevent-pub")
-                .to("pgevent://{{database.host}}:{{database.port}}/{{database.name}}/testchannel?user={{database.user}}&pass={{database.password}}");
+                .to(String.format("pgevent://%s:%s/%s/testchannel?user=%s&pass=%s", host, port, databaseName, user, password));
 
         //consumer for simple pub-sub
-        from("pgevent://{{database.host}}:{{database.port}}/{{database.name}}/testchannel?user={{database.user}}&pass={{database.password}}")
-                .log("Message got ${body}")
-                .bean(MyBean.class);
+        from(String.format("pgevent://%s:%s/%s/testchannel?user=%s&pass=%s", host, port, databaseName, user, password))
+                .to(MOCK_ENDPOINT_CLASSIC_CONF);
 
         // producer with datasource
         from("direct:pgevent-datasource")
-                .to("pgevent:///postgres/testchannel?datasource=#pgDataSource");
+                .to("pgevent:///postgres/testchannel?datasource=#pgDatasource");
 
         // consumer with datasource
-        from("pgevent:///postgres/testchannel?datasource=#pgDataSource")
-                .log("Message got ${body}")
-                .bean(MyBean.class);
+        from("pgevent:///postgres/testchannel?datasource=#pgDatasource")
+                .to(MOCK_ENDPOINT_AGROALDATASOURCE);
     }
 
-    @Produces
-    @Named("pgDataSource")
-    public PGDataSource loadDataSource() throws Exception {
-        PGDataSource dataSource = new PGDataSource();
-        dataSource.setHost(host);
-        dataSource.setPort(port);
-        dataSource.setDatabaseName(databaseName);
-        dataSource.setUser(user);
-        dataSource.setPassword(password);
-        return dataSource;
-    }
 }
diff --git a/integration-tests/pgevent/src/main/resources/application.properties b/integration-tests/pgevent/src/main/resources/application.properties
index 727f522..aec4000 100644
--- a/integration-tests/pgevent/src/main/resources/application.properties
+++ b/integration-tests/pgevent/src/main/resources/application.properties
@@ -17,8 +17,15 @@
 # Change the following properties to work with your PostgreSQL instance.
 # The default values work with the PostgreSQL docker image https://hub.docker.com/_/postgres/.
 # docker run -ti --rm -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 postgres
+
+# classic conf
 database.name=postgres
-database.user=postgres
-database.password=mysecretpassword
 database.host=localhost
-database.port=5432
\ No newline at end of file
+database.port=5432
+
+#Agroal datasource conf named pgDatasource
+quarkus.datasource.pgDatasource.db-kind=pgsql
+quarkus.datasource.pgDatasource.jdbc.url=jdbc:pgsql://localhost:5432/postgres
+quarkus.datasource.pgDatasource.username=postgres
+quarkus.datasource.pgDatasource.password=mysecretpassword
+quarkus.datasource.pgDatasource.jdbc.max-size=16
diff --git a/integration-tests/pgevent/src/test/java/org/apache/camel/quarkus/component/pgevent/it/PgEventTestResource.java b/integration-tests/pgevent/src/test/java/org/apache/camel/quarkus/component/pgevent/it/PgEventTestResource.java
index 4d8bdb1..c585409 100644
--- a/integration-tests/pgevent/src/test/java/org/apache/camel/quarkus/component/pgevent/it/PgEventTestResource.java
+++ b/integration-tests/pgevent/src/test/java/org/apache/camel/quarkus/component/pgevent/it/PgEventTestResource.java
@@ -47,7 +47,9 @@ public class PgEventTestResource implements ContainerResourceLifecycleManager {
                 "database.port",
                 container.getMappedPort(POSTGRES_PORT).toString(),
                 "database.host",
-                container.getHost());
+                container.getHost(),
+                "quarkus.datasource.pgDatasource.jdbc.url",
+                String.format("jdbc:pgsql://%s:%s/postgres", container.getHost(), container.getMappedPort(POSTGRES_PORT)));
     }
 
     @Override