Apache Camel: What is it and why use it? Code breakdown in Java

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Apache Camel? Let's figure it out in practice with Java code

\n\n

Have you ever wondered how large systems exchange data? One writes logs to a file, another expects JSON over HTTP, a third works with RabbitMQ queues. Connecting them directly means writing a mountain of "glue" code: parsers, adapters, error handlers. This is where Apache Camel comes onto the scene — a powerful integration framework that takes care of all the routine.

\n\n

If you are looking for an explanation of Apache Camel with real Java examples, you've come to the right place. We will break down how routes work, why EIP (Enterprise Integration Patterns) are needed, and why Camel is not just another ORM, but a Swiss Army knife for integration.

\n\n

The problem that Apache Camel solves

\n

Imagine: you have three services:

\n
    \n
  • Service A provides data in CSV files via FTP.
  • \n
  • Service B only accepts XML over HTTP.
  • \n
  • Service C works with Kafka.
  • \n
\n

Without an integration framework, you would have to write a separate connector for each pair. And if tomorrow AMQP or MongoDB gets added? The code will bloat, and tests will become a nightmare.

\n

Apache Camel solves this simply: you describe routes in Java or XML, and Camel itself connects the necessary components (FTP, HTTP, Kafka, JMS, and hundreds of others). You don't need to know the protocol details — Camel does everything "under the hood".

\n\n

Basic architecture: routes and components

\n

At the core of Camel lies the concept of a route. This is a chain of steps: from the source (from) to the recipient (to). Between them, there can be transformations, filters, and data enrichment.

\n

Example of a simple route:

\n
from("file:data/input?noop=true")\n    .log("Received file: ${header.CamelFileName}")\n    .to("file:data/output");\n
\n

Here we read files from the data/input folder (with the noop=true option to not delete the sources) and copy them to data/output. Camel itself tracks changes, locks, and errors.

\n\n

Code breakdown: from file to database

\n

Suppose you need to read CSV from a folder, parse the lines, and save them to PostgreSQL. Without Camel, you would write FileWatcher + OpenCSV + JDBC. With Camel — a couple of lines:

\n
import org.apache.camel.builder.RouteBuilder;\n\npublic class CsvToDbRoute extends RouteBuilder {\n    @Override\n    public void configure() throws Exception {\n        from("file:data/csv?delay=5000")\n            .unmarshal().csv() // parse CSV into a list of lists\n            .split(body()) // process each line\n            .process(exchange -> {\n                // Convert the line into an SQL query\n                List<String> row = exchange.getIn().getBody(List.class);\n                String sql = String.format("INSERT INTO users (name, age) VALUES ('%s', %s)", row.get(0), row.get(1));\n                exchange.getIn().setBody(sql);\n            })\n            .to("jdbc:myDataSource?useHeadersAsParameters=true");\n    }\n}\n
\n

What is happening here?

\n
    \n
  • unmarshal().csv() — built-in CSV parser.
  • \n
  • split(body()) — splits the set of lines into individual messages.
  • \n
  • process() — custom logic (conversion to SQL).
  • \n
  • to("jdbc:myDataSource") — executing the query via the JDBC component.
  • \n
\n

This is an Apache Camel example in action: minimum code, maximum functionality.

\n\n

Integration Patterns (EIP) — Camel's secret weapon

\n

One of the main features of Camel is the implementation of Enterprise Integration Patterns (EIP). These are proven architectural solutions: Content-Based Router, Splitter, Aggregator, Wire Tap, and others.

\n

Example of a Content-Based Router:

\n
from("direc                

Blogs

Book Recommendations