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, 07 Sep 2012 07:43: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/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (4)</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" >The {{ExecutorServiceManager}} has
APIs for shutting down thread pools graceful and aggressively. Its encourage to use this API
for creating and shutting down thread pools. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >From *Camel <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">2.10.2*</span>
<span class="diff-added-words"style="background-color: #dfd;">2.11*</span> onwards
Camel the graceful {{shutdownGraceful(executorService)}} method from {{ExecutorServiceManager}}
will shutdown graceful at first, until a timeout value is hit. After that it shutdown aggressively,
again using the timeout value to wait for the operation to complete. This means you can wait
at most 2 x timeout for shutting down the thread pool. <br></td></tr>
            <tr><td class="diff-unchanged" >The timeout value is by default {{10000}}
millis. You can configure a custom value on the {{ExecutorServiceManager}} if needed. During
shutdown Camel will log every 2nd second at INFO level progress of shutting down the thread
pool. For example in case a shutdown takes a while, then there is activity in the logs. <br>
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| shutdown | Marks the thread pool
as shutdown, eg just as calling {{ExecutorService.shutdown()}} method | <br>| shutdownNow
| Forces the thread pool to shutdown now, eg just as calling {{ExecutorService.shutdownNow()}}
method | <br></td></tr>
            <tr><td class="diff-changed-lines" >| shutdownGraceful | *Camel <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">2.10.2:*</span>
<span class="diff-added-words"style="background-color: #dfd;">2.11:*</span> Marks
the thread pool as shutdown, and graceful shutdown the pool, by waiting for tasks to complete.
A default timeout value of 10 sec is used, before <br></td></tr>
            <tr><td class="diff-unchanged" >  shutdown becomes aggressive using
{{shutdownNow}}, to force threads to shutdown. | <br></td></tr>
            <tr><td class="diff-changed-lines" >| shutdownGraceful(timeout) |
*Camel <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">2.10.2:*</span>
<span class="diff-added-words"style="background-color: #dfd;">2.11:*</span> As
above but with custom timeout value | <br></td></tr>
            <tr><td class="diff-changed-lines" >| awaitTermination | *Camel <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">2.10.2:*</span>
<span class="diff-added-words"style="background-color: #dfd;">2.11:*</span> To
wait graceful for the termination of a thread pool (eg to wait for its tasks to complete).
Will wait until all tasks is completed or a timeout value is hit. | <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. See Also <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>
has 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>ExecutorServiceStrategy</tt>/<tt>ExecutorServiceManager</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"></a>ExecutorServiceStrategy</h3>
<p><b>Available as of Camel 2.3 to 2.8.x</b><br/>
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"></a>ExecutorServiceManager</h3>
<p><b>Available as of Camel 2.9</b></p>

<p>In camel 2.9.0 the <tt>ExecutorServiceManager</tt> replaces the <tt>ExecutorServiceStrategy</tt>.
It is renamed to manager as is not only provides a strategy for thread pool creation but also
keeps track of thread pools and thread pool profiles.</p>

<p>To hook in custom thread pool providers (e.g. for J2EE servers) a <tt>ThreadPoolFactory</tt>
interface can be implemented. The implementation can be set in the  <tt>ExecutorServiceManager</tt>.
The Factory interface is much simpler then the former <tt>ExecutorServiceStrategy</tt>
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>/<tt>ExecutorServiceManager</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 for:</p>
<ul class="alternate" type="square">
	<li><b>Camel 2.9.x or older:</b> <tt>Camel (${camelId}) thread #${counter}
- ${name</tt>}</li>
	<li><b>Camel 2.10 onwards:</b> <tt>Camel (#camelId#) thread ##counter#
- #name#</tt></li>
</ul>


<p>Notice we renamed the tokens from Camel 2.10 onwards to not clash with tokens by
the <a href="/confluence/display/CAMEL/Using+PropertyPlaceholder" title="Using PropertyPlaceholder">Property
Placeholder</a>.</p>

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


<p>Notice the pattern name has changed from Camel 2.10 onwards, use #name# instead.</p>

<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>/<tt>ExecutorServiceManager</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>

<p>The <tt>ExecutorServiceManager</tt> has APIs for shutting down thread
pools graceful and aggressively. Its encourage to use this API for creating and shutting down
thread pools.</p>

<p>From <b>Camel 2.11</b> onwards Camel the graceful <tt>shutdownGraceful(executorService)</tt>
method from <tt>ExecutorServiceManager</tt> will shutdown graceful at first, until
a timeout value is hit. After that it shutdown aggressively, again using the timeout value
to wait for the operation to complete. This means you can wait at most 2 x timeout for shutting
down the thread pool.<br/>
The timeout value is by default <tt>10000</tt> millis. You can configure a custom
value on the <tt>ExecutorServiceManager</tt> if needed. During shutdown Camel
will log every 2nd second at INFO level progress of shutting down the thread pool. For example
in case a shutdown takes a while, then there is activity in the logs.</p>

<p>The APIs on <tt>ExecutorServiceManager</tt> that is related to shutting
down a thread pool is as follows:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Method </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> shutdown </td>
<td class='confluenceTd'> Marks the thread pool as shutdown, eg just as calling <tt>ExecutorService.shutdown()</tt>
method </td>
</tr>
<tr>
<td class='confluenceTd'> shutdownNow </td>
<td class='confluenceTd'> Forces the thread pool to shutdown now, eg just as calling
<tt>ExecutorService.shutdownNow()</tt> method </td>
</tr>
<tr>
<td class='confluenceTd'> shutdownGraceful </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> Marks the thread pool as shutdown,
and graceful shutdown the pool, by waiting for tasks to complete. A default timeout value
of 10 sec is used, before<br/>
  shutdown becomes aggressive using <tt>shutdownNow</tt>, to force threads to
shutdown. </td>
</tr>
<tr>
<td class='confluenceTd'> shutdownGraceful(timeout) </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> As above but with custom timeout
value </td>
</tr>
<tr>
<td class='confluenceTd'> awaitTermination </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> To wait graceful for the termination
of a thread pool (eg to wait for its tasks to complete). Will wait until all tasks is completed
or a timeout value is hit. </td>
</tr>
</tbody></table>
</div>


<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=14&originalVersion=13">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