Skip to content

Commit 3eba193

Browse files
mcarlettCroway
authored andcommitted
CAMEL-19790: Add camel-spring-jdbc example
1 parent 8c57cda commit 3eba193

File tree

8 files changed

+307
-0
lines changed

8 files changed

+307
-0
lines changed

README.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Number of Examples: 49 (0 deprecated)
6060

6161
| link:spring-boot/readme.adoc[Spring Boot] (spring-boot) | Beginner | An example showing how to work with Camel and Spring Boot
6262

63+
| link:spring-jdbc/readme.adoc[Spring Jdbc] (spring-jdbc) | Beginner | Camel transacted routes integrating local Spring Transaction
64+
6365
| link:type-converter/README.adoc[Type Converter] (type-converter) | Beginner | An example showing how to create custom type converter with Camel and Spring Boot
6466

6567
| link:xml/readme.adoc[Spring Boot XML] (xml) | Beginner | An example showing how to work with Camel routes in XML files and Spring Boot

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<module>xml</module>
8282
<module>xml-import</module>
8383
<module>strimzi</module>
84+
<module>spring-jdbc</module>
8485
<module>quartz</module>
8586
<module>splitter-eip</module>
8687
<module>widget-gadget</module>

spring-jdbc/pom.xml

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<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">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.apache.camel.springboot.example</groupId>
25+
<artifactId>examples</artifactId>
26+
<version>4.1.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>camel-example-spring-boot-spring-jdbc</artifactId>
30+
<name>Camel SB Examples :: Spring JDBC</name>
31+
<description>Camel transacted routes integrating local Spring Transaction</description>
32+
33+
<properties>
34+
<category>Beginner</category>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
37+
</properties>
38+
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<!-- Spring Boot BOM -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-dependencies</artifactId>
46+
<version>${spring-boot-version}</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
<!-- Camel BOM -->
51+
<dependency>
52+
<groupId>org.apache.camel.springboot</groupId>
53+
<artifactId>camel-spring-boot-bom</artifactId>
54+
<version>${project.version}</version>
55+
<type>pom</type>
56+
<scope>import</scope>
57+
</dependency>
58+
</dependencies>
59+
</dependencyManagement>
60+
61+
62+
<dependencies>
63+
64+
<!-- Spring Boot -->
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
<exclusions>
69+
<exclusion>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-tomcat</artifactId>
72+
</exclusion>
73+
</exclusions>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-starter-undertow</artifactId>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.springframework.boot</groupId>
81+
<artifactId>spring-boot-starter-actuator</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.springframework.boot</groupId>
85+
<artifactId>spring-boot-starter-jdbc</artifactId>
86+
</dependency>
87+
88+
<!-- Camel -->
89+
<dependency>
90+
<groupId>org.apache.camel.springboot</groupId>
91+
<artifactId>camel-servlet-starter</artifactId>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.apache.camel.springboot</groupId>
95+
<artifactId>camel-rest-starter</artifactId>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.apache.camel.springboot</groupId>
99+
<artifactId>camel-spring-jdbc-starter</artifactId>
100+
</dependency>
101+
<dependency>
102+
<groupId>org.apache.camel.springboot</groupId>
103+
<artifactId>camel-direct-starter</artifactId>
104+
</dependency>
105+
106+
<!-- db driver -->
107+
<dependency>
108+
<groupId>org.postgresql</groupId>
109+
<artifactId>postgresql</artifactId>
110+
</dependency>
111+
</dependencies>
112+
113+
<build>
114+
<defaultGoal>spring-boot:run</defaultGoal>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.springframework.boot</groupId>
118+
<artifactId>spring-boot-maven-plugin</artifactId>
119+
<version>${spring-boot-version}</version>
120+
<executions>
121+
<execution>
122+
<goals>
123+
<goal>repackage</goal>
124+
</goals>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
</project>

