You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by fm...@apache.org on 2023/08/31 12:45:26 UTC

[camel-spring-boot-examples] branch main updated: CAMEL-19790: Add camel-spring-jdbc example

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 3eba193  CAMEL-19790: Add camel-spring-jdbc example
3eba193 is described below

commit 3eba193eef0c52de8eb2b20350a7409f560c0ddd
Author: Marco Carletti <mc...@redhat.com>
AuthorDate: Fri Aug 25 17:32:21 2023 +0200

    CAMEL-19790: Add camel-spring-jdbc example
---
 README.adoc                                        |   2 +
 pom.xml                                            |   1 +
 spring-jdbc/pom.xml                                | 130 +++++++++++++++++++++
 spring-jdbc/readme.adoc                            |  44 +++++++
 .../spring/jdbc/CamelSpringJdbcApplication.java    |  31 +++++
 .../camel/example/spring/jdbc/SpringJdbcRoute.java |  62 ++++++++++
 .../src/main/resources/application.properties      |  31 +++++
 spring-jdbc/src/main/resources/schema.sql          |   6 +
 8 files changed, 307 insertions(+)

diff --git a/README.adoc b/README.adoc
index 062e3d6..b53c8ac 100644
--- a/README.adoc
+++ b/README.adoc
@@ -60,6 +60,8 @@ Number of Examples: 49 (0 deprecated)
 
 | link:spring-boot/readme.adoc[Spring Boot] (spring-boot) | Beginner | An example showing how to work with Camel and Spring Boot
 
+| link:spring-jdbc/readme.adoc[Spring Jdbc] (spring-jdbc) | Beginner | Camel transacted routes integrating local Spring Transaction
+
 | link:type-converter/README.adoc[Type Converter] (type-converter) | Beginner | An example showing how to create custom type converter with Camel and Spring Boot
 
 | link:xml/readme.adoc[Spring Boot XML] (xml) | Beginner | An example showing how to work with Camel routes in XML files and Spring Boot
diff --git a/pom.xml b/pom.xml
index 2899739..6bc5e3e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -81,6 +81,7 @@
 		<module>xml</module>
 		<module>xml-import</module>
 		<module>strimzi</module>
+		<module>spring-jdbc</module>
 		<module>quartz</module>
 		<module>splitter-eip</module>
 		<module>widget-gadget</module>
