Apache Camel

Language: Java

Integration / Enterprise Application

Apache Camel was created to simplify system integration by providing a standard way to define routes and transformations. Developers can use Camel to connect databases, message brokers, REST APIs, and other systems with minimal boilerplate, making it popular in enterprise applications.

Apache Camel is a versatile open-source integration framework that provides routing and mediation rules for connecting different systems. It implements Enterprise Integration Patterns (EIPs) and supports hundreds of components for various protocols and data formats.

Installation

maven: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>3.23.0</version> </dependency>
gradle: implementation 'org.apache.camel:camel-core:3.23.0'

Usage

Camel allows you to define routes using Java DSL, XML DSL, or Spring Boot integration. It supports message transformation, filtering, aggregation, and protocol bridging, including HTTP, JMS, Kafka, File, and more.

Simple route from file to console

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() {
        from("file:input").to("stream:out");
    }
});
context.start();
Thread.sleep(5000);
context.stop();

Reads files from the 'input' directory and prints their contents to the console.

HTTP to file route

from("jetty:http://0.0.0.0:8080/hello")
    .to("file:output");

Accepts HTTP requests and writes request bodies to files.

Using processors for custom transformations

from("file:input")
    .process(exchange -> {
        String body = exchange.getIn().getBody(String.class);
        exchange.getMessage().setBody(body.toUpperCase());
    })
    .to("file:output");

Applies custom transformation logic using a Processor before sending messages.

Routing with conditions

from("file:input")
    .choice()
        .when(header("CamelFileName").endsWith(".txt")).to("file:txtOutput")
        .otherwise().to("file:otherOutput");

Routes files to different destinations based on file extension using content-based routing.

Integration with Kafka

from("kafka:my-topic?brokers=localhost:9092")
    .to("file:output");

Consumes messages from a Kafka topic and writes them to a file.

Error handling

onException(Exception.class)
    .handled(true)
    .log("Error occurred: ${exception.message}");

Handles exceptions in routes and logs the error message without stopping the route.

Error Handling

org.apache.camel.CamelExecutionException: Occurs when a route fails during execution. Check route configuration, endpoints, and processors.
Connection refused: Ensure target systems (HTTP, JMS, Kafka, etc.) are reachable and listening on correct ports.
FileNotFoundException: Verify source and destination directories exist and have proper permissions.

Best Practices

Use Camel components appropriate for your protocols and systems.

Define routes using Java DSL for easier IDE support and refactoring.

Apply content-based routing and filtering for clean message flows.

Leverage error handling and dead-letter channels for robust integration.

Monitor routes using JMX or Camel’s management tools.