Open In App

Spring Boot – Using Spring Boot with Apache Camel

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Apache Camel and Spring Boot are two powerful frameworks that can be seamlessly integrated to build robust, scalable, and efficient applications. Apache Camel is an open-source integration framework that provides an extensive range of components and connectors, enabling developers to integrate different systems, APIs, and applications. It simplifies complex enterprise integration patterns, allowing developers to focus more on business logic rather than the intricacies of integration.

This article will guide you on how to use Apache Camel within a Spring Boot application. We will walk through setting up a simple Camel route using Spring Boot, demonstrating how to leverage the strengths of both frameworks for effective application integration.

Apache Camel

Apache Camel is an open-source integration framework that enables developers to integrate various systems, APIs, and applications using a variety of protocols and technologies. It offers a set of predefined patterns and connectors to handle integration logic efficiently.

Use Apache Camel with Spring Boot

Combining Apache Camel with Spring Boot offers a powerful toolkit for developing robust, scalable, and maintainable integration solutions. Camel's routing capabilities and Spring Boot's auto-configuration support work together to create a seamless development experience.

Camel Route

A Camel route defines the flow of messages between different components in an application. Routes are defined using domain-specific languages (DSL) like Java, XML, or YAML. Camel provides various components like Direct, File, HTTP, JMS, and more for routing messages.

Integration Patterns

Camel provides various enterprise integration patterns (EIPs) such as message routing, filtering, transformation, and aggregation. These patterns help in solving complex integration problems efficiently.

Camel Context

The Camel context is the runtime environment where the routes are executed. It acts as the central point of configuration, allowing developers to manage routes, components, and services.

Implementation of Spring Boot with Apache Camel

Step 1: Create a New Spring Boot Project

Create a new Spring Boot project using IntelliJ IDEA. Choose the following options:

  • Name: spring-boot-camel-integration
  • Language: Java
  • Type: Maven
  • Packaging: Jar

Click on the "Next" button.

Project Metadata

Step 2: Add Dependencies

Add the following dependencies into the Spring Boot project.

Add Dependencies

Project Structure

Once the project is created, the file structure will look like this:

Project Folder Structure

Step 3: Configure Application Properties

Open the application.properties file and add the following configuration:

# Application name
spring.application.name=spring-boot-camel-integration

# Log level for Apache Camel
logging.level.org.apache.camel=INFO

# Log file name
logging.file.name=application.log

Step 4: Define the Camel Route

Create a Java file named FileMoveRoute.java. This class defines a simple route that moves files from one directory to another in the Spring Boot application.

Java
package com.gfg.springbootcamelintegration;

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

/**
 * FileMoveRoute class defines a simple Camel route that moves files
 * from a source directory to a destination directory.
 */
@Component
public class FileMoveRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        // Define the route starting from a direct component
        from("direct:fileMoveRoute")
            .log("Processing file: ${file:name}") // Log the name of the file being processed
            .to("file:C:/Users/Syam/OneDrive/Desktop/Testing Folder/destination?fileName=${file:name.noext}.bak"); // Move the file to the destination directory and rename it with a .bak extension
    }
}
  • The FileMoveRoute class extends RouteBuilder and overrides the configure() method to define the route.
  • The route starts from the direct:fileMoveRoute endpoint, logs the file name being processed, and moves the file to the specified destination directory.

Step 5: Create the Controller to Trigger the Camel Route

Create a Java file named FileController.java. This controller exposes a REST endpoint to trigger the Camel route.

Java
package com.gfg.springbootcamelintegration;

import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * FileController class provides a REST endpoint to trigger the Camel route.
 */
@RestController
public class FileController {

    @Autowired
    private ProducerTemplate producerTemplate; // Inject ProducerTemplate to send messages to Camel routes

    /**
     * This endpoint starts the file move process by triggering the Camel route.
     *
     * @return String message indicating the start of the process
     */
    @GetMapping("/startFileMove")
    public String startFileMove() {
        producerTemplate.sendBody("direct:fileMoveRoute", "Sample content"); // Trigger the Camel route with sample content
        return "File move process started!";
    }
}
  • The FileController class is annotated with @RestController, making it a Spring MVC controller.
  • It uses ProducerTemplate to send messages to the Camel route.
  • The startFileMove() method triggers the Camel route by sending a message to the direct:fileMoveRoute endpoint.

Step 6: Main class

No changes are required in the main class. This is the entry point of the Spring Boot application.

Java
package com.gfg.springbootcamelintegration;

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

/**
 * SpringBootCamelIntegrationApplication class serves as the entry point of the Spring Boot application.
 */
@SpringBootApplication
public class SpringBootCamelIntegrationApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCamelIntegrationApplication.class, args); // Start the Spring Boot application
    }
}

pom.xml File:

XML
<?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.2.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gfg</groupId>
    <artifactId>spring-boot-camel-integration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-camel-integration</name>
    <description>spring-boot-camel-integration</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Step 7: Run the Application

Once the project is completed, you can start the Spring Boot application. The application will run on port 8080.

Application Starts

Step 8: Testing the Application

To test the application, open a web browser or use a tool like Postman to send a GET request to the following URL:

http://localhost:8080/startFileMove

Output:

postman ui

Verify the Output

After sending the request, check the destination directory to verify that the file has been moved and renamed with a .bak extension.

verify output

Conclusion

Using Apache Camel with Spring Boot provides a powerful, flexible, and scalable solution for building integration and message routing applications. With Camel's rich set of components and Spring Boot's auto-configuration capabilities, developers can easily implement complex routing logic and integrate various systems. This tutorial demonstrated how to set up a simple file-moving route, but Apache Camel offers much more, including support for complex enterprise integration patterns.


Next Article

Similar Reads