camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Threading Model
Date Fri, 29 Jul 2011 07:47: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/Threading+Model">Threading
Model</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~christian%2Bschneider">Christian
Schneider</a>
    </h4>
        <br/>
                         <h4>Changes (2)</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" >All the thread pools that Camel creates
are managed and thus you can see them in JConsole under the {{threadpools}} category. <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >h3. ExecutorServiceStrategy (
Camel <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&lt;=</span>
<span class="diff-added-words"style="background-color: #dfd;">&lt;</span>
2.9.0 ) <br></td></tr>
            <tr><td class="diff-unchanged" >Camel provides a pluggable strategy
to hook in your own thread pool provider, for example from a WorkManager in a J2EE server
etc. <br>See the {{org.apache.camel.spi.ExecutorServiceStrategy}} interface which you
should implement and hook into the WorkManager. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >You can configure it on the {{CamelContext}}
from Java DSL using the getter/setter. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >h3. ExecutorServiceManager ( Camel
<span class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">&lt;</span><span
class="diff-added-chars"style="background-color: #dfd;">&gt;</span>=</span>
2.9.0 ) <br></td></tr>
            <tr><td class="diff-unchanged" > <br>In camel 2.9.0 the ExecutorServiceManager
replaces the ExecutorServiceStrategy. It is renamed to manager as is not only provides a strategy
for threadPool creation but also keeps track of thread pools and thread pool profiles. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="ThreadingModel-ThreadingModel"></a>Threading Model</h2>
<p><b>Available as of Camel 2.3</b></p>

<p>The threading model in Camel is based on leveraging the JDK concurrency API which
provides thread pools, named <tt>ExecutorService</tt>.</p>

<p>Camel leverages thread pools in the following places:</p>
<ul class="alternate" type="square">
	<li>several <a href="/confluence/display/CAMEL/EIP" title="EIP">EIP</a>
patterns supports using thread pools for concurrency</li>
	<li><a href="/confluence/display/CAMEL/SEDA" title="SEDA">SEDA</a> component
for asynchronous connectivity</li>
	<li><a href="/confluence/display/CAMEL/Async" title="Async">Threads DSL</a>
in the Camel route</li>
	<li><a href="/confluence/display/CAMEL/ServicePool" title="ServicePool">ServicePool</a>
for pooling services</li>
	<li>And some component provide thread pools by nature such as <a href="/confluence/display/CAMEL/JMS"
title="JMS">JMS</a>, <a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a></li>
</ul>


<h3><a name="ThreadingModel-Threadpoolprofiles"></a>Thread pool profiles</h3>
<p>By default when a thread pool is to be created then its based on the default thread
pool profile which is:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    &lt;threadPoolProfile id=<span class="code-quote">"defaultThreadPoolProfile"</span>
defaultProfile=<span class="code-quote">"true"</span>
                       poolSize=<span class="code-quote">"10"</span> maxPoolSize=<span
class="code-quote">"20"</span> maxQueueSize=<span class="code-quote">"1000"</span>
                       rejectedPolicy=<span class="code-quote">"CallerRuns"</span>/&gt;
</pre>
</div></div>

<p>What that means is that for example when you use <a href="/confluence/display/CAMEL/Multicast"
title="Multicast">Multicast</a> with <tt>parallelProcessing=true</tt>
enabled, then it would create a thread pool based on the profile above. The <tt>rejectedPolicy</tt>
have four options: <tt>Abort, CallerRuns, Discard, DiscardOldest</tt> which corresponds
to the same four options provided out of the box in the JDK. </p>

<p>You can define as many thread pool profiles as you like. But there must only <b>one</b>
default profile. A custom thread pool profile will inherit from the default profile. Which
means that any option you do not explicit define will fallback and use the option from the
default profile.</p>

<p>You can use <tt>-1</tt> in maxQueueSize to indicate a unbounded queue.</p>

<p>In Java DSL you can configure the default thread pool profile from the <tt>ExecutorServiceStartegy</tt>
which you access from <tt>CamelContext</tt>.</p>

<h3><a name="ThreadingModel-Usingthreadpoolprofiles"></a>Using thread pool
profiles</h3>

<p>Suppose you want to use a custom thread pool profile for a Multicast EIP pattern
in a Camel route you can do it using the <tt>executorServiceRef</tt> attribute
as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext ...&gt;</span>
    ...
    &lt;threadPoolProfile id=<span class="code-quote">"fooProfile"</span>

                       poolSize=<span class="code-quote">"20"</span> maxPoolSize=<span
class="code-quote">"50"</span> maxQueueSize=<span class="code-quote">"-1"</span>/&gt;
    ...

    <span class="code-tag">&lt;route&gt;</span>
       ...
       <span class="code-tag">&lt;multicast strategyRef=<span class="code-quote">"myStrategy"</span>
