Chapter 13. CDI


Camel CDI

As of 2.10 we now have support Contexts and Dependency Injection - JSR299 and Dependency Injection for Java - JSR330 as a dependency injection framework. This offers new opportunities to develop and deploy Apache Camel projects in Java EE 6 containers but also in standalone Java SE or CDI container
The current project is under active development and does not provide all the features that we have with injection frameworks like Spring or Blueprint

Dependency Injecting Camel with CDI

Basically, two things should be done to use Apache Camel in a CDI environment. First, we just need to create a BootStrap class which will be use by the Java EE 6 container or Java SE to start the Camel Context. The CdiCamelContext when instantiated will add a CDI Bean Registry. That will allow Camel to perform lookup of beans injected and registered in CDI container. Next, we must add CDI annotated beans (@inject, @named, ...) to use them from the Apache Camel routes.

Bootstrapping Camel with CDI container

The following example shows how we can bootstrap an Apache Camel Context in a Boot Strap class. This class contains important annotations like the javax.ejb.Singleton. This annotation will tell the container to create a Singleton instance of the BootStrapClass class. This mechanism is similar to Bean instance creation that we have with Spring framework. By combining this annotation with javax.ejb.Startup, the container will start the camel context at the startup of the CDI container.
    @Singleton
    @Startup
    public class BootStrap {
    ...
When the @PreConstruct annotation is called, then we inject a CdiCamelContext objet, register a SimpleCamelRoute using @Inject annotation and starts the Camel Route.
    @PostConstruct
    public void init() throws Exception {
            logger.info(">> Create CamelContext and register Camel Route.");

            // Define Timer URI
            simpleRoute.setTimerUri("timer://simple?fixedRate=true&period=10s");

            // Add Camel Route
            camelCtx.addRoutes(simpleRoute);

            // Start Camel Context
            camelCtx.start();
When you look to the following Camel Route code, you can see that we do a lookup to find a bean "helloWorld" which has been injected. This is possible because the CdiCamelContext registers a Camel Registry containing a reference to a CDI BeanManager.
    @Override
    public void configure() throws Exception {

        from(timerUri)
            .setBody()
                .simple("Bean Injected")

            // Lookup for bean injected by CDI container
            // The HellowWorld class is annotated using @Named
            .beanRef("helloWorld", "sayHello")

            .log(">> Response : ${body}");

    }
Here is the code of the HelloWorld Bean
@Named
public class HelloWorld {

    public String sayHello(@Body String message) {
        return ">> Hello " + message + " user.";
    }
}
This project is started using the GlassFish maven plugin but alternatively, you can deploy the war file produced in any Java EE 6 servers : Glassfish, JBoss AS 7, OpenEJB, Apache TomEE or Apache KarafEE or using a Java SE.

See Also

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.