What is Apache Camel? Let's figure it out in practice with Java code
\n\nHave 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\nIf 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\nThe problem that Apache Camel solves
\nImagine: 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
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.
\nApache 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\nBasic architecture: routes and components
\nAt 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.
\nExample of a simple route:
\nfrom("file:data/input?noop=true")\n .log("Received file: ${header.CamelFileName}")\n .to("file:data/output");\n\nHere 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.
Code breakdown: from file to database
\nSuppose 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:
\nimport 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\nWhat 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
This is an Apache Camel example in action: minimum code, maximum functionality.
\n\nIntegration Patterns (EIP) — Camel's secret weapon
\nOne 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.
\nExample of a Content-Based Router:
\nfrom("direc