Apache Camel in Spring Boot


Apache Camel is an open-source integration framework that helps developers connect different systems, applications, and services in a flexible and standardized way. It is primarily used for enterprise integration patterns (EIPs), which are common design patterns for integrating systems.

from("file:input").
filter(body().contains("Camel"))
.to("file:output");

1. Add Dependencies

In pom.xml (Maven project), include:

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- Apache Camel Spring Boot -->
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>3.22.2</version> <!-- Use the latest stable version -->
    </dependency>

    <!-- Optional: Camel HTTP Component -->
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-http-starter</artifactId>
    </dependency>
</dependencies>

2. Create Spring Boot Application

package com.example.camelapp;

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

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

3. Create a Camel Route

Camel routes define how messages move between endpoints.

package com.example.camelapp.routes;

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

@Component
public class MyCamelRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        // Simple timer route: prints message every 5 seconds
        from("timer:hello?period=5000")
            .log("Hello from Apache Camel!");

        // Example: HTTP call
        from("timer:fetchData?period=10000")
            .to("https://jsonplaceholder.typicode.com/todos/1")
            .log("${body}");
    }
}

Explanation:

  • timer:hello?period=5000 triggers every 5 seconds.
  • .log("...") prints messages to the console.
  • Second route fetches JSON from a REST API every 10 seconds and logs the body.

4. Run the Application

Run CamelAppApplication as a Spring Boot app. You’ll see logs in the console like:

Hello from Apache Camel!
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

5. Optional: Expose a REST Endpoint via Camel

from("rest:get:/hello")
    .setBody(constant("Hello from Camel REST!"));

With this, visiting http://localhost:8080/hello returns:

Hello from Camel REST!

✅ Summary:

  • Spring Boot manages app lifecycle.
  • Camel handles routing and integration.
  • You can integrate files, databases, REST APIs, or JMS with minimal code.

Leave a Reply