You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2023/10/25 06:00:18 UTC

[camel-k-examples] branch main updated: feat(database): Add multiple datasources example

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

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


The following commit(s) were added to refs/heads/main by this push:
     new f96f57b  feat(database): Add multiple datasources example
f96f57b is described below

commit f96f57bba2399a2b995dfabdc71f48eddcd354d0
Author: Gaelle Fournier <ga...@gmail.com>
AuthorDate: Fri Oct 13 16:17:12 2023 +0200

    feat(database): Add multiple datasources example
---
 .../databases/H2DBMultiDatasources.java            | 67 ++++++++++++++++++++++
 generic-examples/databases/README.md               | 45 ++++++++++++++-
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/generic-examples/databases/H2DBMultiDatasources.java b/generic-examples/databases/H2DBMultiDatasources.java
new file mode 100644
index 0000000..7d7d164
--- /dev/null
+++ b/generic-examples/databases/H2DBMultiDatasources.java
@@ -0,0 +1,67 @@
+/*
+ * 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.
+ */
+
+// camel-k: language=java
+// camel-k: dependency=mvn:io.quarkus:quarkus-jdbc-h2
+// camel-k: build-property=quarkus.datasource.default.db-kind=h2
+// camel-k: property=quarkus.datasource.default.username=username-default
+// camel-k: property=quarkus.datasource.default.jdbc.url=jdbc:h2:mem:default
+// camel-k: build-property=quarkus.datasource.testing.db-kind=h2
+// camel-k: property=quarkus.datasource.testing.username=username-testing
+// camel-k: property=quarkus.datasource.testing.jdbc.url=jdbc:h2:mem:testing
+// camel-k: trait=camel.runtime-version=3.2.0
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class H2DBMultiDatasources extends RouteBuilder {
+
+
+    @Override
+    public void configure() throws Exception {
+
+        from("timer://initCamel?repeatCount=1")
+                .routeId("Init")
+                .log("Create table defaultcamel")
+                .to("sql:CREATE TABLE defaultcamel (id SERIAL PRIMARY KEY, timestamp VARCHAR(255))?dataSource=#default")
+                .to("log:DEBUG?showBody=true&showHeaders=true")
+                .log("SUCCESS Create table defaultcamel")
+                .log("Create table testingcamel")
+                .to("sql:CREATE TABLE testingcamel (id SERIAL PRIMARY KEY, timestamp VARCHAR(255))?dataSource=#testing")
+                .to("log:DEBUG?showBody=true&showHeaders=true")
+                .log("SUCCESS Create table testingcamel");
+
+
+        from("timer://insertCamel?period=1000&delay=5000")
+            .routeId("Insert")
+                .log("Inserting defaultcamel ${messageTimestamp}")
+                .setBody().simple("INSERT INTO defaultcamel (timestamp) VALUES (${messageTimestamp})")
+                .to("jdbc:default")
+                .log("Inserted defaultcamel ${messageTimestamp}")
+                .log("Inserting testingcamel ${messageTimestamp}")
+                .setBody().simple("INSERT INTO testingcamel (timestamp) VALUES (${messageTimestamp})")
+                .to("jdbc:testing")
+                .log("Inserted testingcamel ${messageTimestamp}");
+
+        from("timer://selectCamel?period=1000&delay=5000")
+                .routeId("Test")
+                .to("sql:SELECT * FROM defaultcamel?dataSource=#default")
+                .to("log:DEBUG?showBody=true&showHeaders=true")
+                .to("sql:SELECT * FROM testingcamel?dataSource=#testing")
+                .to("log:DEBUG?showBody=true&showHeaders=true")
+                .setBody().simple("SUCCESS");
+    }
+}
\ No newline at end of file
diff --git a/generic-examples/databases/README.md b/generic-examples/databases/README.md
index 7727428..0f06a24 100644
--- a/generic-examples/databases/README.md
+++ b/generic-examples/databases/README.md
@@ -37,4 +37,47 @@ If successful, the query result: `hello` and `world`, should be logged to the te
 [1] 2022-06-30 09:30:56,313 INFO  [info] (Camel (camel-1) thread #1 - timer://foo) Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body: [{data=hello}, {data=world}]]
 [1] 2022-06-30 09:31:06,312 INFO  [info] (Camel (camel-1) thread #1 - timer://foo) Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body: [{data=hello}, {data=world}]]
 [1] 2022-06-30 09:31:16,313 INFO  [info] (Camel (camel-1) thread #1 - timer://foo) Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body: [{data=hello}, {data=world}]]
-```
\ No newline at end of file
+```
+
+## Multiple datasources example
+
+An additional example using multiple datasources with an in-memory H2 database is available as a standalone. It does not requires the PostgreSQL instance.
+
+It shows how to configure and use multiple datasources.
+
+[`H2DBMultiDatasources.java`](./H2DBMultiDatasources.java) contains the integration code. It defines 2 datasources, with 3 routes:
+- initialise a table with each datasource only once
+- insert periodically datas via each datasource
+- queries periodically bot datasources and logs the result.
+
+You will find most of the configuration parameters inside the integration code.
+
+
+To run it you only need the following :
+```
+kamel run H2DBMultiDatasources.java --dev
+```
+
+If successful, the following should be logged to the terminal every 10 seconds:
+```console
+[1] 2023-10-13 14:14:48,310 INFO  [Init] (Camel (camel-1) thread #1 - timer://initCamel) Create table defaultcamel
+[1] 2023-10-13 14:14:48,470 INFO  [DEBUG] (Camel (camel-1) thread #1 - timer://initCamel) Exchange[Headers: {CamelMessageTimestamp=1697206488298, CamelSqlUpdateCount=0, firedTime=Fri Oct 13 14:14:48 UTC 2023}, BodyType: null, Body: [Body is null]]
+[1] 2023-10-13 14:14:48,470 INFO  [Init] (Camel (camel-1) thread #1 - timer://initCamel) SUCCESS Create table defaultcamel
+[1] 2023-10-13 14:14:48,470 INFO  [Init] (Camel (camel-1) thread #1 - timer://initCamel) Create table testingcamel
+[1] 2023-10-13 14:14:48,474 INFO  [DEBUG] (Camel (camel-1) thread #1 - timer://initCamel) Exchange[Headers: {CamelMessageTimestamp=1697206488298, CamelSqlUpdateCount=0, firedTime=Fri Oct 13 14:14:48 UTC 2023}, BodyType: null, Body: [Body is null]]
+[1] 2023-10-13 14:14:48,475 INFO  [Init] (Camel (camel-1) thread #1 - timer://initCamel) SUCCESS Create table testingcamel
+[1] 2023-10-13 14:14:52,299 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserting defaultcamel 1697206492298
+[1] 2023-10-13 14:14:52,310 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserted defaultcamel 1697206492298
+[1] 2023-10-13 14:14:52,311 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserting testingcamel 1697206492298
+[1] 2023-10-13 14:14:52,312 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserted testingcamel 1697206492298
+[1] 2023-10-13 14:14:52,342 INFO  [DEBUG] (Camel (camel-1) thread #3 - timer://selectCamel) Exchange[Headers: {CamelMessageTimestamp=1697206492298, CamelSqlRowCount=1, firedTime=Fri Oct 13 14:14:52 UTC 2023}, BodyType: java.util.ArrayList, Body: [{ID=1, TIMESTAMP=1697206492298}]]
+[1] 2023-10-13 14:14:52,345 INFO  [DEBUG] (Camel (camel-1) thread #3 - timer://selectCamel) Exchange[Headers: {CamelMessageTimestamp=1697206492298, CamelSqlRowCount=1, firedTime=Fri Oct 13 14:14:52 UTC 2023}, BodyType: java.util.ArrayList, Body: [{ID=1, TIMESTAMP=1697206492298}]]
+[1] 2023-10-13 14:14:53,299 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserting defaultcamel 1697206493299
+[1] 2023-10-13 14:14:53,301 INFO  [DEBUG] (Camel (camel-1) thread #3 - timer://selectCamel) Exchange[Headers: {CamelMessageTimestamp=1697206493299, CamelSqlRowCount=1, firedTime=Fri Oct 13 14:14:53 UTC 2023}, BodyType: java.util.ArrayList, Body: [{ID=1, TIMESTAMP=1697206492298}]]
+[1] 2023-10-13 14:14:53,302 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserted defaultcamel 1697206493299
+[1] 2023-10-13 14:14:53,303 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserting testingcamel 1697206493299
+[1] 2023-10-13 14:14:53,304 INFO  [DEBUG] (Camel (camel-1) thread #3 - timer://selectCamel) Exchange[Headers: {CamelMessageTimestamp=1697206493299, CamelSqlRowCount=1, firedTime=Fri Oct 13 14:14:53 UTC 2023}, BodyType: java.util.ArrayList, Body: [{ID=1, TIMESTAMP=1697206492298}]]
+[1] 2023-10-13 14:14:53,304 INFO  [Insert] (Camel (camel-1) thread #2 - timer://insertCamel) Inserted testingcamel 1697206493299
+
+```
+