executorServiceRef=<span class="code-quote">"fooProfile"</span>&gt;</span>
          ...
       <span class="code-tag">&lt;/multicast&gt;</span>
      ...
    <span class="code-tag">&lt;route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>
<p>What Camel will do at runtime is to lookup in the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> for a <tt>ExecutorService</tt> with the
id = fooProfile. If none found it will fallback and see if there is a <tt>ThreadPoolProfile</tt>
defined with that id. And in this example there is and so the profile is used for creating
a new <tt>ExecutorService</tt> which is handed back to the <a href="/confluence/display/CAMEL/Multicast"
title="Multicast">Multicast</a> EIP to use in the route.</p>

<h3><a name="ThreadingModel-Createcustomthreadpool"></a>Create custom thread
pool</h3>
<p>You can also use the &lt;threadPool/&gt; tag in Spring XML to create a specific
thread pool (eg <tt>ExecutorService</tt>). Notice that any options you do not
explicit define, will have Camel to use the default thread pool profile as fallback. For example
if you omit setting the <tt>maxQueueSize</tt> then Camel will fallback and use
the value from the default thread pool profiles, which by default is 1000. </p>

<h3><a name="ThreadingModel-Management"></a>Management</h3>
<p>All the thread pools that Camel creates are managed and thus you can see them in
JConsole under the <tt>threadpools</tt> category.</p>

<h3><a name="ThreadingModel-ExecutorServiceStrategy%28Camel%3C2.9.0%29"></a>ExecutorServiceStrategy
( Camel &lt; 2.9.0 )</h3>
<p>Camel provides a pluggable strategy to hook in your own thread pool provider, for
example from a WorkManager in a J2EE server etc.<br/>
See the <tt>org.apache.camel.spi.ExecutorServiceStrategy</tt> interface which
you should implement and hook into the WorkManager.</p>

<p>See <a href="/confluence/display/CAMEL/Advanced+configuration+of+CamelContext+using+Spring"
title="Advanced configuration of CamelContext using Spring">Advanced configuration of CamelContext
using Spring</a> for how to configure it.</p>

<p>You can configure it on the <tt>CamelContext</tt> from Java DSL using
the getter/setter.</p>

<h3><a name="ThreadingModel-ExecutorServiceManager%28Camel%3E%3D2.9.0%29"></a>ExecutorServiceManager
( Camel &gt;= 2.9.0 )</h3>

<p>In camel 2.9.0 the ExecutorServiceManager replaces the ExecutorServiceStrategy. It
is renamed to manager as is not only provides a strategy for threadPool creation but also
keeps track of thread pools and thread pool profiles.<br/>
It has methods to register thread pool profiles and to create and shutdown ExecutorService
and ScheduledExecutorService instances. The ThreadPoolBuilder class helps to create ThreadPoolProfiles
in Java.</p>

<p>To hook in custom thread pool providers (e.g. for J2EE servers) a ThreadPoolFactory
interface can be implemented. The implementation can be set in the  ExecutorServiceManager.
The Factory interface is much simpler then the former Strategy and makes the job of integrators
much easier.</p>

<p>See <a href="/confluence/display/CAMEL/Advanced+configuration+of+CamelContext+using+Spring"
title="Advanced configuration of CamelContext using Spring">Advanced configuration of CamelContext
using Spring</a> for how to configure it.</p>

<h3><a name="ThreadingModel-Customizingthreadnames"></a>Customizing thread
names</h3>
<p>On the <tt>ExecutorServiceStrategy</tt> you can configure the thread
name pattern using the <tt>setThreadNamePattern</tt> method, which defines the
thread names used when a thread pool creates a thread.</p>

<p>The default pattern is: <tt>Camel (${camelId}) thread #${counter} - ${name</tt>}<br/>
In the pattern you can use the following placeholders</p>
<ul class="alternate" type="square">
	<li>${camelId} <b>Camel 2.6:</b> is the <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> name</li>
	<li>${counter} is a unique incrementing counter.</li>
	<li>${name} is the regular thread name.</li>
	<li>${longName} is the long thread name which can includes endpoint parameters etc.</li>
</ul>


<h3><a name="ThreadingModel-Componentdevelopers"></a>Component developers</h3>
<p>If you develop your own Camel component and are in need of a thread pool, then its
advised to use the <tt>ExecutorServiceStrategy</tt> to create the thread pool
you need. </p>

<h3><a name="ThreadingModel-Shutdown"></a>Shutdown</h3>
<p>All thread pools created by Camel will be properly shutdown when <tt>CamelContext</tt>
shutdowns which ensures no leaks in the pools in case you run in a server environment with
hot deployments and the likes.</p>

<h3><a name="ThreadingModel-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Architecture" title="Architecture">Architecture</a></li>
	<li><a href="/confluence/display/CAMEL/Async" title="Async">Async</a></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/Threading+Model">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=14813422&revisedVersion=7&originalVersion=6">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Threading+Model?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message