You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bu...@apache.org on 2016/02/10 03:21:11 UTC

svn commit: r979991 [4/7] - in /websites/production/camel/content: book-component-appendix.html book-in-one-page.html cache/main.pageCache camel-2170-release.html cdi.html http.html http4.html leveldb.html

Modified: websites/production/camel/content/cdi.html
==============================================================================
--- websites/production/camel/content/cdi.html (original)
+++ websites/production/camel/content/cdi.html Wed Feb 10 02:21:11 2016
@@ -40,6 +40,8 @@
   <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css' rel='stylesheet' type='text/css' />
   <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js' type='text/javascript'></script>
   <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js' type='text/javascript'></script>
+  <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js' type='text/javascript'></script>
+  <script src='//camel.apache.org/styles/highlighter/scripts/shBrushBash.js' type='text/javascript'></script>
   
   <script type="text/javascript">
   SyntaxHighlighter.defaults['toolbar'] = false;
@@ -84,88 +86,338 @@
 	<tbody>
         <tr>
         <td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2 id="CDI-CamelCDI">Camel CDI</h2>
-
-<p>As of 2.10 we now have support <a shape="rect" class="external-link" href="http://jcp.org/en/jsr/detail?id=299" rel="nofollow">Contexts and Dependency Injection - JSR299</a> and <a shape="rect" class="external-link" href="http://jcp.org/en/jsr/detail?id=330" rel="nofollow">Dependency Injection for Java - JSR330</a> as a dependency injection framework. This offers new opportunities to develop and deploy Apache Camel projects in <a shape="rect" class="external-link" href="http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition" rel="nofollow">Java EE 6 containers</a> but also in standalone Java SE or <a shape="rect" class="external-link" href="http://openwebbeans.apache.org">CDI container</a> </p>
-
-<p>The current project is under active development and does not provide all the features that we have with injection frameworks like Spring or Blueprint </p>
-
-<h3 id="CDI-DependencyInjectingCamelwithCDI">Dependency Injecting Camel with CDI</h3>
+<div class="wiki-content maincontent"><h2 id="CDI-CamelCDI">Camel CDI</h2><p>The Camel CDI component provides auto-configuration for Apache Camel using CDI as dependency injection framework based on&#160;<em>convention-over-configuration</em>. It auto-detects Camel routes available in the application and provides beans for common Camel primitives like <code>Endpoint</code>,&#160;<code>ProducerTemplate</code> or&#160;<code>TypeConverter</code>. It implements standard <a shape="rect" href="bean-integration.html">Camel bean integration</a> so that Camel annotations like&#160;<code>@Consume</code>,&#160;<code>@Produce</code> and&#160;<span style="color: rgb(0,0,0);"><code>@PropertyInject</code> can be used seamlessly in CDI beans</span>. Besides, it bridges Camel events (e.g. <code>RouteAddedEvent</code>, <code>CamelContextStartedEvent</code>,&#160;<code>ExchangeCompletedEvent</code>, ...) as CDI events and provides a CDI events endpoint that can be used to consume / produce CDI events 
 from / to Camel routes.</p><div class="confluence-information-macro confluence-information-macro-information"><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>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.</p></div></div><h3 id="CDI-Auto-configuredCamelcontext">Auto-configured Camel context</h3><p>Camel CDI automatically deploys and configures a&#160;<code>CamelContext</code> bean. That <code>CamelContext</code> bean is automatically instantiated, configured and started (resp. stopped) when the CDI container initialises (resp. shuts down). It can be injected in the application, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent pa
 nelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+CamelContext context;]]></script>
