[[Routes-Routes]]
= Routes

Camel supports the definition of routing rules using a Java DSL (domain specific language) which avoids the need for cumbersome XML using a RouteBuilder.

For example a simple route can be created as follows.
[source,java]
------------------------------------------------------
RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a").to("direct:b");
    }
};
------------------------------------------------------

As you can see from the above Camel uses URIs to wire endpoints together.

[[Routes-URI-String-Formatting]]
== URI String formatting

If you have endpoint URIs that accept options and you want to be able to substitute the value, e.g. build the URI by concat the strings together, then you can use the java.lang.String.format method. But in Camel 2.0 we have added two convenient methods in the Java DSL so you can do fromF and toF that uses String formatting to build the URI.

[source,java]
--------------------------------------------------------------------
from("direct:start").toF("file://%s?fileName=%s", path, name);

fromF("file://%s?include=%s", path, pattern).toF("mock:%s", result);
--------------------------------------------------------------------

[[Routes-Filters]]
== Filters
You can combine simple routes with filters which can be arbitrary Predicate implementations.

[source,java]
-------------------------------------------------------------
RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .filter(header("foo").isEqualTo("bar"))
                .to("direct:b");
    }
};
-------------------------------------------------------------

[[Routes-Lambda]]
== Routes using Java lambda style

*Since Camel 3.5*

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:
[source,java]
----
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 xref:lambda-route-builder.adoc[LambdaRouteBuilder] documentation.

[[Routes-Choices]]
== Choices
With a choice you provide a list of predicates and outcomes along with an optional default otherwise clause which is invoked if none of the conditions are met.

[source,java]
-------------------------------------------------------------
RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .choice()
                .when(header("foo").isEqualTo("bar"))
                    .to("direct:b")
                .when(header("foo").isEqualTo("cheese"))
                    .to("direct:c")
                .otherwise()
                    .to("direct:d");
    }
};
-------------------------------------------------------------

[[Routes-Using-a-custom-processor]]
== Using a custom processor

Here is an example of using a custom Processor
[source,java]
----------------------------------------------------------
myProcessor = new Processor() {
    public void process(Exchange exchange) {
        log.debug("Called with exchange: " + exchange);
    }
};

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .process(myProcessor);
    }
};
----------------------------------------------------------

You can mix and match custom processors with filters and choices.

[source,java]
----------------------------------------------------------
RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .filter(header("foo").isEqualTo("bar"))
                .process(myProcessor);
    }
};
----------------------------------------------------------
