Routes
In Apache Camel, a route is a set of processing steps that are applied to a message as it travels from a source to a destination. A route typically consists of a series of processing steps that are connected in a linear sequence.
A Camel route is where the integration flow is defined. For example, you can write a Camel route to specify how two systems can be integrated. You can also specify how the data can be manipulated, routed, or mediated between the systems.
The routes are typically defined using a simple, declarative syntax that is easy to read and understand.
For instance, you could write a route to consume files from an FTP server and send them to an ActiveMQ messaging system. A route to do so, using Java DSL, would look like this:
from("ftp:myserver/folder")
.to("activemq:queue:cheese");
Camel routes can be defined using a variety of domain-specific languages (DSLs), such as Java, Spring XML, or YAML. For example, you could write the route described above using XML:
<route>
<from uri="ftp:myserver/folder"/>
<to uri="activemq:queue:cheese"/>
</route>
Writing Routes in Java using the Java DSL
You can create a route using the Java language by extending the RouteBuilder
class, and implementing the configure
method.
Here’s an example:
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("direct:a").to("direct:b");
}
};
As you can see from the code snippet above, Camel uses URIs to wire endpoints together.
We refer to this way of writing route as using the Java DSL.
Route Precondition
The routes can be included or not according to the result of a test. You can express the condition for the tests using the simple language. Camel evaluates this condition only once during the initialization phase.
Here’s an example that includes the route only if the parameter format
has been set to xml
:
from("direct:in").precondition("'{{format}}' == 'xml'")
.unmarshal().jaxb()
.to("direct:out");
You can write the same route described above using the XML DSL:
<route precondition="'{{format}}' == 'xml'">
<from uri="direct:in"/>
<unmarshal><jaxb/></unmarshal>
<to uri="direct:out"/>
</route>
You can also write the same route described above using the YAML DSL:
- route:
precondition: "'{{format}}' == 'xml'"
from:
uri: "direct:in"
steps:
- unmarshal:
jaxb: {}
- to: "direct:out"
More Information
Check the list of supported languages that you can use for writing Camel routes.