+</div></div><p>That default <code>CamelContext</code> bean is qualified with the built-in&#160;<code>@Default</code> qualifier, is scoped&#160;<code>@ApplicationScoped</code> and is of type <code>DefaultCamelContext</code>.</p><p>Note that this bean can be customised programmatically and other Camel context beans can be deployed in the application as well.</p><h3 id="CDI-Auto-detectingCamelroutes">Auto-detecting Camel routes</h3><p>Camel CDI automatically&#160;collects all the&#160;<code>RoutesBuilder</code> beans in the application, instantiates and add them to the <code>CamelContext</code> bean instance when the CDI container initialises. For example, adding a Camel route is as simple as declaring a class, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[class MyRouteBean extends RoutesBuilder {
+ 
+	@Override
+    public void configure() {
+        from(&quot;jms:invoices&quot;).to(&quot;file:/invoices&quot;);
+    }
+}]]></script>
+</div></div><p>Note that you can declare as many&#160;<code>RoutesBuilder</code> beans as you want. Besides,&#160;<code>RouteContainer</code> beans are also automatically collected, instantiated and added to the&#160;<code>CamelContext</code> bean instance managed by Camel CDI when the container initialises.</p><h3 id="CDI-CustomCamelcontextcustomisation">Custom Camel context customisation</h3><p>If you just want to change the name of the default <code>CamelContext</code> bean, you can used the <code>@ContextName</code> qualifier&#160;provided by Camel CDI, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@ContextName(&quot;camel-context&quot;)
+class MyRouteBean extends RoutesBuilder {
+ 
+	@Override
+    public void configure() {
+        from(&quot;jms:invoices&quot;).to(&quot;file:/invoices&quot;);
+    }
+}]]></script>
+</div></div><p>Else, if more customisation is needed, any&#160;<code>CamelContext</code>&#160;class can be used to declare a custom Camel context bean. Then, the&#160;<code>@PostConstruct</code>&#160;and&#160;<code>@PreDestroy</code>&#160;lifecycle callbacks can be done to do the customisation, e.g.:</p><div class="highlight highlight-source-java"><p>&#160;</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@ApplicationScoped
+class CustomCamelContext extends DefaultCamelContext {
 
-<p>Basically, two things should be done to use Apache Camel in a CDI environment. First, we just need to create a <a shape="rect" class="external-link" href="https://github.com/cmoulliard/cdi-camel/blob/master/src/main/java/com/fusesource/cdi/camel/simple/BootStrap.java" rel="nofollow">BootStrap</a> class which will be use by the Java EE 6 container or Java SE to start the Camel Context. The <a shape="rect" class="external-link" href="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</a> when instantiated will add a CDI <a shape="rect" class="external-link" href="http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html" rel="nofollow">Bean Registry</a>. 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.</p>
+    @PostConstruct
+    void customize() {
+        // Set the Camel context name
+        setName(&quot;custom&quot;);
+        // Disable JMX
+        disableJMX();
+    }
 
-<h3 id="CDI-BootstrappingCamelwithCDIcontainer">Bootstrapping Camel with CDI container</h3>
+    @PreDestroy
+    void cleanUp() {
+        // ...
+    }
+}]]></script>
+</div></div><p>&#160;</p></div><p><a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#producer_method" rel="nofollow">Producer</a>&#160;and&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#disposer_method" rel="nofollow">disposer</a>&#160;methods can also be used as well to customize the Camel context bean, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[class CamelContextFactory {
+
+    @Produces
+    @ApplicationScoped
+    CamelContext customize() {
+        DefaultCamelContext context = new DefaultCamelContext();
+        context.setName(&quot;custom&quot;);
+        return context;
+    }
 
