camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Advanced configuration of CamelContext using Spring
Date Wed, 13 Jan 2010 13:12:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Advanced+configuration+of+CamelContext+using+Spring">Advanced
configuration of CamelContext using Spring</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="AdvancedconfigurationofCamelContextusingSpring-AdvancedconfigurationofCamelContextusingSpring"></a>Advanced
configuration of CamelContext using Spring</h2>

<p>When using Spring the CamelContext can be pre configured based on defined beans in
spring XML.<br/>
This wiki page documentes these features. Most of these features requires <b>Camel 2.0</b>.</p>

<h3><a name="AdvancedconfigurationofCamelContextusingSpring-Whatcanbeconfigured"></a>What
can be configured</h3>
<p>The following functions can be configured:</p>
<ul class="alternate" type="square">
	<li>Class resolvers</li>
	<li>Lifecycle factories</li>
	<li>Registry for lookup</li>
	<li><a href="/confluence/display/CAMEL/Debugger" title="Debugger">Debugger</a>,
<a href="/confluence/display/CAMEL/Tracer" title="Tracer">Tracer</a> and <a
href="/confluence/display/CAMEL/Delay+Interceptor" title="Delay Interceptor">Delay</a></li>
	<li><a href="/confluence/display/CAMEL/Intercept" title="Intercept">Intercept</a></li>
	<li><a href="/confluence/display/CAMEL/Graceful+Shutdown" title="Graceful Shutdown">Graceful
Shutdown</a></li>
</ul>


<p>Camel will configure these functions by doing a lookup in the Spring bean registry
to find beans of the given type<br/>
When Camel finds and uses any of these it logs at <tt>INFO</tt> level.</p>

<p>The following list all requires at most 1 beans defined. If there are more than 1
bean of this type, then Camel will <b>not</b> use it. </p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Number of beans </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> PackageScanClassResolver </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd party package scan resolver. More details at
<a href="/confluence/display/CAMEL/Pluggable+Class+Resolvers" title="Pluggable Class Resolvers">Pluggable
Class Resolvers</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> ClassResolver </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd party class resolver. More details at <a href="/confluence/display/CAMEL/Pluggable+Class+Resolvers"
title="Pluggable Class Resolvers">Pluggable Class Resolvers</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> FactoryFinderResolver </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd party factory finder. </td>
</tr>
<tr>
<td class='confluenceTd'> Registry </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd party bean registry. By default Camel will use
Spring ApplicationContext as registry. </td>
</tr>
<tr>
<td class='confluenceTd'> Debugger </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a <a href="/confluence/display/CAMEL/Debugger" title="Debugger">Debugger</a>
usually for tooling. </td>
</tr>
<tr>
<td class='confluenceTd'> Tracer </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd party <a href="/confluence/display/CAMEL/Tracer"
title="Tracer">Tracer</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> TraceFormatter </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a bean that has the tracing options configured. </td>
</tr>
<tr>
<td class='confluenceTd'> HandleFault </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd part fault handler to handle FAULT messages.
</td>
</tr>
<tr>
<td class='confluenceTd'> Delayer </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> To use a 3rd part <a href="/confluence/display/CAMEL/Delayer"
title="Delayer">Delayer</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> ManagementStrategy </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> To use a 3rd part strategy for
<a href="/confluence/display/CAMEL/Camel+JMX" title="Camel JMX">management</a>,
for example JMX management.</td>
</tr>
<tr>
<td class='confluenceTd'> EventFactory </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> To use a 3rd part event factory.
</td>
</tr>
<tr>
<td class='confluenceTd'> EventNotifier </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> To use a 3rd part event notifier.
In Camel 2.2 onwards you can have multiple notifiers, see next table.</td>
</tr>
<tr>
<td class='confluenceTd'> InflightRepository </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> To use a 3rd part inflight repository.
</td>
</tr>
<tr>
<td class='confluenceTd'> ShutdownStrategy </td>
<td class='confluenceTd'> 0..1 </td>
<td class='confluenceTd'> <b>Camel 2.2:</b> To use a 3rd part shutdown strategy.
</td>
</tr>
</tbody></table>

<p>And the following options have support for any number of beans defined.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Number of beans </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> InterceptStrategy </td>
<td class='confluenceTd'> 0..n </td>
<td class='confluenceTd'> To use your own <a href="/confluence/display/CAMEL/Intercept"
title="Intercept">Intercept</a> that intercepts every processing steps in all routes
in the <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>.
For instance you can use this to do an AOP like performance timer interceptor. </td>
</tr>
<tr>
<td class='confluenceTd'> LifecycleStrategy </td>
<td class='confluenceTd'> 0..n </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> To use 3rd party lifecycle strategies.
By default Camel uses a JMX aware that does JMX instrumentation. </td>
</tr>
<tr>
<td class='confluenceTd'> EventNotifier </td>
<td class='confluenceTd'> 0..n </td>
<td class='confluenceTd'> <b>Camel 2.2:</b> To use 3rd part event notifiers.
</td>
</tr>
</tbody></table>

