forked from apache/camel-spring-boot-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#145 from rinaldodev/jws-example
JBoss Web Server example
- Loading branch information
Showing
11 changed files
with
481 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.apache.camel.springboot.example</groupId> | ||
<artifactId>examples</artifactId> | ||
<version>4.4.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>camel-example-spring-boot</artifactId> | ||
<name>Camel SB Examples :: Tomcat JDBC</name> | ||
<description>An example showing how to deploy a Camel Spring Boot application in Tomcat using its JDBC Data Source</description> | ||
<packaging>war</packaging> | ||
|
||
<properties> | ||
<category>Beginner</category> | ||
|
||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<!-- Camel BOM --> | ||
<dependency> | ||
<groupId>com.redhat.camel.springboot.platform</groupId> | ||
<artifactId>camel-spring-boot-bom</artifactId> | ||
<version>${camel-spring-boot-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> | ||
</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> | ||
<exclusions> | ||
<exclusion> | ||
<!-- Spring Boot uses HikariCP DataSource by default, so we remove it --> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<!-- Tomcat --> | ||
<!-- Those dependencies will be provided by the tomcat server and therefore must be flagged as 'provided' --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-tomcat</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.tomcat</groupId> | ||
<artifactId>tomcat-jdbc</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- db driver --> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
</dependency> | ||
|
||
<!-- Camel --> | ||
<dependency> | ||
<groupId>org.apache.camel.springboot</groupId> | ||
<artifactId>camel-spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel.springboot</groupId> | ||
<artifactId>camel-spring-jdbc-starter</artifactId> | ||
</dependency> | ||
|
||
<!-- test --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-test-spring-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>${artifactId}</finalName> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
== Camel Spring Boot example with JBoss Web Server and database connection | ||
|
||
This example shows how to work with a simple Apache Camel Spring Boot application deploying to JBoss Web Server and using its JDBC data source. | ||
|
||
The example generates 2 routes: | ||
|
||
. The first route is triggered by a timer and writes messages to the database. | ||
. The second route is triggered by a timer and read the COUNT of messages from the database. | ||
|
||
=== Classes | ||
|
||
* `MyCamelRouter`: where the camel routes are defined | ||
* `MyBean`: used to generate a simple message with a counter | ||
* `MyCamelApplication`: the Spring Boot servlet initializer | ||
|
||
=== How to deploy | ||
|
||
. Run the database container | ||
[source,console] | ||
podman run --rm --name db -e POSTGRES_PASSWORD=password -p 5432:5432 docker.io/library/postgres:latest | ||
|
||
. Run the tests and package the `war` file | ||
[source,console] | ||
mvn clean package | ||
|
||
. Copy the `war` file to your JBoss Web Server `tomcat/webapps` directory | ||
[source,console] | ||
cp target/camel-example-spring-boot.war <your_jws_dir>/tomcat/webapps/ | ||
|
||
. Run JBoss Web Server | ||
[source,console] | ||
sh <your_jws_dir>/tomcat/bin/catalina.sh run | ||
|
||
. Look for the log messages in the console | ||
[source,log] | ||
[- timer://hello] ... Body: insert into messages(message) values('Hello World I am invoked 1 times')] | ||
[- timer://query] ... Body: There are 0 messages in the database.] | ||
[- timer://query] ... Body: There are 1 messages in the database.] | ||
[- timer://hello] ... Body: insert into messages(message) values('Hello World I am invoked 2 times')] | ||
[- timer://query] ... Body: There are 2 messages in the database.] | ||
[- timer://hello] ... Body: insert into messages(message) values('Hello World I am invoked 3 times')] | ||
[- timer://query] ... Body: There are 3 messages in the database.] | ||
[- timer://hello] ... Body: insert into messages(message) values('Hello World I am invoked 4 times')] | ||
[- timer://hello] ... Body: insert into messages(message) values('Hello World I am invoked 5 times')] | ||
[- timer://query] ... Body: There are 4 messages in the database.] | ||
|
||
=== 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! | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 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 sample.camel; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* A bean that returns a message when you call the {@link #saySomething()} method. | ||
* <p/> | ||
* Uses <tt>@Component("myBean")</tt> to register this bean with the name <tt>myBean</tt> | ||
* that we use in the Camel route to lookup this bean. | ||
*/ | ||
@Component("myBean") | ||
public class MyBean { | ||
|
||
private int counter; | ||
|
||
@Value("${greeting}") | ||
private String say; | ||
|
||
public String saySomething(String body) { | ||
return String.format("%s I am invoked %d times", say, ++counter); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
tomcat-jdbc/src/main/java/sample/camel/MyCamelApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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 sample.camel; | ||
|
||
import org.springframework.boot.Banner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
//CHECKSTYLE:OFF | ||
/** | ||
* A sample Spring Boot application that starts the Camel routes. | ||
* | ||
* Having both the {@link #configure} and {@link #main} methods call the same builder ensures the configuration | ||
* will be applied either by starting the application as a .war or by running as an executable. | ||
*/ | ||
@SpringBootApplication | ||
public class MyCamelApplication extends SpringBootServletInitializer { | ||
|
||
@Override | ||
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { | ||
return customizerBuilder(builder); | ||
} | ||
|
||
public static void main(String[] args) { | ||
customizerBuilder(new SpringApplicationBuilder()).run(args); | ||
} | ||
|
||
private static SpringApplicationBuilder customizerBuilder(SpringApplicationBuilder builder) { | ||
return builder.sources(MyCamelApplication.class); | ||
} | ||
|
||
} | ||
//CHECKSTYLE:ON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 sample.camel; | ||
|
||
import org.apache.camel.builder.RouteBuilder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* A simple Camel route that triggers from a timer and calls a bean and prints to system out. | ||
* <p/> | ||
* Use <tt>@Component</tt> to make Camel auto-detect this route when starting. | ||
*/ | ||
@Component | ||
public class MyCamelRouter extends RouteBuilder { | ||
|
||
// we can use spring dependency injection | ||
@Autowired | ||
MyBean myBean; | ||
|
||
@Override | ||
public void configure() throws Exception { | ||
// route to keep inserting messages in the database | ||
// start from a timer | ||
from("timer:hello?period={{myPeriod}}") | ||
.routeId("hello") | ||
// store the message from the bean in a header | ||
.setHeader("message").method(myBean, "saySomething") | ||
// prepare the insert SQL | ||
.setBody(simple("insert into messages(message) values('${header.message}')")) | ||
// insert the message into the database | ||
.to("spring-jdbc:default") | ||
// print the body | ||
.to("log:info"); | ||
|
||
// route to print inserted messages | ||
from("timer:query?period={{myPeriod}}") | ||
.routeId("query") | ||
.setBody(constant("select count(*) as \"C\" from messages")) | ||
.to("spring-jdbc:default") | ||
.setBody(simple("There are ${body[0][C]} messages in the database.")) | ||
.to("log:info"); | ||
} | ||
|
||
} |
Oops, something went wrong.