-<p>The following example shows how we can bootstrap an Apache Camel Context in a Boot Strap class. This class contains important annotations like the  <a shape="rect" class="external-link" href="http://docs.oracle.com/javaee/6/api/javax/ejb/Singleton.html" rel="nofollow">javax.ejb.Singleton</a>. 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 <a shape="rect" class="external-link" href="http://docs.oracle.com/javaee/6/api/javax/ejb/Startup.html" rel="nofollow">javax.ejb.Startup</a>, the container will start the camel context at the startup of the CDI container. </p>
+    void cleanUp(@Disposes CamelContext context) {
+        // ...
+    }
+}]]></script>
+</div></div><p>Similarly,&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#producer_field" rel="nofollow">producer fields</a>&#160;can be used, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Produces
+@ApplicationScoped
+CamelContext context = new CustomCamelContext();
 
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[
-    @Singleton
-    @Startup
-    public class BootStrap {
-    ...
-]]></script>
-</div></div>
+class CustomCamelContext extends DefaultCamelContext {
 
-<p>When the @PreConstruct annotation is called, then we inject a CdiCamelContext objet, register a SimpleCamelRoute using @Inject annotation and starts the Camel Route.</p>
+    CustomCamelContext() {
+        setName(&quot;custom&quot;);
+    }
+}]]></script>
+</div></div><p>This pattern can be used for example to avoid having the Camel context routes started automatically when the container initialises by calling the&#160;<code>setAutoStartup</code>&#160;method, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@ApplicationScoped
+class ManualStartupCamelContext extends DefaultCamelContext {
 
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[
     @PostConstruct
-    public void init() throws Exception {
-            logger.info(&quot;&gt;&gt; Create CamelContext and register Camel Route.&quot;);
-
-            // Define Timer URI
-            simpleRoute.setTimerUri(&quot;timer://simple?fixedRate=true&amp;period=10s&quot;);
-
-            // Add Camel Route
-            camelCtx.addRoutes(simpleRoute);
-
-            // Start Camel Context
-            camelCtx.start();
-]]></script>
-</div></div>
+    void manual() {
+        setAutoStartup(false);
+    }
+}]]></script>
+</div></div><h3 id="CDI-MultipleCamelcontextssupport">Multiple Camel contexts support</h3><p>Any number of <code>CamelContext</code> beans can actually be declared in the application as documented above. In that case, the CDI qualifiers declared on these <code>CamelContext</code> 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:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@ApplicationScoped
+@ContextName(&quot;foo&quot;)
+class FooCamelContext extends DefaultCamelContext {
+}
 
