Skip to content

Commit

Permalink
Merge pull request apache#145 from rinaldodev/jws-example
Browse files Browse the repository at this point in the history
JBoss Web Server example
  • Loading branch information
rinaldodev authored Aug 20, 2024
2 parents 17e8384 + ee22727 commit bcdfdb9
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ readme's instructions.
=== Examples

// examples: START
Number of Examples: 48 (0 deprecated)
Number of Examples: 50 (0 deprecated)

[width="100%",cols="4,2,4",options="header"]
|===
Expand Down Expand Up @@ -65,6 +65,8 @@ Number of Examples: 48 (0 deprecated)

| link:spring-jdbc/readme.adoc[Spring Jdbc] (spring-jdbc) | Beginner | Camel transacted routes integrating local Spring Transaction

| link:tomcat-jdbc/readme.adoc[Tomcat Jdbc] (tomcat-jdbc) | Beginner | An example showing how to deploy a Camel Spring Boot application in Tomcat using its JDBC Data Source

| 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
Expand Down Expand Up @@ -108,7 +110,7 @@ Number of Examples: 48 (0 deprecated)
| link:observation/README.adoc[Micrometer Observation] (observation) | Management and Monitoring | An example showing how to trace incoming and outgoing messages from Camel with Micrometer Observation


| link:opentelemetry/README.adoc[OpenTelemetry] (opentelemetry) | Management and Monitoring | An example showing how to use Camel with OpenTelemetry
| link:opentelemetry/README.adoc[OpenTelemetry] (opentelemetry) | Management and Monitoring | An example showing how to use Camel with OpenTelemetry


| link:supervising-route-controller/readme.adoc[Supervising Route Controller] (supervising-route-controller) | Management and Monitoring | An example showing how to work with Camel's Supervising Route Controller and Spring Boot
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion http-ssl/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ to show the full handshake it is possible to add `-Dspring-boot.run.jvmArguments

== Deploy on OpenShift Container Platform

To run example on OpenShift, please go to link:README-OCP.adoc[the dedicated guide]
To run example on OpenShift, please go to link:OCP.adoc[the dedicated guide]

=== Help and contributions

Expand Down
142 changes: 142 additions & 0 deletions tomcat-jdbc/pom.xml
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>
58 changes: 58 additions & 0 deletions tomcat-jdbc/readme.adoc
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!



40 changes: 40 additions & 0 deletions tomcat-jdbc/src/main/java/sample/camel/MyBean.java
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 tomcat-jdbc/src/main/java/sample/camel/MyCamelApplication.java
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
59 changes: 59 additions & 0 deletions tomcat-jdbc/src/main/java/sample/camel/MyCamelRouter.java
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");
}

}
Loading

0 comments on commit bcdfdb9

Please sign in to comment.