Spring Boot + Apache Camel Hello World Example | CodeUsingJava








Spring Boot + Apache Camel Hello World Example


Overview

In this tutorial we will be implementing Spring Boot + Apache Camel Hello World Example to copy files from one location to another. In a previous tutorial we had looked at what is Enterprise Application Integration and how Apache Camel helps achieve it. In this tutorial we will have a brief look at why we need to use Apache Camel in microservices architecture. What benefits it provides.

Table Of Contents :


Video Tutorial

Need for Apache Camel with Spring Boot Microservices

Usually for systems developed using Microservices architecture, there are many microservices involved. These enterprise services need to be integrated with each other.
Apache Camel Microservices Architecture
These microservices communicate with each other using a variety of Transport protocols like http, JMS etc. Gregor Hohpe and Bobby Woolf wrote a book -"Enterprise Integration Patterns" which list all the good practices that should be followed when implementing Enterprise Integration Frameworks. Camel supports most of the Enterprise Integration Patterns mentioned in this book.

Implementation

Go to Spring Initializr website and select the following dependencies -
  • Web
  • Camel
  • Actuator
  • Dev Tools

Apache Camel Spring Boot
Download the maven project. The Maven project we will be developing is as follows -
Apache Camel Spring Boot Maven
The pom.xml is as follows -
<?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>2.4.3</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.codusingjava</groupId>
	<artifactId>spring-boot-camel</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-camel</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</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>3.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>

</project>


What are Routes?

Route is the most important unit of Apache Camel. Using Routes we define the message flow and integration logic for our application.
Apache Camel Routes can be written in various Domain Specific Languages(DSL). Most popular are-
  • Java DSL - A Java based DSL
  • Spring XML - A XML based DSL in Spring XML files
For this example we make use of Java DSL for writing Routes. A Route consists of Message Channel using which applications communicate using Messages. The end points of these message channels either consume of produce messages.

Apache Camel Endpoints

What are Components?

Component act as an endpoint factory using which we can interact with external systems. Camel provides a large number of components using which we can interact with externals systems.
For example here we have to transfer files from one folder to another, so we make use of the file component at both the end of the message channel.

Apache Camel File Copy Example
If suppose we have to transfer a file to a JMS Queue, then we make use of the File Component at one end and JMS Component at the other end.
Apache Camel Components
So let us create Apache Camel route in our Spring Boot Project. In Apache Camel for creating a route we need to extend RouteBuilder class and override configure method. Then in configure method we define our route. So our route using Java DSL will be as follows-
package com.codeusingjava.springbootcamel.route;

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

@Component
public class SimpleRouteBuilder extends RouteBuilder {

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

Spring IO has already created the Spring Boot main class for us. Start the Spring Boot Application by running it as a java application.
package com.codeusingjava.springbootcamel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootCamelApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootCamelApplication.class, args);
	}

}
The Route will get executed to copy all content from folder input to the folder output. After the route execution is complete, the file is deleted from the source folder. If you do not want this, then noop=true should be specified. This will copy the files from one location to another without deleting it from the source folder.
package com.codeusingjava.springbootcamel.route;

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

@Component
public class SimpleRouteBuilder extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("file:C://folder1?noop=true").to("file:C://folder2");
	}
}

Downloads-

Spring Boot + Apache Camel Hello World Example