-<p>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.</p>
+@ApplicationScoped
+@BarContextQualifier
+class BarCamelContext extends DefaultCamelContext {
+}
+ 
+@ContextName(&quot;foo&quot;)
+class RouteAdddedToFooCamelContext extends RoutesBuilder {
+ 
+	@Override
+    public void configure() {
+        // ...
+    }
+}
+ 
+@BarContextQualifier
+class RouteAdddedToBarCamelContext extends RoutesBuilder {
+ 
+	@Override
+    public void configure() {
+        // ...
+    }
+}
+ 
+@ContextName(&quot;baz&quot;)
+class RouteAdddedToBazCamelContext extends RoutesBuilder {
+ 
+	@Override
+    public void configure() {
+        // ...
+    }
+}
+ 
+@MyOtherQualifier
+class RouteNotAddedToAnyCamelContext extends RoutesBuilder {
+ 
+	@Override
+    public void configure() {
+        // ...
+    }
+}]]></script>
+</div></div><p>The&#160;<code>RoutesBuilder</code> beans qualified with&#160;<code>@ContextName</code> get added to the corresponding <code>CamelContext</code> beans. If no such <code>CamelContext</code> bean exists, it gets automatically added by Camel CDI, as for the <code>RouteAdddedToBazCamelContext</code> bean. Note this only happens for the&#160;<code>@ContextName</code>&#160;qualifier provided by Camel CDI. Hence the&#160;<code>RouteNotAddedToAnyCamelContext</code> bean qualified with the user-defined&#160;<code>@MyOtherQualifier</code>&#160;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.</p><p>The CDI qualifiers declared on the&#160;<code>CamelContext</code>&#160;beans are also used to bind the corresponding Camel primitives, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@ContextName(&quot;foo&quot;)
+@Uri(&quot;direct:inbound&quot;)
+ProducerTemplate producerTemplate;
+
+@Inject
+@BarContextQualifier
+MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
+
+@Inject
+@ContextName(&quot;baz&quot;)
+@Uri(&quot;direct:inbound&quot;)
+Endpoint endpoint;]]></script>
+</div></div><h3 id="CDI-Configurationproperties">Configuration properties</h3><p>To configure the sourcing of the configuration properties used by Camel to resolve properties placeholders, you can declare a&#160;<code>PropertiesComponent</code>&#160;bean qualified with <code>@Named("properties")</code>, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Produces
+@ApplicationScoped
+@Named(&quot;properties&quot;)
+PropertiesComponent propertiesComponent() {
+    Properties properties = new Properties();
+    properties.put(&quot;property&quot;, &quot;value&quot;);
+    PropertiesComponent component = new PropertiesComponent();
+    component.setInitialProperties(properties);
+    component.setLocation(&quot;classpath:placeholder.properties&quot;);
+    return component;
+}]]></script>
+</div></div><p>If you want to use&#160;<a shape="rect" class="external-link" href="http://deltaspike.apache.org/documentation/configuration.html">DeltaSpike configuration mechanism</a>&#160;you can declare the following&#160;<code>PropertiesComponent</code> bean:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Produces
+@ApplicationScoped
+@Named(&quot;properties&quot;)
+PropertiesComponent properties(PropertiesParser parser) {
+    PropertiesComponent component = new PropertiesComponent();
+    component.setPropertiesParser(parser);
+    return component;
+}
 
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[
+// PropertiesParser bean that uses DeltaSpike to resolve properties
+static class DeltaSpikeParser extends DefaultPropertiesParser {
     @Override
-    public void configure() throws Exception {
-
-        from(timerUri)
-            .setBody()
-                .simple(&quot;Bean Injected&quot;)
-
-            // Lookup for bean injected by CDI container
-            // The HellowWorld class is annotated using @Named
-            .beanRef(&quot;helloWorld&quot;, &quot;sayHello&quot;)
-
-            .log(&quot;&gt;&gt; Response : ${body}&quot;);
-
+    public String parseProperty(String key, String value, Properties properties) {
+        return ConfigResolver.getPropertyValue(key);
     }
-]]></script>
-</div></div>
-
-<p>Here is the code of the HelloWorld Bean</p>
-
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[
-@Named
-public class HelloWorld {
-
-    public String sayHello(@Body String message) {
-        return &quot;&gt;&gt; Hello &quot; + message + &quot; user.&quot;;
+}]]></script>
+</div></div><div class="highlight highlight-source-java"><div>You can see the&#160;<span><code>camel-example-cdi-properties</code> example for a working example of a Camel CDI application using DeltaSpike configuration mechanism.</span></div></div><h3 id="CDI-Auto-configuredtypeconverters">Auto-configured type converters</h3><p>CDI beans annotated with the&#160;<code>@Converter</code>&#160;annotation are automatically registered into the deployed Camel contexts, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Converter
+public class MyTypeConverter {
+
+    @Converter
+    public Output convert(Input input) {
+        //...
     }
-}
+}]]></script>
+</div></div><p>Note that CDI injection is supported within the type converters.</p><h3 id="CDI-Camelbeanintegration">Camel bean integration</h3><h4 id="CDI-Camelannotations">Camel annotations</h4><p>As part of the Camel&#160;<a shape="rect" class="external-link" href="http://camel.apache.org/bean-integration.html">bean integration</a>,&#160;Camel comes with a set of&#160;<a shape="rect" class="external-link" href="http://camel.apache.org/bean-integration.html#BeanIntegration-Annotations">annotations</a>&#160;that are seamlessly supported by Camel CDI. So you can use any of these annotations in your CDI beans, e.g.:</p><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh">&#160;</th><th colspan="1" rowspan="1" class="confluenceTh">Camel annotation</th><th colspan="1" rowspan="1" class="confluenceTh">CDI equivalent</th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Configuration property</td><td colspan="1" rowspa
 n="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@PropertyInject(&quot;property&quot;)
+String property;
 ]]></script>
