camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > CXF Async Example
Date Mon, 16 Nov 2009 14:36: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/CXF+Async+Example">CXF
Async Example</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="CXFAsyncExample-CXFAsyncExample"></a>CXF Async Example</h2>

<p>This example shows how to use the new feature in Camel 2.1 which is support for non
blocking asynchronous producers by <a href="/confluence/display/CAMEL/ToAsync" title="ToAsync">ToAsync</a>.</p>

<p>Currently <a href="/confluence/display/CAMEL/Jetty" title="Jetty">camel-jetty</a>
implements this to the fullest as its <tt>JettyHttpProducer</tt> supports non
blocking request/reply natively in Jetty. However in cases its <b>not</b> natively
supported in the <tt>Producer</tt> then Camel core will fallback to simulate non
blocking request/response where it handles the sending the request in another thread pool.
This ensures the original thread will still not block and it appears as asynchronous request/reply.
</p>

<p>This example shows a client and a server in action. The client sends 100 webservice
messages to the server over <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a>
which the server processes and returns a reply.</p>

<p>The client is working using a single threaded to route the messages to the point
where they are send to the webserver. As we use non blocking asynchronous <a href="/confluence/display/CAMEL/Request+Reply"
title="Request Reply">Request Reply</a> this single thread will terminate its current
task and be ready immediately to route the next message. This allows us to have higher throughput
as the single thread can go as fast as it can, it does not have to wait for the webserver
to reply (i.e. its not blocking).</p>

<p>You can see the difference if you change the <tt>async=true</tt> option
to <tt>async=false</tt> in the <tt>src/main/resources/META-INF/spring/camel-client.xml</tt>
file.</p>

<h3><a name="CXFAsyncExample-Running"></a>Running</h3>

<p>You will need to compile this example first:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  mvn compile
</pre>
</div></div>

<p>The example should run if you type:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  mvn exec:java -PCamelServer

  mvn exec:java -PCamelClient
</pre>
</div></div>

<p>To stop the server hit ctrl + c</p>

<h3><a name="CXFAsyncExample-Sampleoutput"></a>Sample output</h3>
<p>When the client is running it outputs all requests and responses on the screen.</p>

<p>As the client is single threaded it will send the messages in order, e.g. from 0
to 99.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO 
Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@df2925]]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO 
Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@e89199]]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO 
Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@437154]]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO 
Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@ec7a35]]
...
</pre>
</div></div>

<p>As the HTTP server is simulating some time to process each message its replies will
likely come after all the client have send all 100 messages. When they arrive they come back
out of order</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[    Camel thread 7: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:7 = OK]
[    Camel thread 3: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:9 = OK]
[    Camel thread 8: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:0 = OK]
[    Camel thread 1: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:2 = OK]
[    Camel thread 2: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:1 = OK]
[    Camel thread 5: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:5 = OK]
[    Camel thread 4: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:6 = OK]
[    Camel thread 6: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:8 = OK]
[    Camel thread 0: ToAsync[cxf:bean:reportIncidentEndpoint]] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:3 = OK]
...
</pre>
</div></div>

<p>And as you can see they are being handled by different threads, as we have configured
using a <tt>poolSize=10</tt> option.</p>

<h3><a name="CXFAsyncExample-Runningsynchronous"></a>Running synchronous</h3>
<p>If we on the other hand change to synchronous mode, that means we will use the single
thread for the entire routing and it will be blocked while waiting for the reply from the
webserver. To see this in action change the <tt>async="true"</tt> to <tt>async="false"</tt>.
</p>

<p>The output is then as expected a request, reply and so forth. And of course the throughput
is much lower as we are only handle a single message at the time and blocked while waiting
for the webserver reply.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
...
[          org.apache.camel.example.client.CamelClient.main()] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:97 = OK]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO 
Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@2566c1]]
[          org.apache.camel.example.client.CamelClient.main()] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:98 = OK]
[          org.apache.camel.example.client.CamelClient.main()] +++ request +++      INFO 
Exchange[BodyType:java.util.ArrayList, Body:[org.apache.camel.example.reportincident.InputReportIncident@26ec29]]
[          org.apache.camel.example.client.CamelClient.main()] +++  reply  +++      INFO 
Exchange[BodyType:<span class="code-object">String</span>, Body:99 = OK]
... Send done
</pre>
</div></div>


<h3><a name="CXFAsyncExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/display/CAMEL/ToAsync" title="ToAsync">ToAsync</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP+Async+Example" title="HTTP Async Example">HTTP
Async Example</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/CXF+Async+Example">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=5965147&revisedVersion=2&originalVersion=1">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/CXF+Async+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message