Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 14. Camel CDI

14.1. Basic Features

Overview

The Camel CDI component provides auto-configuration for Apache Camel using CDI as the dependency injection framework, based on convention-over-configuration. It auto-detects Camel routes available in the application and provides beans for common Camel primitives like Endpoint, ProducerTemplate or TypeConverter. It implements standard Camel bean integration so that Camel annotations like @Consume, @Produce and @PropertyInject can be used seamlessly in CDI beans. Besides, it bridges Camel events (for example RouteAddedEvent, CamelContextStartedEvent, ExchangeCompletedEvent, …​) as CDI events and provides a CDI events endpoint that can be used to consume / produce CDI events from / to Camel routes.

How to enable Camel CDI in Apache Karaf

To enable Camel CDI in Apache Karaf, perform the following steps:

  1. Add the required pax-cdi, pax-cdi-weld, and camel-cdi features to the Karaf container, as follows:

    JBossFuse:karaf@root> features:install pax-cdi pax-cdi-weld camel-cdi
  2. To enable Camel CDI in a bundle, open the pom.xml file in your bundle’s Maven project and add the following Require-Capability element to the configuration of the Maven bundle plug-in:

    <project ...>
      ...
      <build>
        <plugins>
          ...
          <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
              <instructions>
                <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                <Import-Package>*</Import-Package>
                <Require-Capability>
                  osgi.extender; filter:="(osgi.extender=pax.cdi)",
                  org.ops4j.pax.cdi.extension; filter:="(extension=camel-cdi-extension)"
                </Require-Capability>
              </instructions>
            </configuration>
          </plugin>
          ...
        </plugins>
      </build>
      ...
    </project>
  3. To access the CDI annotations in Java, you must add a dependency on the CDI API package and on the Camel CDI package. Edit your bundle’s POM file, pom.xml, to add the CDI API package as a Maven dependency:

    <project ...>
      ...
      <dependencies>
        ...
        <!-- CDI API -->
        <dependency>
          <groupId>javax.enterprise</groupId>
          <artifactId>cdi-api</artifactId>
          <version>${cdi-api-1.2-version}</version>
          <scope>provided</scope>
        </dependency>
    
        <!-- Camel CDI API -->
        <dependency>
          <groupId>org.apache.camel</groupId>
          <artifactId>camel-cdi</artifactId>
          <version>2.21.0.fuse-000055-redhat-2</version>
        </dependency>
        ...
      </dependencies>
      ...
    </project>
  4. Rebuild your bundle in the usual way for your Maven project. For example, using the command:

    mvn clean install
  5. Deploy the bundle to the Karaf container in the usual way (for example, using the osgi:install console command).

Auto-configured Camel context

Camel CDI automatically deploys and configures a CamelContext bean. That CamelContext bean is automatically instantiated, configured and started (resp. stopped) when the CDI container initialises (resp. shuts down). It can be injected in the application, for example:

@Inject
CamelContext context;

The default CamelContext bean is qualified with the built-in @Default qualifier, is scoped @ApplicationScoped and is of type DefaultCamelContext.

Note that this bean can be customised programmatically and other Camel context beans can be deployed in the application as well.

Auto-detecting Camel routes

Camel CDI automatically collects all the RoutesBuilder beans in the application, instantiates and add them to the CamelContext bean instance when the CDI container initialises. For example, adding a Camel route is as simple as declaring a class, for example:

class MyRouteBean extends RouteBuilder {

	@Override
    public void configure() {
        from("jms:invoices").to("file:/invoices");
    }
}

Note that you can declare as many RoutesBuilder beans as you want. Besides, RouteContainer beans are also automatically collected, instantiated and added to the CamelContext bean instance managed by Camel CDI when the container initialises.