<p>Camel will log at <tt>INFO</tt> level if it pickup and uses a custom
bean using <tt>org.apache.camel.spring.CamelContextFactoryBean</tt> as name.</p>

<h3><a name="AdvancedconfigurationofCamelContextusingSpring-Usingcontainerwideinterceptors"></a>Using
container wide interceptors</h3>
<p>Imagine that you have multiple <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> and you want to configure that they all use
the same container wide interceptor. How do we do that? Well we can leverage the fact that
Camel can auto detect and use custom interceptors. So what we simply do is to define our interceptor
in the spring xml file. The sample below does this and also define 2 camel contexts. The sample
is based on unit test.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Spring DSL</b></div><div class="codeContent
panelContent">
<pre class="code-xml">
&lt;!-- here we define a spring bean that is our container wide interceptor
     its important to notice that the class ContainerWideInterceptor implements
     org.apache.camel.spi.InterceptStrategy that allows us to plugin our interceptors
     Camel will at startup automatic look for any beans in spring registry that is an
     instance of org.apache.camel.spi.InterceptStrategy and add it as interceptor
     to all its routes. Using this we are capable of defining container wide interceptors
     that gets used in all camel contests we define with spring --&gt;
<span class="code-tag">&lt;bean id=<span class="code-quote">"myInterceptor"</span>
class=<span class="code-quote">"org.apache.camel.spring.interceptor.ContainerWideInterceptor"</span>/&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- here we have the
1st CamelContext --&gt;</span></span>
<span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel1"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:one"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- and there we have
the 2nd CamelContext --&gt;</span></span>
<span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel2"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:two"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"log:two"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>Okay lets build our interceptor to simply count the number of interceptions. This
is quite easy as we can just implement this logic in our implementation directly as the code
below illustrates:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>InterceptorStrategy</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class ContainerWideInterceptor
<span class="code-keyword">implements</span> InterceptStrategy {

    <span class="code-keyword">private</span> <span class="code-keyword">static</span>
<span class="code-keyword">final</span> <span class="code-keyword">transient</span>
Log LOG = LogFactory.getLog(ContainerWideInterceptor.class);
    <span class="code-keyword">private</span> <span class="code-keyword">static</span>
<span class="code-object">int</span> count;

    <span class="code-keyword">public</span> Processor wrapProcessorInInterceptors(<span
class="code-keyword">final</span> CamelContext context, <span class="code-keyword">final</span>
ProcessorDefinition definition,
                                                 <span class="code-keyword">final</span>
Processor target, <span class="code-keyword">final</span> Processor nextTarget)
<span class="code-keyword">throws</span> Exception {

        <span class="code-comment">// as <span class="code-keyword">this</span>
is based on an unit test we are a bit lazy and just create an inlined processor
</span>        <span class="code-comment">// where we implement our interception
logic.
</span>        <span class="code-keyword">return</span> <span class="code-keyword">new</span>
Processor() {
            <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
                <span class="code-comment">// we just count number of interceptions
</span>                count++;
                LOG.info(<span class="code-quote">"I am the container wide interceptor.
Intercepted total count: "</span> + count);
                <span class="code-comment">// its important that we delegate to the
real target so we let target process the exchange
</span>                target.process(exchange);
            }

            @Override
            <span class="code-keyword">public</span> <span class="code-object">String</span>
toString() {
                <span class="code-keyword">return</span> <span class="code-quote">"ContainerWideInterceptor["</span>
+ target + <span class="code-quote">"]"</span>;
            }
        };
    }

    <span class="code-keyword">public</span> <span class="code-object">int</span>
getCount() {
        <span class="code-keyword">return</span> count;
    }
}
</pre>
</div></div>

<p>When Camel boots up it logs at <tt>INFO</tt> level the container wide
interceptors it have found:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
INFO  CamelContextFactoryBean        - Using custom intercept strategy with id: myInterceptor
and implementation:org.apache.camel.spring.interceptor.ContainerWideInterceptor@b84c44
</pre>
</div></div>

<p><b>Notice:</b> If we have more than 1 container wide interceptor, we
can just define them as spring bean. Camel will find and use them.</p>

<h2><a name="AdvancedconfigurationofCamelContextusingSpring-SeeAlso"></a>See
Also</h2>
<ul>
	<li><a href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-JmsRemoting" title="Tutorial-JmsRemoting">Spring
JMS Tutorial</a></li>
	<li><a href="/confluence/display/CAMEL/Creating+a+new+Spring+based+Camel+Route"
title="Creating a new Spring based Camel Route">Creating a new Spring based Camel Route</a></li>
	<li><a href="/confluence/display/CAMEL/Spring+Example" title="Spring Example">Spring
example</a></li>
	<li><a href="/confluence/display/CAMEL/Xml+Reference" title="Xml Reference">Xml
Reference</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Advanced+configuration+of+CamelContext+using+Spring">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=120229&revisedVersion=14&originalVersion=13">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Advanced+configuration+of+CamelContext+using+Spring?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message