-</div></div>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd"><p>If using <a shape="rect" class="external-link" href="http://deltaspike.apache.org/documentation/configuration.html">DeltaSpike configuration mechanism</a>:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@ConfigProperty(name = &quot;message&quot;)
+String property;]]></script>
+</div></div><p>See <a shape="rect" href="#CDI-Configurationproperties">configuration properties</a> for more details.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Producer template injection (default Camel context)</td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Produce(uri = &quot;mock:outbound&quot;)
+ProducerTemplate producer;]]></script>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@Uri(&quot;direct:outbound&quot;)
+ProducerTemplate producer;]]></script>
+</div></div></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Endpoint injection (default Camel context)</td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@EndpointInject(uri = &quot;direct:inbound&quot;)
+Endpoint endpoint;]]></script>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@Uri(&quot;direct:inbound&quot;)
+Endpoint endpoint;]]></script>
+</div></div></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Endpoint injection (Camel context by name)</td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@EndpointInject(uri = &quot;direct:inbound&quot;, context = &quot;foo&quot;)
+Endpoint contextEndpoint;]]></script>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@ContextName(&quot;foo&quot;)
+@Uri(&quot;direct:inbound&quot;)
+Endpoint contextEndpoint;]]></script>
+</div></div></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Bean injection (by type)</td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@BeanInject
+MyBean bean;]]></script>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+MyBean bean;
+]]></script>
+</div></div></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Bean injection (by name)</td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@BeanInject(&quot;foo&quot;)
+MyBean bean;]]></script>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@Named(&quot;foo&quot;)
+MyBean bean;
+]]></script>
+</div></div></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">POJO consuming</td><td colspan="1" rowspan="1" class="confluenceTd"><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Consume(uri = &quot;seda:inbound&quot;)
+void consume(@Body String body) {
+    //...
+}]]></script>
+</div></div></td><td colspan="1" rowspan="1" class="confluenceTd">&#160;</td></tr></tbody></table></div><h4 id="CDI-Beancomponent">Bean component</h4><p>You can refer to CDI beans, either by type or name, From the Camel DSL, e.g. with the Java Camel DSL:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[class MyBean {
+	//...
+}
+ 
+from(&quot;direct:inbound&quot;).bean(MyBean.class);]]></script>
+</div></div><p>Or to lookup a CDI bean by name from the Java DSL:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Named(&quot;foo&quot;)
+class MyBean {
+	//...
+}
+ 
+from(&quot;direct:inbound&quot;).bean(&quot;foo&quot;);]]></script>
+</div></div><h4 id="CDI-ReferringbeansfromEndpointURIs">Referring beans from Endpoint URIs</h4><p><span style="color: rgb(0,0,0);">When configuring endpoints using the URI syntax you can refer to beans in the&#160;</span><a shape="rect" href="registry.html">Registry</a><span style="color: rgb(0,0,0);">&#160;using the <code>#</code> notation.</span><span>&#160;</span><span style="color: rgb(0,0,0);">If the URI parameter value starts with a&#160;</span><code>#</code><span style="color: rgb(0,0,0);">&#160;sign then Camel CDI will lookup</span><span style="color: rgb(0,0,0);">&#160;for a bean of the given type by name, e.g.:</span></p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[from(&quot;jms:queue:{{destination}}?transacted=true&amp;transactionManager=#jtaTransactionManager&quot;).to(&quot;...&quot;);]]></script>
+</div></div><p>Having the following CDI bean qualified with&#160;<code>@Named("jtaTransactionManager")</code>:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Produces
+@Named(&quot;jtaTransactionManager&quot;)
+PlatformTransactionManager createTransactionManager(TransactionManager transactionManager, UserTransaction userTransaction) {
+    JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
+    jtaTransactionManager.setUserTransaction(userTransaction);
+    jtaTransactionManager.setTransactionManager(transactionManager);
+    jtaTransactionManager.afterPropertiesSet();
+    return jtaTransactionManager;
+}]]></script>
+</div></div><h3 id="CDI-CameleventstoCDIevents">Camel events to CDI events</h3><p>Camel provides a set of&#160;<a shape="rect" class="external-link" href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/management/event/package-summary.html">management events</a>&#160;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&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_methods" rel="nofollow">observer methods</a>, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[void onContextStarting(@Observes CamelContextStartingEvent event) {
+    // Called before the default Camel context is about to start
+}]]></script>
+</div></div><p>When multiple Camel contexts exist in the CDI container, the Camel context bean qualifiers, like&#160;<code>@ContextName</code>,&#160;can be used to refine the observer method resolution to a particular Camel context as specified in&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_resolution" rel="nofollow">observer resolution</a>, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[void onRouteStarted(@Observes @ContextName(&quot;foo&quot;) RouteStartedEvent event) {
+    // Called after the route &#39;event.getRoute()&#39; for the Camel context &#39;foo&#39; has started
+}
+ 
+void onContextStarted(@Observes @Manual CamelContextStartedEvent event) {
+    // Called after the the Camel context qualified with &#39;@Manual&#39; has started
+}]]></script>
+</div></div><p>Similarly, the&#160;<code>@Default</code>&#160;qualifier can be used to observe Camel events for the&#160;<em>default</em>&#160;Camel context if multiples contexts exist, e.g.:</p><div class="highlight highlight-source-java"><p>&#160;</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[void onExchangeCompleted(@Observes @Default ExchangeCompletedEvent event) {
+    // Called after the exchange &#39;event.getExchange()&#39; processing has completed
+}]]></script>
+</div></div><p>In that example, if no qualifier is specified, the&#160;<code>@Any</code>&#160;qualifier is implicitly assumed, so that corresponding events for all the Camel contexts get received.</p></div><p>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.</p><h3 id="CDI-CDIeventsendpoint">CDI events endpoint</h3><p>The CDI event endpoint bridges the&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#events" rel="nofollow">CDI events</a>&#160;with the Camel routes so that CDI events can be seamlessly observed / consumed (resp. produced / fired) from Camel consumers (resp. by Camel producers).</p><p>The&#160;<code>CdiEventEndpoint&lt;T&gt;</code>&#160;bean provided by Camel CDI can be used to observe / consume CDI events whose&#160;<em>event type</em>&#160;is&#160;<code>T</code>, for example:</p>
 <div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+CdiEventEndpoint&lt;String&gt; cdiEventEndpoint;
