I wanted to make a new simple app to verify the operation of the app made with ** Apache Camel **, but I made the Java system from scratch. I don't know what to do because there is almost no.
For the time being, following the explanation on the official page, I made a camel app that has a route that logs every second. I will not verify the startup of camel, so use ** Camel Spring Boot starters ** that seems to be easy to make. It was.
I somehow know that pom.xml
is needed for maven, but I don't know how to write it at this point. Since the only thing that appears on camel's page is how to write dependencies, we have to prepare an outer frame.
I couldn't help it, so I made a new project with IDE (IntelliJ IDEA).
Maven
from the list on the left1.8
for the time beingmycamelapp
Just in case, set up Git and save your work. I decided to use .gitignore
because there was a template collection on GitHub.
terminal
cd path/to/app
git init
for kind in Java Maven Global/JetBrains; do
echo "###--- github/gitignore : ${kind}.gitignore ---###"
curl https://raw.githubusercontent.com/github/gitignore/master/${kind}.gitignore
echo
done >> .gitignore
git add .
git commit -m 'Create a Maven project'
pom.xml
Add dependencies according to https://camel.apache.org/camel-spring-boot/latest/index.html. It took time without realizing that dependencies
had to be written separately inside and outside dependencyManagement
.
The whole is as follows.
pom.xml (mycamelapp)
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mycamelapp</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://camel.apache.org/camel-spring-boot/latest/index.html -->
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- ... other BOMs or dependencies ... -->
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Camel Starter -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<!-- ... other dependencies ... -->
</dependencies>
</project>
ActiveMQ is introduced as an example of the component to be added to other dependencies, but I skip it because I do not plan to use it.
I've seen a lot of examples here with the app under maintenance, so I don't have any doubts personally. https://camel.apache.org/camel-spring-boot/latest/spring-boot.html#SpringBoot-Camel Define a short route at SpringBootStarter.
terminal
cd path/to/app
mkdir -p src/main/java/org/example/mycamelapp/routes
touch src/main/java/org/example/mycamelapp/routes/SampleTimerRoute.java
src/main/java/org/example/mycamelapp/routes/SampleTimerRoute.java
package org.example.mycamelapp.routes;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class SampleTimerRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:foo").to("log:bar");
}
}
Roughly speaking, ** pipeline **. Imagine Java8's Stream API, but it's flowing one after another. I write in DSL how to process the incoming data.
from (uri)
is the data entry.timer
or poll the file with file
.to (uri)
is the data destination.sql
to make a query to the DB or http
to make an HTTP request.I couldn't find it in camel's documentation, but it seems that I have to write the starting point of the app. When you write it, the "Run" mark is displayed on the corresponding line in the IDE.
src/main/java/org/example/mycamelapp/MyCamelApplication.java
package org.example.mycamelapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyCamelApplication {
public static void main(String[] args) {
SpringApplication.run(MyCamelApplication.class, args);
}
}
Prevent the app from closing immediately after launching. It seems that you only need to give parameters here, so write it in yaml (may be properties).
src/main/resources/application.yml
# to keep the JVM running
camel:
springboot:
main-run-controller: true
On the IDE, you can start it immediately by pressing the "Run" button mentioned above.
If you start it with maven command, add the setting to pom.xml.
pom.xml (mycamelapp)
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
</plugin>
</plugins>
</build>
...
terminal
cd path/to/app
mvn spring-boot:run
Recommended Posts