LambdaRouteBuilder

The LambdaRouteBuilder is a functional interface which is used for creating a routing rule using the DSL, using Java lambda style.

rb -> rb.from("timer:foo").log("Hello Lambda");

Instances of LambdaRouteBuilder are discovered and transformed into RouteBuilder instances which are added to the CamelContext.

Usage

To use LambdaRouteBuilder you need to create a method that returns LambdaRouteBuilder which then allows to use Java lambda style to define a single route.

In the example below the method myRoute is used to create a Camel route that consume from Kafka and send the messages to JMS.

To make the route discoverable by Camel during startup, then the method must be annotated. In standalone mode with camel-main you should use @BindToRegistry and with Spring Boot use @Bean and with Quarkus then use @Produce.

public class MyConfiguration {
    @BindToRegistry
    public LambdaRouteBuilder myRoute() {
        return rb -> rb.from("kafka:cheese").to("jms:queue:foo");
    }
}

LambdaEndpointRouteBuilder

The Endpoint DSL can also be used as a lambda route builder with the org.apache.camel.builder.endpoint.LambdaEndpointRouteBuilder class from the camel-endpointdsl JAR.

public class MyConfiguration {
    @BindToRegistry
    public LambdaEndpointRouteBuilder myRoute() {
        return rb -> rb.from(rb.kafka("cheese")).to(rb.jms("queue:foo"));
    }
}

The LambdaEndpointRouteBuilder has type safe endpoint but requires to prefix with the instance name (rb) when choosing an endpoint name. Notice above how to select the kafka endpoint

rb.from(rb.kafka("cheese"))

With the regular LambdaRouteBuilder it’s just a String type, so the rb prefix is not needed anymore:

rb.from("kafka:cheese")