+
+from(cdiEventEndpoint).log(&quot;CDI event received: ${body}&quot;);]]></script>
+</div></div><p>This is equivalent to writing:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@Uri(&quot;direct:event&quot;)
+ProducerTemplate producer;
 
-<p>This project is started using the <a shape="rect" class="external-link" href="http://embedded-glassfish.java.net/nonav/plugindocs/3.1/plugin-info.html" rel="nofollow">GlassFish maven plugin</a> 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 <a shape="rect" class="external-link" href="http://agoncal.wordpress.com/2011/01/12/bootstrapping-cdi-in-several-environments/" rel="nofollow">Java SE</a>.</p>
+void observeCdiEvents(@Observes String event) {
+    producer.sendBody(event);
+}
 
-<h3 id="CDI-SeeAlso">See Also</h3>
+from(&quot;direct:event&quot;).log(&quot;CDI event received: ${body}&quot;);]]></script>
+</div></div><p>Conversely, the&#160;<code>CdiEventEndpoint&lt;T&gt;</code>&#160;bean can be used to produce / fire CDI events whose&#160;<em>event type</em>&#160;is&#160;<code>T</code>, for example:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+CdiEventEndpoint&lt;String&gt; cdiEventEndpoint;
+
+from(&quot;direct:event&quot;).to(cdiEventEndpoint).log(&quot;CDI event sent: ${body}&quot;);]]></script>
+</div></div><p>This is equivalent to writing:</p><div class="highlight highlight-source-java"><p>&#160;</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+Event&lt;String&gt; event;
 
-<ul><li>Simple <a shape="rect" class="external-link" href="https://github.com/cmoulliard/cdi-camel-example/" rel="nofollow">Camel CDI BootStrap project</a></li><li><a shape="rect" class="external-link" href="http://docs.jboss.org/weld/reference/1.1.5.Final/en-US/html_single/" rel="nofollow">JSR299</a> and <a shape="rect" class="external-link" href="http://openwebbeans.apache.org/owb/jsr330.html">JSR330</a> reference documentations</li><li><a shape="rect" class="external-link" href="http://deltaspike.apache.org">Apache DeltaSpike project</a> - CDI extensions and JavaSE BootStrap</li><li>CDI revealed by Antonio Goncalves - <a shape="rect" class="external-link" href="https://agoncal.wordpress.com/2011/04/07/injection-with-cdi-part-i/" rel="nofollow">part 1</a>, <a shape="rect" class="external-link" href="https://agoncal.wordpress.com/2011/05/03/injection-with-cdi-part-ii/" rel="nofollow">part 2</a>, <a shape="rect" class="external-link" href="https://agoncal.wordpress.com/2011/09/25/in
 jection-with-cdi-part-iii/" rel="nofollow">part 3</a> and OpenEJB team - see <a shape="rect" class="external-link" href="http://openejb.apache.org/examples-trunk/index.html">examples</a></li><li>Apache implementation of the specs JSR299, 330 - <a shape="rect" class="external-link" href="http://openwebbeans.apache.org">OpenWebbeans</a> and Apache <a shape="rect" class="external-link" href="http://openejb.apache.org/">OpenEJB</a> which provide the container to deploy CDI projects</li><li>Apache Karaf featured with OpenEJB and CDI - <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/openejb/trunk/openejb/osgi/">Apache KarafEE</a></li></ul></div>
