[JAVA] Hello World (console app) with Apache Camel + Spring Boot 2

Introduction

Create a console application that outputs "Hello World" every second with the following combination.

The REST API edition is as follows.

Hello World with Apache Camel + Spring Boot 2 (REST API)

What is Apache Camel?

I don't think Spring Boot needs explanation, so I will explain only Apache Camel briefly.

Apache Camel is often described as middleware (framework) for integration between systems. Integration between systems has a best practice called Enterprise Integration Patterns, which Camel makes it easy. Writing like this sounds a little difficult, and I feel that the threshold is unnecessarily high. In reality, you can think of it as a framework that makes it easy to "receive a request from another system, perform some processing, and send / store (DB or queue) somewhere." REST / SOAP, database (JDBC, MongoDB, DynamoDB, etc), file, message queue (ActiveMQ, Kafka, RabbitMQ, etc), mail, MQTT, etc. can be linked with various things, and parts to realize it Is called a component. There are nearly 200 components, and you can usually find the one you want to connect to. The "do some processing" part also covers functions such as data conversion, format conversion, routing, and error handling, and is a framework that can be used in any application.

Create a project in Eclipse

First, create a new Maven project. Select "Maven Project" and click "Next".

image.png

"Next" without any particular changes.

image.png

Select "maven-archetype-quickstart" and click "Next".

image.png

Enter the project information and click "Finish".

image.png

This completes the project creation.

Modify pom.xml

Modify pom.xml as follows.

pom.xml


<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>mkyz08.sample</groupId>
	<artifactId>camel-springboot-test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>camel-springboot-test</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.source>${java.version}</maven.compiler.source>
		<camel-version>2.23.0</camel-version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.apache.camel</groupId>
				<artifactId>camel-parent</artifactId>
				<version>${camel-version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-stream</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Create a program

Create the main program as follows.

Application.java


package mkyz08.example;

import org.apache.camel.spring.boot.CamelSpringBootApplicationController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new SpringApplication(Application.class).run(args);
        CamelSpringBootApplicationController applicationController =
                applicationContext.getBean(CamelSpringBootApplicationController.class);
        applicationController.run();
    }

}

The following is a normal Spring Boot application launch.

ApplicationContext applicationContext = new SpringApplication(Application.class).run(args);

The following is the difference from starting a normal Spring Boot application.

You can continue execution as a daemon thread by executing the run method of the CamelSpringBootApplicationController class. If you don't do this, the console app will start and exit.

        CamelSpringBootApplicationController applicationController =
                applicationContext.getBean(CamelSpringBootApplicationController.class);
        applicationController.run();

The configuration file is as follows. By setting "spring.main.web-application-type = none", Tomcat will not start. "Timer.period" is an application setting and is used as a setting value when Hello World is displayed every second.

src/main/resources/application.properties


camel.springboot.name = MyCamelApp
spring.main.web-application-type=none
timer.period = 1000

Finally, create a Camel route. Because of Camel's route definition, Component annotation is added to the class and it inherits RouteBuilder class. Then override the configure method and code the contents of the route.

HelloWorldRoute.java


package mkyz08.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class HelloWorldRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
		from("timer://exampleTimer?period={{timer.period}}")
				.routeId("hello world route")
				.setBody(simple("Hello World at ${header.firedTime}"))
				.to("stream:out");
    }
}

Below is the content of the route

--In "timer: //exampleTimer?period={{timer.period}}", define a timer to be executed every second. --". RouteId (" hello world route ")", which only specifies the route ID --".SetBody (simple (" Hello World at $ {header.firedTime} "))", set Hello World string to BODY of Exchange --The Hello World character string in the Exchange BODY is output to the console with ".to (" stream: out ")".

		from("timer://exampleTimer?period={{timer.period}}")
				.routeId("hello world route")
				.setBody(simple("Hello World at ${header.firedTime}"))
				.to("stream:out");

Run the Hello World console application

