Spring Boot 3 + Apache Camel Hello World Example(2023)

Spring Boot 3 + Apache Camel Hello World Example(2023)


In previous tutorial we looked at what is Apache Camel. In this tutorial we will be implementing a hello world example using spring boot and apache camel. We will be implementing a simple example to move a file from one folder location to another.

Implementation

Go to the Spring Boot Initializer website and create a new spring boot project as follows- Spring Boot Apache Camel Initializer Our final maven project will be as follows -
Spring Boot Apache Camel Maven
The pom.xml will have the following dependencies-
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.3-SNAPSHOT</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javastructures</groupId>
	<artifactId>camel-boot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>camel-boot</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.camel.springboot</groupId>
			<artifactId>camel-spring-boot-starter</artifactId>
			<version>4.0.0-M1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>

</project>
Next we will be creating an apache camel route. All logic/flow in apache camel is implemented using routes.

Apache Camel Route

Apache Camel routes are a way of connecting endpoints together in a meaningful and efficient way. A route can start from any endpoint, go through multiple processing steps, and then end either with another endpoint or a terminal step. You can also set up filters and rules to only allow certain messages or types of data in your route. Apache Camel Route

Apache Camel Component

Apache Camel Routes are used to connect different Components together. A route often starts with a Component, which is responsible for creating a message. The message then passes through the Route, where it can be processed, modified, or routed to a different Component. The end result is usually an output from another Component that is sent to a final endpoint.
As we have to transfer file from one location to another so we will be making use of File Component. Apache Camel Component So let us create the HelloWorldRouteBuilder class which extends the RouteBuilder class. RouteBuilder is a class from the Apache Camel library that allows us to build a route from a source to a destination. It provides methods to define the source and destination, as well as any necessary processing logic. We have used the RouteBuilder to create a route from 'C://input' to 'C://output'.
package com.javastructures.camelboot;

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

@Component
public class HelloWorldRouteBuilder extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("file:C://input").to("file:C://output");
	}

}
We are done with the changes. Start the Spring Boot Application and we can see that 1 route has been started. All files from input folder will be automatically copied to the output folder.