+from(&quot;direct:event&quot;).process(new Processor() {
+    @Override
+    public void process(Exchange exchange) {
+        event.fire(exchange.getBody(String.class));
+    }
+}).log(&quot;CDI event sent: ${body}&quot;);]]></script>
+</div></div><p>Or using a Java 8 lambda expression:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+Event&lt;String&gt; event;
+
+from(&quot;direct:event&quot;)
+    .process(exchange -&gt; event.fire(exchange.getIn().getBody(String.class)))
+    .log(&quot;CDI event sent: ${body}&quot;);]]></script>
+</div></div><p>The type variable&#160;<code>T</code> (resp. the qualifiers) of a particular&#160;<code>CdiEventEndpoint&lt;T&gt;</code>&#160;injection point are automatically translated into the parameterized&#160;<em>event type</em>&#160;(resp. into the&#160;<em>event qualifiers</em>)&#160;e.g.:</p></div><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@FooQualifier
+CdiEventEndpoint&lt;List&lt;String&gt;&gt; cdiEventEndpoint;
+
+from(&quot;direct:event&quot;).to(cdiEventEndpoint);
+
+void observeCdiEvents(@Observes @FooQualifier List&lt;String&gt; event) {
+    logger.info(&quot;CDI event: {}&quot;, event);
+}]]></script>
+</div></div><p>When multiple Camel contexts exist in the CDI container, the Camel context bean qualifiers, like&#160;<code>@ContextName</code>,&#160;can be used to qualify the&#160;<code>CdiEventEndpoint&lt;T&gt;</code>&#160;injection points, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[@Inject
+@ContextName(&quot;foo&quot;)
+CdiEventEndpoint&lt;List&lt;String&gt;&gt; cdiEventEndpoint;
+// Only observes / consumes events having the @ContextName(&quot;foo&quot;) qualifier
+from(cdiEventEndpoint).log(&quot;Camel context &#39;foo&#39; &gt; CDI event received: ${body}&quot;);
+// Produces / fires events with the @ContextName(&quot;foo&quot;) qualifier
+from(&quot;...&quot;).to(cdiEventEndpoint);
+
+void observeCdiEvents(@Observes @ContextName(&quot;foo&quot;) List&lt;String&gt; event) {
+    logger.info(&quot;Camel context &#39;foo&#39; &gt; CDI event: {}&quot;, event);
+}]]></script>
+</div></div><p>Note that the CDI event Camel endpoint dynamically adds an&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_methods" rel="nofollow">observer method</a>&#160;for each unique combination of&#160;<em>event type</em>&#160;and&#160;<em>event qualifiers</em>&#160;and solely relies on the container typesafe&#160;<a shape="rect" class="external-link" href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_resolution" rel="nofollow">observer resolution</a>, which leads to an implementation as efficient as possible.</p><p>Besides, as the impedance between the&#160;<em>typesafe</em>&#160;nature of CDI and the&#160;<em>dynamic</em>&#160;nature of the&#160;<a shape="rect" class="external-link" href="http://camel.apache.org/component.html">Camel component</a>&#160;model is quite high, it is not possible to create an instance of the CDI event Camel endpoint via&#160;<a shape="rect" class="external-link" href="http://c
 amel.apache.org/uris.html">URIs</a>. Indeed, the URI format for the CDI event component is:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: text; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[cdi-event://PayloadType&lt;T1,...,Tn&gt;[?qualifiers=QualifierType1[,...[,QualifierTypeN]...]]]]></script>