When you execute the created program, it will be output to the console as shown below.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-12-06 14:59:46.472  INFO 17612 --- [           main] mkyz08.example.Application               : Starting Application on mky-PC with PID 17612 (C:\pleiades\workspace\camel-springboot-test\target\classes started by mky in C:\pleiades\workspace\camel-springboot-test)
2018-12-06 14:59:46.474  INFO 17612 --- [           main] mkyz08.example.Application               : No active profile set, falling back to default profiles: default
2018-12-06 14:59:47.516  INFO 17612 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boot.CamelAutoConfiguration$$EnhancerBySpringCGLIB$$9ed7a086] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-12-06 14:59:47.731  INFO 17612 --- [           main] o.a.c.i.converter.DefaultTypeConverter   : Type converters loaded (core: 195, classpath: 1)
2018-12-06 14:59:48.080  INFO 17612 --- [           main] o.a.camel.spring.boot.RoutesCollector    : Loading additional Camel XML routes from: classpath:camel/*.xml
2018-12-06 14:59:48.080  INFO 17612 --- [           main] o.a.camel.spring.boot.RoutesCollector    : Loading additional Camel XML rests from: classpath:camel-rest/*.xml
2018-12-06 14:59:48.086  INFO 17612 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.23.0 (CamelContext: MyCamelApp) is starting
2018-12-06 14:59:48.086  INFO 17612 --- [           main] o.a.c.m.ManagedManagementStrategy        : JMX is enabled
2018-12-06 14:59:48.274  INFO 17612 --- [           main] o.a.camel.spring.SpringCamelContext      : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2018-12-06 14:59:48.385  INFO 17612 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: hello world route started and consuming from: timer://exampleTimer?period=1000
2018-12-06 14:59:48.387  INFO 17612 --- [           main] o.a.camel.spring.SpringCamelContext      : Total 1 routes, of which 1 are started
2018-12-06 14:59:48.390  INFO 17612 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.23.0 (CamelContext: MyCamelApp) started in 0.302 seconds
2018-12-06 14:59:48.394  INFO 17612 --- [           main] mkyz08.example.Application               : Started Application in 2.316 seconds (JVM running for 2.825)
Hello World at Thu Dec 06 14:59:49 JST 2018
Hello World at Thu Dec 06 14:59:50 JST 2018
Hello World at Thu Dec 06 14:59:51 JST 2018

Create an executable jar file

To create an executable jar file, enter "clean install spring-boot: repackage" in the goal in "Execution Configuration" and execute it as shown below.

image.png

If the execution is successful, the following log will be output.

[INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:repackage (default-cli) @ camel-springboot-test ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.930 s
[INFO] Finished at: 2018-12-06T13:03:00+09:00
[INFO] ------------------------------------------------------------------------

The executable jar file is created in the "target" and directory of the project, and can be executed with the following command.

java -jar camel-springboot-test-0.0.1-SNAPSHOT.jar

reference

-Apache Camel official website Spring Boot

-Apache Camel official website Spring Boot sample

Recommended Posts

Hello World (console app) with Apache Camel + Spring Boot 2
Hello World (REST API) with Apache Camel + Spring Boot 2
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with Spring Boot
Until "Hello World" with Spring Boot
(IntelliJ + gradle) Hello World with Spring Boot
Hello world! With Spring Boot (Marven + text editor)
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Spring Boot Hello World in Eclipse
Create an app with Spring Boot 2
Create an app with Spring Boot
Create a Hello World web app with Spring framework + Jetty
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
[Practice! ] Display Hello World in Spring Boot
How Spring Security works with Hello World
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Steps to create a simple camel app using Apache Camel Spring Boot starters
Create a simple search app with Spring Boot
Hello World at explosive speed with Spring Initializr! !! !!
Try to display hello world with spring + gradle
Hello World with Micronaut
Download with Spring Boot
Sample to batch process data on DB with Apache Camel Spring Boot starters
Message cooperation started with Spring Boot Apache Kafka edition
Create Restapi with Spring Boot ((1) Until Run of App)
Hello World comparison between Spark Framework and Spring Boot
Generate barcode with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
Hello World with VS Code!
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Hello World with SpringBoot / Gradle
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Circuit Breaker Pattern with Apache Camel
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Hello world with Kotlin and JavaFX
Hello World with Docker and C
Get validation results with Spring Boot
Check date correlation with Spring Boot
Hello World with GlassFish 5.1 + Servlet + JSP
I tried GraphQL with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
"Hello world" for ImageJ with Eclipse
I tried Flyway with Spring Boot
Hello World with GWT 2.8.2 and Maven
Message cooperation started with Spring Boot