Language DSL

The Language DSL is a builder API that allows using type safe construction of Camel Languages.

The Language DSL is exclusively available as part of the Java DSL.

The DSL can be accessed directly from the RouteBuilder thanks to the method expression().

Using Language DSL

In the following example, a TokenizerExpression is created using the legacy approach where the expression is instantiated explicitly and configured using setters:

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() {
        TokenizerExpression expression = new TokenizerExpression("(\\W+)\\s*"); (1)
        expression.setRegex(true); (2)
        from("file:data")
            .split(expression) (3)
            .process("processEntry");
    }
}
1 Instantiate the expected expression
2 Configure the expression according to the needs
3 Affect the expression with the expected configuration

The previous code could be simplified using the utility methods available directly from the ExpressionClause corresponding to the type returned by several existing methods such as split(), setBody(), setHeader(String), aggregate(), etc.:

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() {
        from("file:data")
            .split()
            .tokenize("(\\W+)\\s*", true) (1)
            .process("processEntry");
    }
}
1 Select the tokenize language with a specific regular expression

This approach is suitable for very basic configuration, but as there are only limited utility methods for each supported language, for more complex configuration, we can quickly face situations where the utility method for our expected configuration doesn’t exist. In this situation, you can either use the legacy approach or the language DSL like in the next code snippet:

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() {
        from("file:data")
            .split(
                expression() (1)
                    .tokenize() (2)
                        .token("(\\W+)\\s*") (3)
                        .regex(true) (3)
                    .end() (4)
            )
            .process("processEntry");
    }
}
1 Give access to all the supported languages
2 Select the tokenize language
3 Configure the expression according to the needs
4 Build the expression with the expected configuration