camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > LoadBalancing Mina Example
Date Wed, 12 May 2010 07:19:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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="http://cwiki.apache.org/confluence/display/CAMEL/LoadBalancing+Mina+Example">LoadBalancing
Mina Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (6)</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" >MINA has been configured to send over
the wire objects serialized and this is what is showed also in this example. <br>The
advantage of this approach is that you don&#39;t need to use CORBA or Java RMI for the
communication between the different JVMs. <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
example has been configured to use InOut EIP pattern. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
example has been configured to use InOut EIP pattern (sync=true on the Mina endpoint). <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The demo starts when every
one minute, a {{Report}} object is created from the camel loadbalancer server. This object
is send by the  <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>&lt;camelContext xmlns=&quot;http://camel.apache.org/schema/spring&quot;
trace=&quot;false&quot;&gt; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&lt;package&gt;</span>
<span class="diff-added-words"style="background-color: #dfd;"> </span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
       com.intuit.ai.step.camel  <br>    &lt;/package&gt; <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
   &lt;!-- this is just a route to auto generate sample reports --&gt; <br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;route id=&quot;sendMessage&quot;&gt;
<br>    	&lt;from uri=&quot;timer://org.apache.camel.example.loadbalancer?fixedRate=true&amp;amp;period=60000&quot;/&gt;
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;/route&gt; <br>
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
   &lt;!-- this is the route with the load balancer which sends in round robin to the
2 mina endpoints --&gt; <br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;route id=&quot;loadbalancer&quot;&gt;
<br>        &lt;from uri=&quot;direct:loadbalance&quot;/&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="LoadBalancingMinaExample-LoadbalancingusingMinaexample"></a>Load
balancing using Mina example</h2>

<p>This example show how you can easily use the <a href="/confluence/display/CAMEL/MINA"
title="MINA">camel-mina</a> component to design a solution allowing to distribute
message <br/>
workload on several servers. Those servers are simple TCP/IP servers created by the Apache
MINA framework and running in<br/>
separate Java Virtual Machine. The <a href="/confluence/display/CAMEL/Load+Balancer" title="Load
Balancer">Load Balancer</a> EIP pattern of Camel which is used top of them allows
to send in a Round Robin model<br/>
mode the messages created from a <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
component respectively to each server running on <tt>localhost:9999</tt> and <tt>localhost:9998</tt>.</p>

<p>MINA has been configured to send over the wire objects serialized and this is what
is showed also in this example.<br/>
The advantage of this approach is that you don't need to use CORBA or Java RMI for the communication
between the different JVMs.<br/>
The example has been configured to use InOut EIP pattern (sync=true on the Mina endpoint).</p>

<p>The demo starts when every one minute, a <tt>Report</tt> object is created
from the camel loadbalancer server. This object is send by the <br/>
camel loadbalancer to a MINA server and object is serialized. One of the two MINA servers
(<tt>localhost:9999</tt> and <tt>localhost:9998</tt>) receives<br/>
the object and enrich it by setting the field reply of the <tt>Report</tt> object.
The reply is send back by the MINA server to the waiting caller, <br/>
who will display in its log the content of the <tt>Report</tt> object. </p>


<h3><a name="LoadBalancingMinaExample-Descriptionoftheroutes"></a>Description
of the routes </h3>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Load balancer (the caller)</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
 <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
 <span class="code-keyword">xmlns:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://camel.apache.org/schema/spring
 http://camel.apache.org/schema/spring/camel-spring.xsd "&gt;
 
<span class="code-tag">&lt;bean id=<span class="code-quote">"service"</span>
class=<span class="code-quote">"org.apache.camel.example.service.Generator"</span>/&gt;</span>


<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
trace=<span class="code-quote">"false"</span>&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- this is just
a route to auto generate sample reports --&gt;</span></span>
    <span class="code-tag">&lt;route id=<span class="code-quote">"sendMessage"</span>&gt;</span>
    	<span class="code-tag">&lt;from uri=<span class="code-quote">"timer://org.apache.camel.example.loadbalancer?fixedRate=true&amp;amp;period=60000"</span>/&gt;</span>
    	<span class="code-tag">&lt;bean ref=<span class="code-quote">"service"</span>
method=<span class="code-quote">"createReport"</span>/&gt;</span>
    	<span class="code-tag">&lt;to uri=<span class="code-quote">"direct:loadbalance"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
    
    <span class="code-tag"><span class="code-comment">&lt;!-- this is the
route with the load balancer which sends in round robin to the 2 mina endpoints --&gt;</span></span>
    <span class="code-tag">&lt;route id=<span class="code-quote">"loadbalancer"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:loadbalance"</span>/&gt;</span>
        <span class="code-tag">&lt;loadBalance&gt;</span>
            <span class="code-tag">&lt;roundRobin/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mina:tcp://localhost:9999?sync=true&amp;amp;allowDefaultCodec=true"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mina:tcp://localhost:9998?sync=true&amp;amp;allowDefaultCodec=true"</span>/&gt;</span>
        <span class="code-tag">&lt;/loadBalance&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"log:org.apache.camel.example?level=INFO"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Mina Server 1</b></div><div class="codeContent
panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
 <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
 <span class="code-keyword">xmlns:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://camel.apache.org/schema/spring
 http://camel.apache.org/schema/spring/camel-spring.xsd "&gt;
 
<span class="code-tag">&lt;bean id=<span class="code-quote">"service"</span>
class=<span class="code-quote">"org.apache.camel.example.service.Reporting"</span>/&gt;</span>


<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
trace=<span class="code-quote">"false"</span>&gt;</span>
    <span class="code-tag">&lt;route id=<span class="code-quote">"mina1"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"mina:tcp://localhost:9999"</span>/&gt;</span>
        <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"minaServer"</span>&gt;</span><span
class="code-tag">&lt;constant&gt;</span>localhost:9999<span class="code-tag">&lt;/constant&gt;</span><span
class="code-tag">&lt;/setHeader&gt;</span>
		<span class="code-tag">&lt;bean ref=<span class="code-quote">"service"</span>
method=<span class="code-quote">"updateReport"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Mina Server 2</b></div><div class="codeContent
panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
 <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
 <span class="code-keyword">xmlns:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://camel.apache.org/schema/spring
 http://camel.apache.org/schema/spring/camel-spring.xsd "&gt;
 
<span class="code-tag">&lt;bean id=<span class="code-quote">"service"</span>
class=<span class="code-quote">"org.apache.camel.example.service.Reporting"</span>/&gt;</span>


<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
trace=<span class="code-quote">"false"</span>&gt;</span>
    <span class="code-tag">&lt;route id=<span class="code-quote">"mina1"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"mina:tcp://localhost:9998"</span>/&gt;</span>
        <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"minaServer"</span>&gt;</span><span
class="code-tag">&lt;constant&gt;</span>localhost:9999<span class="code-tag">&lt;/constant&gt;</span><span
class="code-tag">&lt;/setHeader&gt;</span>
		<span class="code-tag">&lt;bean ref=<span class="code-quote">"service"</span>
method=<span class="code-quote">"updateReport"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>


<h3><a name="LoadBalancingMinaExample-Howtoruntheexample"></a>How to run
the example</h3>

<p>To compile and install the project in your maven repo, execute the following command
on the  root of the project:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn clean install 
</pre>
</div></div>

<p>To run the example, execute now the following command in the respective folder:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&gt;mina1
mvn exec:java -Pmina1
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&gt;mina2
mvn exec:java -Pmina2 
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&gt;loadbalancing
mvn exec:java -Ploadbalancer
</pre>
</div></div>

<p>and check the result in the log of loadbalancer</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[pache.camel.spring.Main.main()] MainSupport                    INFO  Apache Camel 2.2.0 starting
[pache.camel.spring.Main.main()] CamelNamespaceHandler          INFO  camel-osgi.jar/camel-spring-osgi.jar
not detected in classpath
[pache.camel.spring.Main.main()] DefaultCamelContext            INFO  Apache Camel 2.2.0 (CamelContext:camelContext)
is starting
[pache.camel.spring.Main.main()] DefaultCamelContext            INFO  JMX enabled. Using DefaultManagedLifecycleStrategy.
[pache.camel.spring.Main.main()] DefaultCamelContext            INFO  Started 2 routes
[pache.camel.spring.Main.main()] DefaultCamelContext            INFO  Apache Camel 2.2.0 (CamelContext:camelContext)
started
[che.camel.example.loadbalancer] example                        INFO  Exchange[BodyType:org.apache.camel.example.model.Report,
Body:&gt;&gt; ***************
********************************
&gt;&gt; Report id : 1
&gt;&gt; Report title : Report Title : 1
&gt;&gt; Report content : This is a dummy report
&gt;&gt; Report reply : Report updated from MINA server running on : localhost:9999
&gt;&gt; ***********************************************
]
[che.camel.example.loadbalancer] example                        INFO  Exchange[BodyType:org.apache.camel.example.model.Report,
Body:&gt;&gt; ***************
********************************
&gt;&gt; Report id : 2
&gt;&gt; Report title : Report Title : 2
&gt;&gt; Report content : This is a dummy report
&gt;&gt; Report reply : Report updated from MINA server running on : localhost:9998
&gt;&gt; ***********************************************
]
[che.camel.example.loadbalancer] example                        INFO  Exchange[BodyType:org.apache.camel.example.model.Report,
Body:&gt;&gt; ***************
********************************
&gt;&gt; Report id : 3
&gt;&gt; Report title : Report Title : 3
&gt;&gt; Report content : This is a dummy report
&gt;&gt; Report reply : Report updated from MINA server running on : localhost:9999
&gt;&gt; ***********************************************
]
...
</pre>
</div></div>

    </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/LoadBalancing+Mina+Example">View
Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=20645532&revisedVersion=2&originalVersion=1">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/LoadBalancing+Mina+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message