camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > SEDA
Date Thu, 16 Jul 2009 10:41: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/SEDA">SEDA</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="SEDA-SEDAComponent"></a>SEDA Component</h2>

<p>The <b>seda:</b> component provides asynchronous <a href="http://www.eecs.harvard.edu/~mdw/proj/seda/"
rel="nofollow">SEDA</a> behavior so that messages are exchanged on a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html"
rel="nofollow">BlockingQueue</a> and consumers are invoked in a separate thread to
the producer.</p>

<p>Note that queues are only visible within a single <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a>. If you want to communicate across CamelContext
instances such as to communicate across web applications, see the <a href="/confluence/display/CAMEL/VM"
title="VM">VM</a> component.</p>

<p>This component does not implement any kind of persistence or recovery if the VM terminates
while messages are yet to be processed. If you need persistence, reliability or distributed
SEDA then try using either <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
or <a href="/confluence/display/CAMEL/ActiveMQ" title="ActiveMQ">ActiveMQ</a>.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Synchronous</b><br
/><p>The <a href="/confluence/display/CAMEL/Direct" title="Direct">Direct</a>
component provides synchronous invocation of any consumers when a producer sends a message
exchange.</p></td></tr></table></div>

<h3><a name="SEDA-URIformat"></a>URI format</h3>

<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">seda:someName[?options]</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>Where <b>someName</b> can be any string to uniquely identify the endpoint
within the current <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Same URI
must be used for both producer and consumer</b><br /><p>The excact same
<a href="/confluence/display/CAMEL/SEDA" title="SEDA">SEDA</a> endpoint URI <b>must</b>
be used for both the producer and consumer. Otherwise Camel will create a 2nd <a href="/confluence/display/CAMEL/SEDA"
title="SEDA">SEDA</a> endpoint even thought that <tt>someName</tt> part
of the URI is identical. For example:</p>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("direct:foo").to("seda:bar?concurrentConsumers=5");

from("seda:bar?concurrentConsumers=5").to("file://output");</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>

<p>Notice that we have to use the full URI including options in both the producer and
consumer.</p></td></tr></table></div>

<h3><a name="SEDA-Options"></a>Options</h3>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> size </td>
<td class='confluenceTd'> 1000 </td>
<td class='confluenceTd'> The maximum size of the SEDA queue </td>
</tr>
<tr>
<td class='confluenceTd'> concurrentConsumers </td>
<td class='confluenceTd'> 1 </td>
<td class='confluenceTd'> <b>Camel 1.6.1/2.0</b>: Number of concurrent threads
processing exchanges. </td>
</tr>
<tr>
<td class='confluenceTd'> waitForTaskToComplete </td>
<td class='confluenceTd'> IfReplyExpected </td>
<td class='confluenceTd'> <b>Camel 2.0</b>: Option to specify if the caller
should wait for the async task to be complete or not before continuing. The following 3 options
is supported: <b>Always</b>, <b>Newer</b> or <b>IfReplyExpected</b>.
The first two options is self explained. The last will only wait if the message is <a href="/confluence/display/CAMEL/Request+Reply"
title="Request Reply">Request Reply</a> based. The default option is <b>IfReplyExpected</b>.
See more information about <a href="/confluence/display/CAMEL/Async" title="Async">Async</a>
messaging. </td>
</tr>
<tr>
<td class='confluenceTd'> timeout </td>
<td class='confluenceTd'> 30000 </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> Timeout in millis a seda producer
will at most waiting for an async task to complete. See <tt>waitForTaskToComplete</tt>
and the <a href="/confluence/display/CAMEL/Async" title="Async">Async</a> for
more details. </td>
</tr>
</tbody></table>

<h3><a name="SEDA-ChangesinCamel2.0"></a>Changes in Camel 2.0</h3>
<p>In Camel 2.0 the <a href="/confluence/display/CAMEL/SEDA" title="SEDA">SEDA</a>
component supports using <a href="/confluence/display/CAMEL/Request+Reply" title="Request
Reply">Request Reply</a> where the caller will wait for the <a href="/confluence/display/CAMEL/Async"
title="Async">Async</a> route to complete. For instance:</p>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("mina:tcp://0.0.0.0:9876?textline=true&amp;sync=true").to("seda:input");

  from("seda:input").to("bean:processInput").to("bean:createResponse");</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>

<p>In the route above we have a TCP listener on port 9876 that accepts incoming requests.
The request is routed to the <tt>seda:input</tt> queue. As its a <a href="/confluence/display/CAMEL/Request+Reply"
title="Request Reply">Request Reply</a> message we will wait for the response. So
when the consumer on the <tt>seda:input</tt> queue is complete it will copy this
response to the original message as response.</p>

<p>Camel 1.x does <b>not</b> have this feature implemented, the <a href="/confluence/display/CAMEL/SEDA"
title="SEDA">SEDA</a> queues in Camel 1.x will newer wait.</p>

<h3><a name="SEDA-Concurrentconsumers"></a>Concurrent consumers</h3>
<p>By default Camel uses a single consumer. You can configure the endpoint to use concurrent
consumers. So instead of thread pools you can use:</p>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("seda:stageName?concurrentConsumers=5").process(...)</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<h4><a name="SEDA-Differencebetweenthreadpoolsandconcurrentconsumers"></a>Difference
between thread pools and concurrent consumers</h4>
<p>The thread pool is a pool that dynamically can increase/shrink at runtime depending
on load, the concurrent consumers is always fixed. </p>

<h3><a name="SEDA-Threadpools"></a>Thread pools</h3>
<p>Be aware that adding a thread pool to a seda endpoint by doing something like:</p>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("seda:stageName").thread(5).process(...)</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>

<p>can wind up with two BlockQueues. One from seda endpoint and one from the workqueue
of the thread pool which may not be what you want. Instead, you might want to consider configuring
a <a href="/confluence/display/CAMEL/Direct" title="Direct">Direct</a> endpoint
with a thread pool which can process messages both synchronously and asynchronously. For example:</p>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("direct:stageName").thread(5).process(...)</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>

<p>You can also directly configure number of threads that process messages on seda endpoint
using <tt>concurrentConsumers</tt> parameter.</p>

<h3><a name="SEDA-Sample"></a>Sample</h3>
<p>In the route below we use the SEDA queue to send the request to this async queue
to be able to send a fire-and-forget message for further processing in another thread, and
return a constant reply in this thread to the original caller. </p>
<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">public
void configure() throws Exception {
    from("direct:start")
        // send it to the seda queue that is async
        .to("seda:next")
        // return a constant response
        .transform(constant("OK"));

    from("seda:next").to("mock:result");
}</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>Here we send a Hello World message and expects the reply to be OK.</p>
<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">Object
out = template.requestBody("direct:start", "Hello World");
assertEquals("OK", out);</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>The "Hello World" message will be consumed from the SEDA queue from another thread
for further processing, since this is from an unit test it will be sent to a mock endpoint
where we can do assertions in the unit test.</p>

<h3><a name="SEDA-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Direct" title="Direct">Direct</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="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/SEDA">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=61225&revisedVersion=18&originalVersion=17">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/SEDA?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message