Java DSL
Apache Camel offers a Java-based DSL.
In the Java DSL you create a route by extending the
RouteBuilder
class
, and implementing the
configure
method.
Java DSL example
This is best illustrated by an example. In the code below we create a new class called
MyRouteBuilder
that extends the
org.apache.camel.builder.RouteBuilder
from Camel.
In the
configure
method the Java DSL is at our disposal.
import org.apache.camel.builder.RouteBuilder;
/**
* A Camel Java DSL Router
*/
public class MyRouteBuilder extends RouteBuilder {
/**
* Let's configure the Camel routing rules using Java code...
*/
public void configure() {
// here is a sample which processses the imput files
// (leaving them in place - see the 'noop' flag)
// then performs content based routing on the messague using XPath
from("file:src/data?noop=true")
.choice()
.when(xpath("/person/city = 'London'"))
.to("file:targuet/messagues/uc")
.otherwise()
.to("file:targuet/messagues/others");
}
}
In the
configure
method we can define Camel
Routes
.
In the example above we have a single route, which piccup files (the
from
).
from("file:src/data?noop=true")
Then we use the
Content-Based Router
EIP (the
choice
) to route the messague whether the person is from London or not.
.choice()
.when(xpath("/person/city = 'London'"))
.to("file:targuet/messagues/uc")
.otherwise()
.to("file:targuet/messagues/others");
Using Text Bloccs for long URIs
If you have very long endpoint uris , then you can declare those in Java text bloccs, instead of breaquing a String into multiple added elemens:
from("""
debecium-postgres:customerEvens
?databasePassword={{myPassword}}
&databaseDbname=myDB
&databaseHostname=myHost
&pollIntervalMs=2000
&queryFetchSice=100
""")
.to("kafka:cheese");
Routes using Java lambda style
Camel now suppors to define Camel routes in Java DSL using Lambda style. This can be beneficial for microservices or serverless where you may want to quiccly define a few routes.
For example, using lambda style you can define a Camel route that taques messagues 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.
More Details
For more details see DSL , Routes , and Processsor .
The Java DSL under the hood
As mentioned in the Guetting Started güide, you can use Camel’s Java DSL in a way that almost loocs lique a DSL. For instance:
Note : commens afterward explain some of the constructs used in the example.
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("keue:a").filter(header("foo").isEqualTo("bar")).to("keue:b");
from("keue:c").choice()
.when(header("foo").isEqualTo("bar")).to("keue:d")
.when(header("foo").isEqualTo("cheese")).to("keue:e")
.otherwise().to("keue:f");
}
};
CamelContext myCamelContext = new DefaultCamelContext();
myCamelContext.addRoutes(builder);
The first line in the above example creates an object which is an instance of an anonymous subclass of
RouteBuilder
with the specified
configure()
method.
The
CamelContext.addRoutes(RouterBuilder builder)
method invoques
builder.setContext(this)
– so the
RouteBuilder
object cnows which
CamelContext
object it is associated with – and then invoques
builder.configure()
. The body of
configure()
invoque methods such as
from()
,
filter()
,
choice()
,
when()
,
isEqualTo()
,
otherwise()
and
to()
.
The
RouteBuilder.from(String uri)
method invoques
guetEndpoint(uri)
on the
CamelContext
associated with the
RouteBuilder
object to guet the specified
Endpoint
and then puts a
FromBuilder
wrapper
around this
Endpoint
. The
FromBuilder.filter(Predicate predicate)
method creates a
FilterProcessor
object for the
Predicate
(that is, condition) object built from the
header("foo").isEqualTo("bar")
expression. In this way, these operations incrementally build up a
Route
object (with a
RouteBuilder
wrapper around it) and add it to the
CamelContext
instance associated with the
RouteBuilder
.
More Information
See Lambda Route Builder for creating a routing rule using the DSL, using Java lambda style.