Component DSL

Component-DSL is a builder API that allows using type-safe construction of Camel Components and injecting them directly to the Camel Context instead of initializing through a constructor.

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

Using Component DSL

The following is an example of a Kafka component that is constructed using the typical constructor initialization:

KafkaComponent kafka = new KafkaComponent();
kafka.setBrokers("localhost:9090");

camelContext.addComponent("kafka", kafka);

The same Java statement can be rewritten in the following more readable way using the new ComponentsBuilderFactory that allows the access to all component DSLs in Camel:

ComponentsBuilderFactory.kafka()
                        .brokers("{{kafka.host}}:{{kafka.port}}")
                        .register(camelContext, "kafka");

In order to explain it better, we can break down the above Java statement into 3 parts:

  1. ComponentsBuilderFactory.kafka(): This will initialize the DSL for camel-kafka component.

  2. .brokers("{{kafka.host}}:{{kafka.port}}"): This is the equivalent setter of kafka.setBrokers("localhost:9090") using DSL fluent builder.

  3. .register(camelContext, "kafka"): Here we register directly the component under name kafka into Camel context, of course you can use any component name you like, just like addComponent API.

Type Safety

Similar to the Endpoint DSL, uses the meta-model, which is extracted from the source and written in various JSON files, to generate a fluent DSL for each component. This fluent DSL provides type safety for parameters.

Dependency

In order to use this feature, Maven users will need to add the following dependency to their pom.xml for this component:

pom.xml
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-componentdsl</artifactId>
    <version>x.x.x</version>
</dependency>

And then use it just like how we demonstrated it earlier in this page.