spring-jdbc/readme.adoc

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
== Camel Example Spring JDBC
2+
3+
This example shows how `transacted()` routes create a jdbc local transaction integrating Camel and Spring Transaction
4+
5+
=== How to run
6+
Run the database container
7+
8+
podman run --rm --name db -e POSTGRES_PASSWORD=password -p 5432:5432 docker.io/library/postgres:latest
9+
10+
You can run this example using
11+
12+
mvn spring-boot:run
13+
14+
To execute the routes:
15+
16+
1. verify the table is empty (no data on response)
17+
18+
curl http://localhost:8080/api/horses
19+
20+
2. insert data
21+
22+
curl -X POST http://localhost:8080/api/horses -H "name: Varenne" -H "age: 8"
23+
24+
3. verify the data has been persisted
25+
26+
curl http://localhost:8080/api/horses
27+
28+
4. force to rollback after the insert (setting header "fail: true")
29+
30+
curl -X POST http://localhost:8080/api/horses -H "name: Seabiscuit" -H "age: 9" -H "fail: true"
31+
32+
5. verify the same content of the step 3
33+
34+
curl http://localhost:8080/api/horses
35+
36+
=== Help and contributions
37+
38+
If you hit any problem using Camel or have some feedback, then please
39+
https://camel.apache.org/support.html[let us know].
40+
41+
We also love contributors, so
42+
https://camel.apache.org/contributing.html[get involved] :-)
43+
44+
The Camel riders!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.example.spring.jdbc;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
//CHECKSTYLE:OFF
23+
@SpringBootApplication
24+
public class CamelSpringJdbcApplication {
25+
26+
public static void main(String[] args) {
27+
SpringApplication.run(CamelSpringJdbcApplication.class, args);
28+
}
29+
}
30+
// CHECKSTYLE:ON
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.example.spring.jdbc;
18+
19+
import org.apache.camel.builder.RouteBuilder;
20+
21+
import org.springframework.stereotype.Component;
22+
23+
@Component
24+
public class SpringJdbcRoute extends RouteBuilder {
25+
26+
protected static final String INSERT_QUERY = "insert into horses(name,age) values('${header.name}',${header.age})";
27+
protected static final String SELECT_QUERY = "select * from horses";
28+
29+
@Override
30+
public void configure() throws Exception {
31+
32+
rest()
33+
.post("/horses")
34+
.to("direct:persist")
35+
.get("/horses")
36+
.to("direct:read");
37+
38+
from("direct:persist")
39+
.choice().when(simple("${header.fail} == 'true'"))
40+
.to("direct:rollback")
41+
.otherwise()
42+
.to("direct:commit");
43+
44+
from("direct:commit")
45+
.setBody(simple(INSERT_QUERY))
46+
.transacted()
47+
.to("spring-jdbc:default?resetAutoCommit=false")
48+
.setBody(constant("executed"));
49+
50+
from("direct:rollback")
51+
.setBody(simple(INSERT_QUERY))
52+
.transacted()
53+
.to("spring-jdbc:default?resetAutoCommit=false")
54+
.rollback("forced to rollback");
55+
56+
from("direct:read")
57+
.setBody(simple(SELECT_QUERY))
58+
.to("spring-jdbc:default");
59+
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## ---------------------------------------------------------------------------
2+
## Licensed to the Apache Software Foundation (ASF) under one or more
3+
## contributor license agreements. See the NOTICE file distributed with
4+
## this work for additional information regarding copyright ownership.
5+
## The ASF licenses this file to You under the Apache License, Version 2.0
6+
## (the "License"); you may not use this file except in compliance with
7+
## the License. You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
## ---------------------------------------------------------------------------
17+
18+
camel.springboot.name = MyCamel
19+
camel.springboot.main-run-controller=true
20+
21+
camel.servlet.mapping.enabled = true
22+
camel.servlet.mapping.context-path = /api/*
23+
24+
#database configuration
25+
spring.datasource.driver-class-name=org.postgresql.Driver
26+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
27+
spring.datasource.username=postgres
28+
spring.datasource.password=password
29+
#init schema.sql
30+
spring.sql.init.mode=always
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DROP TABLE IF EXISTS horses;
2+
CREATE TABLE horses (
3+
name VARCHAR(255),
4+
age INT,
5+
ts TIMESTAMP NOT NULL DEFAULT now()
6+
);

0 commit comments

Comments
 (0)