camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Guice
Date Tue, 29 Jan 2013 19:32:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/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/Guice">Guice</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bvahdat">Babak
Vahdat</a>
    </h4>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >h2. Camel Guice <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >We have support for [Google Guice|http://code.google.com/p/google-guice/]
as a dependency injection framework. <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">To
use it just be dependent on *camel-guice.jar* which also depends on [the following jars|http://activemq.apache.org/camel/maven/camel-guice/dependencies.html].</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Maven
users will need to add the following dependency to their {{pom.xml}} for this component: <br>{code:xml}
<br>&lt;dependency&gt; <br>    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
<br>    &lt;artifactId&gt;camel-guice&lt;/artifactId&gt; <br>
   &lt;version&gt;x.x.x&lt;/version&gt; <br>    &lt;!-- use the
same version as your Camel core version --&gt; <br>&lt;/dependency&gt; <br>{code}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Dependency Injecting Camel with
Guice <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Guice-CamelGuice"></a>Camel Guice</h2>

<p>We have support for <a href="http://code.google.com/p/google-guice/" class="external-link"
rel="nofollow">Google Guice</a> as a dependency injection framework.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-guice<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="Guice-DependencyInjectingCamelwithGuice"></a>Dependency Injecting
Camel with Guice</h3>

<p>The <a href="http://camel.apache.org/maven/current/camel-guice/apidocs/org/apache/camel/guice/GuiceCamelContext.html"
class="external-link" rel="nofollow">GuiceCamelContext</a> is designed to work nicely
inside Guice. You then need to bind it using some Guice Module.</p>

<p>The camel-guice library comes with a number of reusable Guice Modules you can use
if you wish - or you can bind the GuiceCamelContext yourself in your own module.</p>

<ul>
	<li><a href="http://camel.apache.org/maven/current/camel-guice/apidocs/org/apache/camel/guice/CamelModule.html"
class="external-link" rel="nofollow">CamelModule</a> is the base module which binds
the GuiceCamelContext but leaves it up you to bind the RouteBuilder instances</li>
	<li><a href="hhttp://camel.apache.org/maven/current/camel-guice/apidocs/org/apache/camel/guice/CamelModuleWithRouteTypes.html"
class="external-link" rel="nofollow">CamelModuleWithRouteTypes</a> extends CamelModule
so that in the constructor of the module you specify the RouteBuilder classes or instances
to use</li>
	<li><a href="http://camel.apache.org/maven/current/camel-guice/apidocs/org/apache/camel/guice/CamelModuleWithMatchingRoutes.html"
class="external-link" rel="nofollow">CamelModuleWithMatchingRoutes</a> extends CamelModule
so that all bound RouteBuilder instances will be injected into the CamelContext or you can
supply an optional Matcher to find RouteBuilder instances matching some kind of predicate.</li>
</ul>


<p>So you can specify the exact <a href="/confluence/display/CAMEL/RouteBuilder"
title="RouteBuilder">RouteBuilder</a> instances you want</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Injector injector = Guice.createInjector(<span class="code-keyword">new</span>
CamelModuleWithRouteTypes(MyRouteBuilder.class, AnotherRouteBuilder.class));
<span class="code-comment">// <span class="code-keyword">if</span> required
you can lookup the CamelContext
</span>CamelContext camelContext = injector.getInstance(CamelContext.class);
</pre>
</div></div>

<p>Or inject them all</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Injector injector = Guice.createInjector(<span class="code-keyword">new</span>
CamelModuleWithRouteTypes());
<span class="code-comment">// <span class="code-keyword">if</span> required
you can lookup the CamelContext
</span>CamelContext camelContext = injector.getInstance(CamelContext.class);
</pre>
</div></div>

<p>You can then use Guice in the usual way to inject the route instances or any other
dependent objects.</p>

<h3><a name="Guice-BootstrappingwithJNDI"></a>Bootstrapping with JNDI</h3>

<p>A common pattern used in J2EE is to bootstrap your application or root objects by
looking them up in JNDI. This has long been the approach when working with JMS for example
- looking up the JMS ConnectionFactory in JNDI for example.</p>

<p>You can follow a similar pattern with Guice using the <a href="http://code.google.com/p/guiceyfruit/wiki/GuiceyJndi"
class="external-link" rel="nofollow">GuiceyFruit JNDI Provider</a> which lets you
bootstrap Guice from a <b>jndi.properties</b> file which can include the Guice
Modules to create along with environment specific properties you can inject into your modules
and objects.</p>

<p>If the <b>jndi.properties</b> is conflict with other component, you can
specify the jndi properties file name in the Guice Main with option -j or -jndiProperties
with the properties file location to let Guice Main to load right jndi properties file.</p>

<h3><a name="Guice-ConfiguringComponent%2CEndpointorRouteBuilderinstances"></a>Configuring
Component, Endpoint or RouteBuilder instances</h3>

<p>You can use <a href="/confluence/display/CAMEL/Guice" title="Guice">Guice</a>
to dependency inject whatever objects you need to create, be it an <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a>, <a href="/confluence/display/CAMEL/Component" title="Component">Component</a>,
<a href="/confluence/display/CAMEL/RouteBuilder" title="RouteBuilder">RouteBuilder</a>
or arbitrary <a href="/confluence/display/CAMEL/Bean+Integration" title="Bean Integration">bean
used within a route</a>. </p>

<p>The easiest way to do this is to create your own Guice Module class which extends
one of the above module classes and add a provider method for each object you wish to create.
A provider method is annotated with <b>@Provides</b> as follows</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class MyModule <span class="code-keyword">extends</span>
CamelModuleWithMatchingRoutes {

    @Provides
    @JndiBind(<span class="code-quote">"jms"</span>)
    JmsComponent jms(@Named(<span class="code-quote">"activemq.brokerURL"</span>)
<span class="code-object">String</span> brokerUrl) {
        <span class="code-keyword">return</span> JmsComponent.jmsComponent(<span
class="code-keyword">new</span> ActiveMQConnectionFactory(brokerUrl));
    }
}
</pre>
</div></div>

<p>You can optionally annotate the method with <b>@JndiBind</b> to bind
the object to JNDI at some name if the object is a component, endpoint or bean you wish to
refer to by name in your routes.</p>

<p>You can inject any environment specific properties (such as URLs, machine names,
usernames/passwords and so forth) from the jndi.properties file easily using the <b>@Named</b>
annotation as shown above. This allows most of your configuration to be in Java code which
is typesafe and easily refactorable - then leaving some properties to be environment specific
(the jndi.properties file) which you can then change based on development, testing, production
etc.</p>


<h3><a name="Guice-CreatingmultipleRouteBuilderinstancespertype"></a>Creating
multiple RouteBuilder instances per type</h3>

<p>It is sometimes useful to create multiple instances of a particular <a href="/confluence/display/CAMEL/RouteBuilder"
title="RouteBuilder">RouteBuilder</a> with different configurations.</p>

<p>To do this just create multiple provider methods for each configuration; or create
a single provider method that returns a collection of RouteBuilder instances.</p>

<p>For example</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.camel.guice.CamelModuleWithMatchingRoutes;
<span class="code-keyword">import</span> com.google.common.collect.Lists;

<span class="code-keyword">public</span> class MyModule <span class="code-keyword">extends</span>
CamelModuleWithMatchingRoutes {

    @Provides
    @JndiBind(<span class="code-quote">"foo"</span>)
    Collection&lt;RouteBuilder&gt; foo(@Named(<span class="code-quote">"fooUrl"</span>)
<span class="code-object">String</span> fooUrl) {
        <span class="code-keyword">return</span> Lists.newArrayList(<span class="code-keyword">new</span>
MyRouteBuilder(fooUrl), <span class="code-keyword">new</span> MyRouteBuilder(<span
class="code-quote">"activemq:CheeseQueue"</span>));
    }
}
</pre>
</div></div>

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

<ul>
	<li>there are a number of <a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a>
you can look at to see Guice and Camel being used such as <a href="/confluence/display/CAMEL/Guice+JMS+Example"
title="Guice JMS Example">Guice JMS Example</a></li>
	<li><a href="/confluence/display/CAMEL/Guice+Maven+Plugin" title="Guice Maven Plugin">Guice
Maven Plugin</a> for running your Guice based routes via Maven</li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Guice">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=92801&revisedVersion=17&originalVersion=16">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Guice?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message