Routes
A Camel route is where the integration flow is defined. For example to integrate two systems then a Camel route can be coded to specify how these systems are integrated.
An example could be to take files from a FTP server and send to a ActiveMQ messaging system.
This can be coded in a route using Java with the Java DSL
from("ftp:myserver/folder")
.to("activemq:queue:cheese");
Camel support coding in other languages such as XML:
<route>
<from uri="ftp:myserver/folder"/>
<to uri="activemq:queue:cheese"/>
</route>
RouteBuilder with Java DSL
When coding routes with the Java DSL then you would use a RouteBuilder
classes where
you code the route in the configure
method as shown:
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("direct:a").to("direct:b");
}
};
As you can see from the above Camel uses URIs to wire endpoints together.
Routes using Java lambda style
Camel now supports to define Camel routes in Java DSL using Lambda style. This can be beneficial for microservices or serverless where you may want to quickly define a few routes.
For example using lambda style you can define a Camel route that takes messages from Kafka and send to JMS in a single line of code:
rb -> rb.from("kafka:cheese").to("jms:queue:foo");
There is a bit more to this as the lambda route must be coded in a Java method that returns an instance of LambdaRouteBuilder
.
See more at the LambdaRouteBuilder documentation.
Route Precondition
The routes can be included or not according to the result of a test expressed in simple language that is evaluated only once during the initialization phase.
In the next example, the route is only included if the parameter format
has been set to xml
.
from("direct:in").precondition("'{{format}}' == 'xml'")
.unmarshal().jaxb()
.to("direct:out");
And the same example using XML DSL:
<route precondition="'{{format}}' == 'xml'">
<from uri="direct:in"/>
<unmarshal><jaxb/></unmarshal>
<to uri="direct:out"/>
</route>
And in YAML DSL:
- route:
precondition: "'{{format}}' == 'xml'"
from:
uri: "direct:in"
steps:
- unmarshal:
jaxb: {}
- to: "direct:out"
More Information
See RouteBuilder and DSL for a list of supported languages you can use for coding Camel routes.