diff --git a/spring-jdbc/pom.xml b/spring-jdbc/pom.xml
new file mode 100644
index 0000000..83d5c74
--- /dev/null
+++ b/spring-jdbc/pom.xml
@@ -0,0 +1,130 @@
+<?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 http://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>
+
+	<parent>
+		<groupId>org.apache.camel.springboot.example</groupId>
+		<artifactId>examples</artifactId>
+		<version>4.1.0-SNAPSHOT</version>
+	</parent>
+
+	<artifactId>camel-example-spring-boot-spring-jdbc</artifactId>
+	<name>Camel SB Examples :: Spring JDBC</name>
+	<description>Camel transacted routes integrating local Spring Transaction</description>
+
+	<properties>
+		<category>Beginner</category>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+	</properties>
+
+
+	<dependencyManagement>
+		<dependencies>
+			<!-- Spring Boot BOM -->
+			<dependency>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-dependencies</artifactId>
+				<version>${spring-boot-version}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+			<!-- Camel BOM -->
+			<dependency>
+				<groupId>org.apache.camel.springboot</groupId>
+				<artifactId>camel-spring-boot-bom</artifactId>
+				<version>${project.version}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
+
+
+	<dependencies>
+
+		<!-- Spring Boot -->
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+			<exclusions>
+				<exclusion>
+					<groupId>org.springframework.boot</groupId>
+					<artifactId>spring-boot-starter-tomcat</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-undertow</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-actuator</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-jdbc</artifactId>
+		</dependency>
+
+		<!-- Camel -->
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-servlet-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-rest-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-spring-jdbc-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-direct-starter</artifactId>
+		</dependency>
+
+		<!-- db driver -->
+		<dependency>
+			<groupId>org.postgresql</groupId>
+			<artifactId>postgresql</artifactId>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<defaultGoal>spring-boot:run</defaultGoal>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+				<version>${spring-boot-version}</version>
+				<executions>
+					<execution>
+						<goals>
+							<goal>repackage</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>
diff --git a/spring-jdbc/readme.adoc b/spring-jdbc/readme.adoc
new file mode 100644
index 0000000..d778f89
--- /dev/null
+++ b/spring-jdbc/readme.adoc
@@ -0,0 +1,44 @@
+== Camel Example Spring JDBC
+
+This example shows how `transacted()` routes create a jdbc local transaction integrating Camel and Spring Transaction
+
+=== How to run
+Run the database container
+
+    podman run --rm --name db -e POSTGRES_PASSWORD=password -p 5432:5432 docker.io/library/postgres:latest
+
+You can run this example using
+
+    mvn spring-boot:run
+
+To execute the routes:
+
+1. verify the table is empty (no data on response)
+
+    curl http://localhost:8080/api/horses
+
+2. insert data
+
+     curl -X POST http://localhost:8080/api/horses -H "name: Varenne" -H "age: 8"
+
+3. verify the data has been persisted
+
+    curl http://localhost:8080/api/horses
+
+4. force to rollback after the insert (setting header "fail: true")
+
+    curl -X POST http://localhost:8080/api/horses -H "name: Seabiscuit" -H "age: 9" -H "fail: true"
+
+5. verify the same content of the step 3
+
+    curl http://localhost:8080/api/horses
+
+=== 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/spring-jdbc/src/main/java/org/apache/camel/example/spring/jdbc/CamelSpringJdbcApplication.java b/spring-jdbc/src/main/java/org/apache/camel/example/spring/jdbc/CamelSpringJdbcApplication.java
new file mode 100644
index 0000000..4963e13
--- /dev/null
+++ b/spring-jdbc/src/main/java/org/apache/camel/example/spring/jdbc/CamelSpringJdbcApplication.java
@@ -0,0 +1,31 @@
+/*
+ * 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.spring.jdbc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+//CHECKSTYLE:OFF
+@SpringBootApplication
+public class CamelSpringJdbcApplication {
+
+	public static void main(String[] args) {
+		SpringApplication.run(CamelSpringJdbcApplication.class, args);
+	}
+}
+// CHECKSTYLE:ON
+
diff --git a/spring-jdbc/src/main/java/org/apache/camel/example/spring/jdbc/SpringJdbcRoute.java b/spring-jdbc/src/main/java/org/apache/camel/example/spring/jdbc/SpringJdbcRoute.java
new file mode 100644
index 0000000..f435719
--- /dev/null
+++ b/spring-jdbc/src/main/java/org/apache/camel/example/spring/jdbc/SpringJdbcRoute.java
@@ -0,0 +1,62 @@
+/*
+ * 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.spring.jdbc;
+
+import org.apache.camel.builder.RouteBuilder;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class SpringJdbcRoute extends RouteBuilder {
+
+    protected static final String INSERT_QUERY = "insert into horses(name,age) values('${header.name}',${header.age})";
+    protected static final String SELECT_QUERY = "select * from horses";
+
+    @Override
+    public void configure() throws Exception {
+
+        rest()
+                .post("/horses")
+                    .to("direct:persist")
+                .get("/horses")
+                    .to("direct:read");
+
+        from("direct:persist")
+                .choice().when(simple("${header.fail} == 'true'"))
+                        .to("direct:rollback")
+                    .otherwise()
+                        .to("direct:commit");
+
+        from("direct:commit")
+                .setBody(simple(INSERT_QUERY))
+                .transacted()
+                    .to("spring-jdbc:default?resetAutoCommit=false")
+                .setBody(constant("executed"));
+
+        from("direct:rollback")
+                .setBody(simple(INSERT_QUERY))
+                .transacted()
+                    .to("spring-jdbc:default?resetAutoCommit=false")
+                .rollback("forced to rollback");
+
+        from("direct:read")
+                .setBody(simple(SELECT_QUERY))
+                .to("spring-jdbc:default");
+
+    }
+
+}
diff --git a/spring-jdbc/src/main/resources/application.properties b/spring-jdbc/src/main/resources/application.properties
new file mode 100644
index 0000000..3f43eb4
--- /dev/null
+++ b/spring-jdbc/src/main/resources/application.properties
@@ -0,0 +1,31 @@
+## ---------------------------------------------------------------------------
+## 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.springboot.name = MyCamel
+camel.springboot.main-run-controller=true
+
+camel.servlet.mapping.enabled = true
+camel.servlet.mapping.context-path = /api/*
+
+#database configuration
+spring.datasource.driver-class-name=org.postgresql.Driver
+spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
+spring.datasource.username=postgres
+spring.datasource.password=password
+#init schema.sql
+spring.sql.init.mode=always
+
diff --git a/spring-jdbc/src/main/resources/schema.sql b/spring-jdbc/src/main/resources/schema.sql
new file mode 100644
index 0000000..ecd887c
--- /dev/null
+++ b/spring-jdbc/src/main/resources/schema.sql
@@ -0,0 +1,6 @@
+DROP TABLE IF EXISTS horses;
+CREATE TABLE horses (
+  name VARCHAR(255),
+  age INT,
+  ts TIMESTAMP NOT NULL DEFAULT now()
+);