In Apache Camel, from() defines where a route starts and to() defines where a message is sent next. from() creates a consumer on an endpoint URI (a file directory, a JMS queue, an HTTP listener, a timer), and every message it receives becomes an Exchange that flows through the route. to() is a producer step that sends the current exchange to another endpoint and, for request-reply endpoints, puts the reply back on the exchange.
Together they express the basic shape of any integration: receive, process, deliver.
A route is declared in the configure() method of a RouteBuilder. The first call must be from(), and each route has exactly one. The argument is an endpoint URI made of a component name, a resource and options.
Here Camel's file component polls data/inbox, creates an exchange for each matching file and runs the steps after from(). By default the file component moves processed files into a .camel subdirectory, so they are not read twice. Set noop=true or move= options to change that.
Because from() is a consumer, its behaviour depends on the component: timer: fires on a schedule, kafka: reads a topic, platform-http: serves HTTP requests, direct: is an in-memory entry point you call from other routes.
to() sends the exchange to an endpoint. It can appear many times in one route:
The first to() writes the file to data/outbox. The second logs it. Steps run in order on the same exchange, so each to() sees what the previous step left behind.
The exchange pattern decides what happens to the response. With InOnly, which is typical for queues and files, the message is delivered and the route continues with the same body. With InOut, which is typical for HTTP and some bean calls, the reply replaces the message body. You can force a pattern with .inOnly(uri) or .inOut(uri).
When the destination depends on the message, use toD() with the Simple language rather than building strings by hand:
Most real routes place processing between the two. Camel's Java DSL reads top to bottom.
This is the content-based router pattern. Other common steps between from() and to() are filter(), transform(), process(), split(), aggregate() and enrich().
| Method | Role |
|---|---|
from(uri) | Starts the route, consumes messages |
to(uri) | Sends the exchange to an endpoint |
toD(uri) | Sends to a dynamically computed endpoint |
routeId(id) | Names the route for monitoring and control |
choice(), filter() | Decide which messages continue and where they go |
Current Camel releases (Camel 4) use Jakarta EE and require a modern JDK. With camel-spring-boot-starter plus the components you need, any RouteBuilder registered as a Spring bean starts with the application. You can also use camel-main or Quarkus. The same from() and to() API works in each.
from() to a route. Define a new route instead, and link routes with direct: or seda: endpoints.to() to change the body for one-way endpoints. Use transform() or setBody() explicitly.onException() or errorHandler() so failed exchanges are retried or sent to a dead letter endpoint.{{kafka.brokers}}.No. Each route has one consumer. Use several routes, and connect them through direct:, seda: or a messaging endpoint.
to() uses a fixed URI. toD() evaluates the URI per message, so the destination can depend on headers or the body.
Only when the endpoint uses an in-out pattern. For one-way endpoints the original body continues unchanged.
A consumer. Producers are the to(), toD() and enrich() family.
from() begins every Camel route by consuming messages from an endpoint, and to() delivers the exchange to another endpoint. Between them you add routing, filtering and transformation steps. Give routes IDs, add error handling, use toD() for computed destinations and keep one from() per route.