camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Mueller (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > CDI
Date Mon, 16 Sep 2013 19:40:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/CDI">CDI</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >* Simple [Camel CDI BootStrap project|https://github.com/cmoulliard/cdi-camel-example/]
<br>* [JSR299|http://docs.jboss.org/weld/reference/1.1.5.Final/en-US/html_single/] and
[JSR330|http://openwebbeans.apache.org/owb/jsr330.html] reference documentations <br></td></tr>
            <tr><td class="diff-changed-lines" >* [Apache DeltaSpike <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">project|http://incubator.apache.org/deltaspike/]</span>
<span class="diff-added-words"style="background-color: #dfd;">project|http://deltaspike.apache.org]</span>
- CDI extensions and JavaSE BootStrap <br></td></tr>
            <tr><td class="diff-unchanged" >* CDI revealed by Antonio Goncalves
- [part 1|https://agoncal.wordpress.com/2011/04/07/injection-with-cdi-part-i/], [part 2|https://agoncal.wordpress.com/2011/05/03/injection-with-cdi-part-ii/],
[part 3|https://agoncal.wordpress.com/2011/09/25/injection-with-cdi-part-iii/] and OpenEJB
team - see [examples|http://openejb.apache.org/examples-trunk/index.html] <br>* Apache
implementation of the specs JSR299, 330 - [OpenWebbeans|http://openwebbeans.apache.org/owb/index.html]
and Apache [OpenEJB|http://openejb.apache.org/] which provide the container to deploy CDI
projects <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="CDI-CamelCDI"></a>Camel CDI</h2>

<p>As of 2.10 we now have support <a href="http://jcp.org/en/jsr/detail?id=299" class="external-link"
rel="nofollow">Contexts and Dependency Injection - JSR299</a> and <a href="http://jcp.org/en/jsr/detail?id=330"
class="external-link" 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 href="http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition"
class="external-link" rel="nofollow">Java EE 6 containers</a> but also in standalone
Java SE or <a href="http://openwebbeans.apache.org" class="external-link" rel="nofollow">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><a name="CDI-DependencyInjectingCamelwithCDI"></a>Dependency Injecting
Camel with CDI</h3>

<p>Basically, two things should be done to use Apache Camel in a CDI environment. First,
we just need to create a <a href="https://github.com/cmoulliard/cdi-camel/blob/master/src/main/java/com/fusesource/cdi/camel/simple/BootStrap.java"
class="external-link" 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 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"
class="external-link" rel="nofollow">CdiCamelContext</a> when instantiated will add
a CDI <a href="http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html"
class="external-link" 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>

<h3><a name="CDI-BootstrappingCamelwithCDIcontainer"></a>Bootstrapping Camel
with CDI container</h3>

<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 href="http://docs.oracle.com/javaee/6/api/javax/ejb/Singleton.html"
class="external-link" 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 href="http://docs.oracle.com/javaee/6/api/javax/ejb/Startup.html" class="external-link"
rel="nofollow">javax.ejb.Startup</a>, the container will start the camel context
at the startup of the CDI container. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    @Singleton
    @Startup
    public class BootStrap {
    ...
</pre>
</div></div>

<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>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    @PostConstruct
    public void init() throws Exception {
            logger.info("&gt;&gt; Create CamelContext and register Camel Route.");

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

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

            // Start Camel Context
            camelCtx.start();
</pre>
</div></div>

<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>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    @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("&gt;&gt; Response : ${body}");

    }
</pre>
</div></div>

<p>Here is the code of the HelloWorld Bean</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
@Named
public class HelloWorld {

    public String sayHello(@Body String message) {
        return "&gt;&gt; Hello " + message + " user.";
    }
}
</pre>
</div></div>

<p>This project is started using the <a href="http://embedded-glassfish.java.net/nonav/plugindocs/3.1/plugin-info.html"
class="external-link" 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 href="http://agoncal.wordpress.com/2011/01/12/bootstrapping-cdi-in-several-environments/"
class="external-link" rel="nofollow">Java SE</a>.</p>

<h3><a name="CDI-SeeAlso"></a>See Also</h3>

<ul>
	<li>Simple <a href="https://github.com/cmoulliard/cdi-camel-example/" class="external-link"
rel="nofollow">Camel CDI BootStrap project</a></li>
	<li><a href="http://docs.jboss.org/weld/reference/1.1.5.Final/en-US/html_single/"
class="external-link" rel="nofollow">JSR299</a> and <a href="http://openwebbeans.apache.org/owb/jsr330.html"
class="external-link" rel="nofollow">JSR330</a> reference documentations</li>
	<li><a href="http://deltaspike.apache.org" class="external-link" rel="nofollow">Apache
DeltaSpike project</a> - CDI extensions and JavaSE BootStrap</li>
	<li>CDI revealed by Antonio Goncalves - <a href="https://agoncal.wordpress.com/2011/04/07/injection-with-cdi-part-i/"
class="external-link" rel="nofollow">part 1</a>, <a href="https://agoncal.wordpress.com/2011/05/03/injection-with-cdi-part-ii/"
class="external-link" rel="nofollow">part 2</a>, <a href="https://agoncal.wordpress.com/2011/09/25/injection-with-cdi-part-iii/"
class="external-link" rel="nofollow">part 3</a> and OpenEJB team - see <a href="http://openejb.apache.org/examples-trunk/index.html"
class="external-link" rel="nofollow">examples</a></li>
	<li>Apache implementation of the specs JSR299, 330 - <a href="http://openwebbeans.apache.org/owb/index.html"
class="external-link" rel="nofollow">OpenWebbeans</a> and Apache <a href="http://openejb.apache.org/"
class="external-link" rel="nofollow">OpenEJB</a> which provide the container to deploy
CDI projects</li>
	<li>Apache Karaf featured with OpenEJB and CDI - <a href="https://svn.apache.org/repos/asf/openejb/trunk/openejb/osgi/"
class="external-link" rel="nofollow">Apache KarafEE</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CDI">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27846495&revisedVersion=8&originalVersion=7">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CDI?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message