RouteBuilder is the base class you extend in Apache Camel to define routes in the Java DSL. You override its configure() method and describe where messages come from (from(...)), how they are processed, and where they go (to(...)). Camel discovers the builder, adds its routes to the CamelContext, and starts them.
This article targets Camel 4, which requires Java 17 or later and uses the jakarta.* namespace. The RouteBuilder API is the same in Camel 3 apart from those platform changes.
A route is a pipeline: a consumer endpoint receives a message, processors transform or filter it, and a producer endpoint sends it on. RouteBuilder is the factory that turns Java method calls into those route definitions.
org.apache.camel.builder, and configure() is its only abstract method.configure() runs once, when the builder is added to the context, not once per message.This route polls data/inbox, logs each file name, and writes the file to data/outbox. The option noop=true leaves the original in place, so Camel remembers processed files in memory to avoid reading them again.
With camel-spring-boot-starter on the classpath, any RouteBuilder that is a Spring bean is picked up automatically. Setting .routeId(...) gives the route a stable name for logs, JMX, metrics and tests.
The DSL covers the common Enterprise Integration Patterns. Content-based routing uses choice():
Other frequently used steps:
| Step | What it does |
|---|---|
filter(predicate) | Drops messages that do not match. |
process(exchange -> ...) | Runs custom Java code on the exchange. |
transform(expression) | Replaces the message body. |
split(expression) | Breaks one message into many. |
unmarshal() / marshal() | Converts between formats such as JSON and objects. |
to(uri) / toD(uri) | Sends to a fixed or dynamic endpoint. |
Endpoints are URIs whose scheme names a component, for example file:, kafka:, jms:, sql:, timer: and platform-http:. A consumer that only exposes an HTTP endpoint should use a server-capable component. The http: component in Camel is a producer only, so from("http://localhost:8080/start"), which appears in many older examples, does not work. Use platform-http on Spring Boot instead:
Spring Boot serves /start through its own web server, and the final message body becomes the HTTP response. Each component lives in its own camel-*-starter dependency, so add only what you use.
Error handling is declared at the top of configure(), before the routes it applies to, so it covers every route in that builder. Placing onException inside a route, as some older snippets do, is unreliable and harder to read.
After three redeliveries one second apart, the exception is marked as handled and the file goes to data/failed. For messaging routes, errorHandler(deadLetterChannel("jms:queue:dead")) sends failed messages to a dead letter queue. doTry().doCatch(...) handles errors inside a single route.
The CamelContext is the runtime that holds routes, components, type converters and registries. A RouteBuilder contributes route definitions to it, and the context controls their lifecycle. In Spring Boot, the context starts with the application and stops routes gracefully on shutdown.
Without Spring Boot you add a builder yourself:
For production standalone apps, Camel Main or Camel JBang manage startup and shutdown for you.
Camel's test support (camel-test-spring-junit5) lets you replace endpoints and assert on messages. In a Spring Boot test you can use AdviceWith to swap a real endpoint for a mock, then send a message with ProducerTemplate and verify the output.
No, but it is the standard way. You can also write routes in XML or YAML, or call context.addRoutes(...) with a RouteBuilder instance created in code.
Yes. Call from(...) several times inside configure(), and give each route its own routeId.
Once, when the builder is added to the CamelContext. It defines the routes; it does not run per message.
Yes. Camel 3.12 and later offer RouteConfigurationBuilder, which lets you define shared error handling and other configuration and apply it to chosen routes.
RouteBuilder is where Camel routes are written in Java. Override configure(), start routes with from(), add processing steps, and finish with to(). Declare onException at the top of the method, pick a consumer-capable component such as platform-http for inbound HTTP, and let Spring Boot register the builder as a bean.