This example shows how to work with a simple Apache Camel Spring Boot application deploying to Tomcat 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.
-
MyCamelRouter: where the camel routes are defined -
MyBean: used to generate a simple message with a counter -
MyCamelApplication: the Spring Boot servlet initializer
-
Start the PostgreSQL server
-
Use camel-jbang
-
In camel-jbang there is a camel infra command to start services.
To start the postgresql server
camel infra run postgresIt will output this:
Starting service postgres
{
"getServiceAddress" : "localhost:5432",
"host" : "localhost",
"password" : "test",
"port" : 5432,
"userName" : "test"
}
Press any key to stop the execution|
Note
|
If you use camel infra run, make sure the credentials in src/main/resources/application.properties match the ones reported by the service (e.g. spring.datasource.username=test and spring.datasource.password=test).
|
-
Use docker
You can use docker in case you don’t want to use camel-jbang.
docker run --rm --name db -e POSTGRES_PASSWORD=password -p 5432:5432 docker.io/library/postgres:latest-
Run the tests and package the
warfilemvn clean package -
Copy the
warfile to your tomcatwebappsdirectorycp target/camel-example-spring-boot.war <your_tomcat_dir>/webapps/ -
Run tomcat
sh <your_tomcat_dir>/bin/catalina.sh run -
Look for the log messages in the console
[- 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.]
If you hit any problem using Camel or have some feedback, then please let us know.
We also love contributors, so get involved :-)
The Camel riders!