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.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.23.0</version>
</dependency>implementation 'org.apache.camel:camel-core:3.23.0'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.
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.
from("jetty:http://0.0.0.0:8080/hello")
.to("file:output");Accepts HTTP requests and writes request bodies to files.
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.
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.
from("kafka:my-topic?brokers=localhost:9092")
.to("file:output");Consumes messages from a Kafka topic and writes them to a file.
onException(Exception.class)
.handled(true)
.log("Error occurred: ${exception.message}");Handles exceptions in routes and logs the error message without stopping the route.
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.