+</div></div><p>With the authority&#160;<code>PayloadType</code>&#160;(resp. the&#160;<code>QualifierType</code>) 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&#160;<em>unfriendly</em>&#160;URIs, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: text; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[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]]></script>
+</div></div><p>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.</p><h3 id="CDI-Auto-configuredOSGiintegration">Auto-configured OSGi integration</h3><p>The Camel context beans are automatically adapted by Camel CDI so that they are registered as OSGi services and the various resolvers (like&#160;<code>ComponentResolver</code> and&#160;<code>DataFormatResolver</code>) integrate with the OSGi registry. That means that the <a shape="rect" href="karaf.html#Karaf-Karafcommands">Karaf Camel commands</a> can be used to operate the Camel contexts auto-configured by Camel CDI, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: text; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[karaf@root()&gt; camel:context-list
+ Context        Status              Total #       Failed #     Inflight #   Uptime        
+ -------        ------              -------       --------     ----------   ------        
+ camel-cdi      Started                   1              0              0   1 minute  ]]></script>
+</div></div><p>See the&#160;<span><code>camel-example-cdi-osgi</code> example for a working example of the Camel CDI OSGi integration.</span></p><h3 id="CDI-MavenArchetype">Maven Archetype</h3><p>Among the available&#160;<a shape="rect" href="camel-maven-archetypes.html">Camel Maven archetypes</a>, you can use the provided&#160;<code>camel-archetype-cdi</code>&#160;to generate a Camel CDI Maven project, e.g.:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: bash; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-cdi]]></script>
+</div></div><h3 id="CDI-Supportedcontainers">Supported containers</h3><p>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:</p><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh">Container</th><th colspan="1" rowspan="1" class="confluenceTh">Version</th><th colspan="1" rowspan="1" class="confluenceTh">Runtime</th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Weld SE</td><td colspan="1" rowspan="1" class="confluenceTd"><code>1.1.28.Final</code></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.0 / Java SE 7</td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">OpenWebBeans</td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>1.2.7</code></p></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.0 / Java SE 7</td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Weld S
 E</td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>2.3.2.Final</code></p></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.2 / Java SE 7</td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">OpenWebBeans</td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>1.6.2</code></p></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.2 / Java SE 7</td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">WildFly</td><td colspan="1" rowspan="1" class="confluenceTd"><code>8.2.1.Final</code></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.2 / Java EE 7</td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">WildFly</td><td colspan="1" rowspan="1" class="confluenceTd"><code>9.0.1.Final</code></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.2 / Java EE 7</td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Karaf</td><td colspan="1" rowspan="1" class="confluenceTd"><code>2.4.4</code></td><td colspan="1" rowspan=
 "1" class="confluenceTd">CDI 1.2 / <span>OSGi 4 / PAX CDI</span></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Karaf</td><td colspan="1" rowspan="1" class="confluenceTd"><code>3.0.5</code></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.2 / <span>OSGi 5 / PAX CDI</span></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd">Karaf</td><td colspan="1" rowspan="1" class="confluenceTd"><code>4.0.4</code></td><td colspan="1" rowspan="1" class="confluenceTd">CDI 1.2 / <span>OSGi 6 / PAX CDI</span></td></tr></tbody></table></div><h3 id="CDI-Examples">Examples</h3><p>The following examples are available in the <code>examples</code> directory of the Camel project:</p><ul><li><code>camel-example-cdi-metrics</code> - illustrates the integration between Camel, Dropwizard Metrics and CDI,</li><li><code>camel-example-cdi-properties</code> -&#160;illustrates the integration between Camel, DeltaSpike and CDI for configuration properties,</li><li><code>camel-example
 -cdi-osgi</code> - a&#160;CDI application using the SJMS component that can be executed inside an OSGi container using PAX CDI,</li><li><code>camel-example-cdi-rest-servlet</code> -&#160;illustrates the Camel REST DSL being used in a Web application that uses CDI as dependency injection framework,</li><li><code>camel-example-widget-gadget-cdi</code> - The Widget and Gadget use-case from the EIP book implemented in Java with CDI dependency Injection.</li></ul><h3 id="CDI-SeeAlso">See Also</h3><ul><li><a shape="rect" class="external-link" href="http://www.cdi-spec.org" rel="nofollow">CDI Web site</a></li></ul></div>
         </td>
         <td valign="top">
           <div class="navigation">