Repository: camel
Updated Branches:
  refs/heads/master 0e0d6e981 -> 5e83a39e3


Up-to-date Camel CDI documentation


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5e83a39e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5e83a39e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5e83a39e

Branch: refs/heads/master
Commit: 5e83a39e3a99206b6d4f8b91a78e2f350846f19a
Parents: 1a8fc04
Author: Antonin Stefanutti <anto...@stefanutti.fr>
Authored: Fri Feb 12 12:49:57 2016 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Feb 12 13:46:14 2016 +0100

----------------------------------------------------------------------
 components/camel-cdi/src/main/docs/cdi.adoc | 900 ++++++++++++++++++++---
 1 file changed, 796 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5e83a39e/components/camel-cdi/src/main/docs/cdi.adoc
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/docs/cdi.adoc 
b/components/camel-cdi/src/main/docs/cdi.adoc
index 6a2aece..3fc1071 100644
--- a/components/camel-cdi/src/main/docs/cdi.adoc
+++ b/components/camel-cdi/src/main/docs/cdi.adoc
@@ -2,140 +2,832 @@
 Camel CDI
 ~~~~~~~~~
 
-As of 2.10 we now have support
-http://jcp.org/en/jsr/detail?id=299[Contexts and Dependency Injection -
-JSR299] and http://jcp.org/en/jsr/detail?id=330[Dependency Injection for
-Java - JSR330] as a dependency injection framework. This offers new
-opportunities to develop and deploy Apache Camel projects in
-http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition[Java EE 6
-containers] but also in standalone Java SE or
-http://openwebbeans.apache.org[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
-
-[[CDI-DependencyInjectingCamelwithCDI]]
-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
-https://github.com/cmoulliard/cdi-camel/blob/master/src/main/java/com/fusesource/cdi/camel/simple/BootStrap.java[BootStrap]
-class which will be use by the Java EE 6 container or Java SE to start
-the Camel Context. The
-https://git-wip-us.apache.org/repos/asf?p=camel.git;a=blob;f=components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelContext.java;h=0d863bc8f5aa521b15955c26a512cdec09e366e9;hb=HEAD[CdiCamelContext]
-when instantiated will add a CDI
-http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html[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.
-
-[[CDI-BootstrappingCamelwithCDIcontainer]]
-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
-http://docs.oracle.com/javaee/6/api/javax/ejb/Singleton.html[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
-http://docs.oracle.com/javaee/6/api/javax/ejb/Startup.html[javax.ejb.Startup],
-the container will start the camel context at the startup of the CDI
-container.
-
-[source,java]
-----
-    @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.
+The Camel CDI component provides auto-configuration for Apache Camel
+using CDI as 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 link:bean-integration.html[Camel bean integration]
+so that Camel annotations like `@Consume`, `@Produce`
+and `@PropertyInject` can be used seamlessly in CDI beans. Besides, it
+bridges Camel events (e.g. `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.
+
+
+NOTE: While the Camel CDI component is available as of **Camel 2.10**, it's
+been rewritten in *Camel 2.17* to better fit into the CDI programming
+model. Hence some of the features like the Camel events to CDI events
+bridge and the CDI events endpoint only apply starting Camel 2.17.
+
+
+[[CDI-Auto-configuredCamelcontext]]
+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 initializes (resp. shuts
+down). It can be injected in the application, e.g.:
+
+[source,java]
+----
+@Inject
+CamelContext context;
+----
+
+That 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 customized programmatically and other Camel
+context beans can be deployed in the application as well.
+
+[[CDI-Auto-detectingCamelroutes]]
+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 initializes. For example, adding a Camel
+route is as simple as declaring a class, e.g.:
+
+[source,java]
+----
+class MyRouteBean extends RoutesBuilder {
+ 
+    @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 initializes.
+
+[[CDI-Auto-configuredCamelprimitives]]
+Auto-configured Camel primitives
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Camel CDI provides beans for common Camel primitives that can be
+injected in any CDI beans, e.g.:
 
 [source,java]
 ----
+@Inject
+@Uri("direct:inbound")
+ProducerTemplate producerTemplate;
+
+@Inject
+MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
+
+@Inject
+@Uri("direct:inbound")
+Endpoint endpoint;
+
+@Inject
+TypeConverter converter;
+----
+
+[[CDI-Camelcontextconfiguration]]
+Camel context configuration
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+If you just want to change the name of the default `CamelContext` bean,
+you can used the `@ContextName` qualifier provided by Camel CDI, e.g.:
+
+[source,java]
+----
+@ContextName("camel-context")
+class MyRouteBean extends RoutesBuilder {
+ 
+    @Override
+    public void configure() {
+        from("jms:invoices").to("file:/invoices");
+    }
+}
+----
+
+Else, if more customization is needed, any `CamelContext` class can be
+used to declare a custom Camel context bean. Then,
+the `@PostConstruct` and `@PreDestroy` lifecycle callbacks can be done
+to do the customization, e.g.:
+
+[source,java]
+----
+@ApplicationScoped
+class CustomCamelContext extends DefaultCamelContext {
+
     @PostConstruct
-    public void init() throws Exception {
-            logger.info(">> Create CamelContext and register Camel Route.");
+    void customize() {
+        // Set the Camel context name
+        setName("custom");
+        // Disable JMX
+        disableJMX();
+    }
 
-            // Define Timer URI
-            
simpleRoute.setTimerUri("timer://simple?fixedRate=true&period=10s");
+    @PreDestroy
+    void cleanUp() {
+        // ...
+    }
+}
+----
 
-            // Add Camel Route
-            camelCtx.addRoutes(simpleRoute);
+link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#producer_method[Producer]
+and 
link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#disposer_method[disposer]Â
 methods
+can also be used as well to customize the Camel context bean, e.g.:
 
-            // Start Camel Context
-            camelCtx.start();
+[source,java]
 ----
+class CamelContextFactory {
 
-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.
+    @Produces
+    @ApplicationScoped
+    CamelContext customize() {
+        DefaultCamelContext context = new DefaultCamelContext();
+        context.setName("custom");
+        return context;
+    }
+
+    void cleanUp(@Disposes CamelContext context) {
+        // ...
+    }
+}
+----
+
+Similarly, 
link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#producer_field[producer
+fields] can be used, e.g.:
 
 [source,java]
 ----
+@Produces
+@ApplicationScoped
+CamelContext context = new CustomCamelContext();
+
+class CustomCamelContext extends DefaultCamelContext {
+
+    CustomCamelContext() {
+        setName("custom");
+    }
+}
+----
+
+This pattern can be used for example to avoid having the Camel context
+routes started automatically when the container initializes by calling
+the `setAutoStartup` method, e.g.:
+
+[source,java]
+----
+@ApplicationScoped
+class ManualStartupCamelContext extends DefaultCamelContext {
+
+    @PostConstruct
+    void manual() {
+        setAutoStartup(false);
+    }
+}
+----
+
+[[CDI-MultipleCamelcontexts]]
+Multiple Camel contexts
+^^^^^^^^^^^^^^^^^^^^^^^
+
+Any number of `CamelContext` beans can actually be declared in the
+application as documented above. In that case, the CDI qualifiers
+declared on these `CamelContext` beans are used to bind the Camel routes
+and other Camel primitives to the corresponding Camel contexts. From
+example, if the following beans get declared:
+
+[source,java]
+----
+@ApplicationScoped
+@ContextName("foo")
+class FooCamelContext extends DefaultCamelContext {
+}
+
+@ApplicationScoped
+@BarContextQualifier
+class BarCamelContext extends DefaultCamelContext {
+}
+ 
+@ContextName("foo")
+class RouteAddedToFooCamelContext extends RoutesBuilder {
+
     @Override
-    public void configure() throws Exception {
+    public void configure() {
+        // ...
+    }
+}
+ 
+@BarContextQualifier
+class RouteAddedToBarCamelContext extends RoutesBuilder {
+
+    @Override
+    public void configure() {
+        // ...
+    }
+}
+ 
+@ContextName("baz")
+class RouteAddedToBazCamelContext extends RoutesBuilder {
+
+    @Override
+    public void configure() {
+        // ...
+    }
+}
+ 
+@MyOtherQualifier
+class RouteNotAddedToAnyCamelContext extends RoutesBuilder {
+
+    @Override
+    public void configure() {
+        // ...
+    }
+}
+----
+
+The `RoutesBuilder` beans qualified with `@ContextName` are
+automatically added to the corresponding `CamelContext` beans by Camel
+CDI. If no such `CamelContext` bean exists, it gets automatically
+created, as for the `RouteAddedToBazCamelContext` bean. Note this only
+happens for the `@ContextName` qualifier provided by Camel CDI. Hence
+the `RouteNotAddedToAnyCamelContext` bean qualified with the
+user-defined `@MyOtherQualifier` qualifier does not get added to any
+Camel contexts. That may be useful, for example, for Camel routes that
+may be required to be added later during the application execution.
+
+The CDI qualifiers declared on the `CamelContext` beans are also used to
+bind the corresponding Camel primitives, e.g.:
+
+[source,java]
+----
+@Inject
+@ContextName("foo")
+@Uri("direct:inbound")
+ProducerTemplate producerTemplate;
+
+@Inject
+@BarContextQualifier
+MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
+
+@Inject
+@ContextName("baz")
+@Uri("direct:inbound")
+Endpoint endpoint;
+----
+
+[[CDI-Configurationproperties]]
+Configuration properties
+^^^^^^^^^^^^^^^^^^^^^^^^
 
-        from(timerUri)
-            .setBody()
-                .simple("Bean Injected")
+To configure the sourcing of the configuration properties used by Camel
+to resolve properties placeholders, you can declare
+a `PropertiesComponent` bean qualified with `@Named("properties")`,
+e.g.:
 
-            // Lookup for bean injected by CDI container
-            // The HellowWorld class is annotated using @Named
-            .beanRef("helloWorld", "sayHello")
+[source,java]
+----
+@Produces
+@ApplicationScoped
+@Named("properties")
+PropertiesComponent propertiesComponent() {
+    Properties properties = new Properties();
+    properties.put("property", "value");
+    PropertiesComponent component = new PropertiesComponent();
+    component.setInitialProperties(properties);
+    component.setLocation("classpath:placeholder.properties");
+    return component;
+}
+----
 
-            .log(">> Response : ${body}");
+If you want to
+use 
link:http://deltaspike.apache.org/documentation/configuration.html[DeltaSpike
+configuration mechanism] you can declare the
+following `PropertiesComponent` bean:
 
+[source,java]
+----
+@Produces
+@ApplicationScoped
+@Named("properties")
+PropertiesComponent properties(PropertiesParser parser) {
+    PropertiesComponent component = new PropertiesComponent();
+    component.setPropertiesParser(parser);
+    return component;
+}
+
+// PropertiesParser bean that uses DeltaSpike to resolve properties
+static class DeltaSpikeParser extends DefaultPropertiesParser {
+    @Override
+    public String parseProperty(String key, String value, Properties 
properties) {
+        return ConfigResolver.getPropertyValue(key);
     }
+}
 ----
 
-Here is the code of the HelloWorld Bean
+You can see the `camel-example-cdi-properties` example for a working
+example of a Camel CDI application using DeltaSpike configuration
+mechanism.
+
+[[CDI-Auto-configuredtypeconverters]]
+Auto-configured type converters
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+CDI beans annotated with the `@Converter` annotation are automatically
+registered into the deployed Camel contexts, e.g.:
 
 [source,java]
 ----
-@Named
-public class HelloWorld {
+@Converter
+public class MyTypeConverter {
 
-    public String sayHello(@Body String message) {
-        return ">> Hello " + message + " user.";
+    @Converter
+    public Output convert(Input input) {
+        //...
     }
 }
 ----
 
-This project is started using the
-http://embedded-glassfish.java.net/nonav/plugindocs/3.1/plugin-info.html[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
-http://agoncal.wordpress.com/2011/01/12/bootstrapping-cdi-in-several-environments/[Java
-SE].
+Note that CDI injection is supported within the type converters.
+
+[[CDI-Camelbeanintegration]]
+Camel bean integration
+^^^^^^^^^^^^^^^^^^^^^^
+
+[[CDI-Camelannotations]]
+Camel annotations
++++++++++++++++++
+
+As part of the Camel link:http://camel.apache.org/bean-integration.html[bean
+integration], Camel comes with a set
+of 
link:http://camel.apache.org/bean-integration.html#BeanIntegration-Annotations[annotations]Â
 that
+are seamlessly supported by Camel CDI. So you can use any of these
+annotations in your CDI beans, e.g.:
+
+[width="100%",cols="1,2a,2a",options="header",]
+|=======================================================================
+|  |Camel annotation |CDI equivalent
+|Configuration property a|
+[source,java]
+----
+@PropertyInject("key")
+String value;
+----
+
+ a|
+If using
+http://deltaspike.apache.org/documentation/configuration.html[DeltaSpike
+configuration mechanism]:
+
+[source,java]
+----
+@Inject
+@ConfigProperty(name = "key")
+String value;
+----
+
+See link:cdi.html[configuration properties] for more details.
+
+|Producer template injection (default Camel context) a|
+[source,java]
+----
+@Produce(uri = "mock:outbound")
+ProducerTemplate producer;
+----
+
+ a|
+[source,java]
+----
+@Inject
+@Uri("direct:outbound")
+ProducerTemplate producer;
+----
+
+|Endpoint injection (default Camel context) a|
+[source,java]
+----
+@EndpointInject(uri = "direct:inbound")
+Endpoint endpoint;
+----
+
+ a|
+[source,java]
+----
+@Inject
+@Uri("direct:inbound")
+Endpoint endpoint;
+----
+
+|Endpoint injection (Camel context by name) a|
+[source,java]
+----
+@EndpointInject(uri = "direct:inbound",
+                context = "foo")
+Endpoint contextEndpoint;
+----
+
+ a|
+[source,java]
+----
+@Inject
+@ContextName("foo")
+@Uri("direct:inbound")
+Endpoint contextEndpoint;
+----
+
+|Bean injection (by type) a|
+[source,java]
+----
+@BeanInject
+MyBean bean;
+----
+
+ a|
+[source,java]
+----
+@Inject
+MyBean bean;
+----
+
+|Bean injection (by name) a|
+[source,java]
+----
+@BeanInject("foo")
+MyBean bean;
+----
+
+ a|
+[source,java]
+----
+@Inject
+@Named("foo")
+MyBean bean;
+----
+
+|POJO consuming a|
+[source,java]
+----
+@Consume(uri = "seda:inbound")
+void consume(@Body String body) {
+    //...
+}
+----
+
+ | 
+|=======================================================================
+
+[[CDI-Beancomponent]]
+Bean component
+++++++++++++++
+
+You can refer to CDI beans, either by type or name, From the Camel DSL,
+e.g. with the Java Camel DSL:
+
+[source,java]
+----
+class MyBean {
+    //...
+}
+ 
+from("direct:inbound").bean(MyBean.class);
+----
+
+Or to lookup a CDI bean by name from the Java DSL:
+
+[source,java]
+----
+@Named("foo")
+class MyNamedBean {
+    //...
+}
+ 
+from("direct:inbound").bean("foo");
+----
+
+[[CDI-ReferringbeansfromEndpointURIs]]
+Referring beans from Endpoint URIs
+++++++++++++++++++++++++++++++++++
+
+When configuring endpoints using the URI syntax you can refer to beans
+in the link:registry.html[Registry] using the `#` notation. If the URI
+parameter value starts with a `#` sign then Camel CDI will lookup for a
+bean of the given type by name, e.g.:
+
+[source,java]
+----
+from("jms:queue:{{destination}}?transacted=true&transactionManager=#jtaTransactionManager").to("...");
+----
+
+Having the following CDI bean qualified
+with `@Named("jtaTransactionManager")`:
+
+[source,java]
+----
+@Produces
+@Named("jtaTransactionManager")
+PlatformTransactionManager createTransactionManager(TransactionManager 
transactionManager, UserTransaction userTransaction) {
+    JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
+    jtaTransactionManager.setUserTransaction(userTransaction);
+    jtaTransactionManager.setTransactionManager(transactionManager);
+    jtaTransactionManager.afterPropertiesSet();
+    return jtaTransactionManager;
+}
+----
+
+[[CDI-CameleventstoCDIevents]]
+Camel events to CDI events
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+*Available as of Camel 2.17*
+
+Camel provides a set
+of 
link:http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/management/event/package-summary.html[management
+events] that can be subscribed to for listening to Camel context,
+service, route and exchange events. Camel CDI seamlessly translates
+these Camel events into CDI events that can be observed using
+CDI 
link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_methods[observer
+methods], e.g.:
+
+[source,java]
+----
+void onContextStarting(@Observes CamelContextStartingEvent event) {
+    // Called before the default Camel context is about to start
+}
+----
+
+When multiple Camel contexts exist in the CDI container, the Camel
+context bean qualifiers, like `@ContextName`, can be used to refine the
+observer method resolution to a particular Camel context as specified
+in 
link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_resolution[observer
+resolution], e.g.:
+
+[source,java]
+----
+void onRouteStarted(@Observes @ContextName("foo") RouteStartedEvent event) {
+    // Called after the route 'event.getRoute()' for the Camel context 'foo' 
has started
+}
+ 
+void onContextStarted(@Observes @Manual CamelContextStartedEvent event) {
+    // Called after the the Camel context qualified with '@Manual' has started
+}
+----
+
+Similarly, the `@Default` qualifier can be used to observe Camel events
+for the _default_ Camel context if multiples contexts exist, e.g.:
+
+[source,java]
+----
+void onExchangeCompleted(@Observes @Default ExchangeCompletedEvent event) {
+    // Called after the exchange 'event.getExchange()' processing has completed
+}
+----
+
+In that example, if no qualifier is specified, the `@Any` qualifier is
+implicitly assumed, so that corresponding events for all the Camel
+contexts get received.
+
+Note that the support for Camel events translation into CDI events is
+only activated if observer methods listening for Camel events are
+detected in the deployment, and that per Camel context.
+
+[[CDI-CDIeventsendpoint]]
+CDI events endpoint
+^^^^^^^^^^^^^^^^^^^
+
+*Available as of Camel 2.17*
+
+The CDI event endpoint bridges
+the link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#events[CDI
+events] with the Camel routes so that CDI events can be seamlessly
+observed / consumed (resp. produced / fired) from Camel consumers (resp.
+by Camel producers).
+
+The `CdiEventEndpoint<T>` bean provided by Camel CDI can be used to
+observe / consume CDI events whose _event type_ is `T`, for example:
+
+[source,java]
+----
+@Inject
+CdiEventEndpoint<String> cdiEventEndpoint;
+
+from(cdiEventEndpoint).log("CDI event received: ${body}");
+----
+
+This is equivalent to writing:
+
+[source,java]
+----
+@Inject
+@Uri("direct:event")
+ProducerTemplate producer;
+
+void observeCdiEvents(@Observes String event) {
+    producer.sendBody(event);
+}
+
+from("direct:event").log("CDI event received: ${body}");
+----
+
+Conversely, the `CdiEventEndpoint<T>` bean can be used to produce / fire
+CDI events whose _event type_ is `T`, for example:
+
+[source,java]
+----
+@Inject
+CdiEventEndpoint<String> cdiEventEndpoint;
+
+from("direct:event").to(cdiEventEndpoint).log("CDI event sent: ${body}");
+----
+
+This is equivalent to writing:
+
+[source,java]
+----
+@Inject
+Event<String> event;
+
+from("direct:event").process(new Processor() {
+    @Override
+    public void process(Exchange exchange) {
+        event.fire(exchange.getBody(String.class));
+    }
+}).log("CDI event sent: ${body}");
+----
+
+Or using a Java 8 lambda expression:
+
+[source,java]
+----
+@Inject
+Event<String> event;
+
+from("direct:event")
+    .process(exchange -> event.fire(exchange.getIn().getBody(String.class)))
+    .log("CDI event sent: ${body}");
+----
+
+The type variable `T` (resp. the qualifiers) of a
+particular `CdiEventEndpoint<T>` injection point are automatically
+translated into the parameterized _event type_ (resp. into the _event
+qualifiers_) e.g.:
+
+[source,java]
+----
+@Inject
+@FooQualifier
+CdiEventEndpoint<List<String>> cdiEventEndpoint;
+
+from("direct:event").to(cdiEventEndpoint);
+
+void observeCdiEvents(@Observes @FooQualifier List<String> event) {
+    logger.info("CDI event: {}", event);
+}
+----
+
+When multiple Camel contexts exist in the CDI container, the Camel
+context bean qualifiers, like `@ContextName`, can be used to qualify
+the `CdiEventEndpoint<T>` injection points, e.g.:
+
+[source,java]
+----
+@Inject
+@ContextName("foo")
+CdiEventEndpoint<List<String>> cdiEventEndpoint;
+// Only observes / consumes events having the @ContextName("foo") qualifier
+from(cdiEventEndpoint).log("Camel context (foo) > CDI event received: 
${body}");
+// Produces / fires events with the @ContextName("foo") qualifier
+from("...").to(cdiEventEndpoint);
+
+void observeCdiEvents(@Observes @ContextName("foo") List<String> event) {
+    logger.info("Camel context (foo) > CDI event: {}", event);
+}
+----
+
+Note that the CDI event Camel endpoint dynamically adds
+an 
link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_methods[observer
+method] for each unique combination of _event type_ and _event
+qualifiers_ and solely relies on the container
+typesafe 
link:http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_resolution[observer
+resolution], which leads to an implementation as efficient as possible.
+
+Besides, as the impedance between the _typesafe_ nature of CDI and
+the _dynamic_ nature of
+the link:http://camel.apache.org/component.html[Camel component] model is
+quite high, it is not possible to create an instance of the CDI event
+Camel endpoint via link:http://camel.apache.org/uris.html[URIs]. Indeed, the
+URI format for the CDI event component is:
+
+[source,text]
+----
+cdi-event://PayloadType<T1,...,Tn>[?qualifiers=QualifierType1[,...[,QualifierTypeN]...]]
+----
+
+With the authority `PayloadType` (resp. the `QualifierType`) being the
+URI escaped fully qualified name of the payload (resp. qualifier) raw
+type followed by the type parameters section delimited by angle brackets
+for payload parameterized type. Which leads to _unfriendly_ URIs,
+e.g.:
+
+[source,text]
+----
+cdi-event://org.apache.camel.cdi.example.EventPayload%3Cjava.lang.Integer%3E?qualifiers=org.apache.camel.cdi.example.FooQualifier%2Corg.apache.camel.cdi.example.BarQualifier
+----
+
+But more fundamentally, that would prevent efficient binding between the
+endpoint instances and the observer methods as the CDI container doesn't
+have any ways of discovering the Camel context model during the
+deployment phase.
+
+[[CDI-Auto-configuredOSGiintegration]]
+Auto-configured OSGi integration
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+*Available as of Camel 2.17*
+
+The Camel context beans are automatically adapted by Camel CDI so that
+they are registered as OSGi services and the various resolvers
+(like `ComponentResolver` and `DataFormatResolver`) integrate with the
+OSGi registry. That means that the link:karaf.html[Karaf Camel commands]
+can be used to operate the Camel contexts auto-configured by Camel CDI,
+e.g.:
+
+[source,brush:,text;,gutter:,false;,theme:,Default]
+----
+karaf@root()> camel:context-list
+ Context        Status              Total #       Failed #     Inflight #   
Uptime
+ -------        ------              -------       --------     ----------   
------
+ camel-cdi      Started                   1              0              0   1 
minute
+----
+
+See the `camel-example-cdi-osgi` example for a working example of the
+Camel CDI OSGi integration.
+
+[[CDI-MavenArchetype]]
+Maven Archetype
+^^^^^^^^^^^^^^^
+
+Among the available link:camel-maven-archetypes.html[Camel Maven
+archetypes], you can use the provided `camel-archetype-cdi` to generate
+a Camel CDI Maven project, e.g.:
+
+[source,bash]
+----
+mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes 
-DarchetypeArtifactId=camel-archetype-cdi
+----
+
+[[CDI-Supportedcontainers]]
+Supported containers
+^^^^^^^^^^^^^^^^^^^^
+
+The Camel CDI component is compatible with any CDI 1.0, CDI 1.1 and CDI
+1.2 compliant runtime. It's been successfully tested against the
+following runtimes:
+
+[width="100%",cols="2,1m,2",options="header",]
+|============================================
+|Container |Version |Runtime
+|Weld SE |1.1.28.Final |CDI 1.0 / Java SE 7
+|OpenWebBeans |1.2.7 |CDI 1.0 / Java SE 7
+|Weld SE |2.3.2.Final |CDI 1.2 / Java SE 7
+|OpenWebBeans |1.6.2 |CDI 1.2 / Java SE 7
+|WildFly |8.2.1.Final |CDI 1.2 / Java EE 7
+|WildFly |9.0.1.Final |CDI 1.2 / Java EE 7
+|Karaf |2.4.4 |CDI 1.2 / OSGi 4 / PAX CDI
+|Karaf |3.0.5 |CDI 1.2 / OSGi 5 / PAX CDI
+|Karaf |4.0.4 |CDI 1.2 / OSGi 6 / PAX CDI
+|============================================
+
+[[CDI-Examples]]
+Examples
+^^^^^^^^
+
+The following examples are available in the `examples` directory of the
+Camel project:
+
+* `camel-example-cdi-metrics` - illustrates the integration between
+Camel, Dropwizard Metrics and CDI,
+* `camel-example-cdi-properties` - illustrates the integration between
+Camel, DeltaSpike and CDI for configuration properties,
+* `camel-example-cdi-osgi` - a CDI application using the SJMS component
+that can be executed inside an OSGi container using PAX CDI,
+* `camel-example-cdi-rest-servlet` - illustrates the Camel REST DSL
+being used in a Web application that uses CDI as dependency injection
+framework,
+* `camel-example-widget-gadget-cdi` - the Widget and Gadget use-case
+from the EIP book implemented in Java with CDI dependency Injection,
+* `camel-example-swagger-cdi` - an example using REST DSL and Swagger
+Java with CDI.
 
 [[CDI-SeeAlso]]
 See Also
 ^^^^^^^^
 
-* Simple https://github.com/cmoulliard/cdi-camel-example/[Camel CDI BootStrap 
project]
-* http://docs.jboss.org/weld/reference/1.1.5.Final/en-US/html_single/[JSR299]
-  and http://openwebbeans.apache.org/owb/jsr330.html[JSR330] reference 
documentations
-* http://deltaspike.apache.org[Apache DeltaSpike project] - CDI
-  extensions and JavaSE BootStrap
-* CDI revealed by Antonio Goncalves -
-  https://agoncal.wordpress.com/2011/04/07/injection-with-cdi-part-i/[part 1],
-  https://agoncal.wordpress.com/2011/05/03/injection-with-cdi-part-ii/[part 2],
-  https://agoncal.wordpress.com/2011/09/25/injection-with-cdi-part-iii/[part 3]
-  and OpenEJB team - see 
http://openejb.apache.org/examples-trunk/index.html[examples]
-* Apache implementation of the specs JSR299, 330 -
-  http://openwebbeans.apache.org[OpenWebbeans] and Apache
-  http://openejb.apache.org/[OpenEJB] which provide the container to
-  deploy CDI projects
-* Apache Karaf featured with OpenEJB and CDI -
-  https://svn.apache.org/repos/asf/openejb/trunk/openejb/osgi/[Apache KarafEE]
+* http://www.cdi-spec.org[CDI Web site]
+* http://www.cdi-spec.org/ecosystem/[CDI ecosystem]
+* https://github.com/astefanutti/further-cdi[Going further with CDI]
+(See Camel CDI section)

Reply via email to