<html>
<head>
<base href="http://cwiki.apache.org/confluence">
<link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=CAMEL&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'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><camelContext xmlns="http://camel.apache.org/schema/spring"
trace="false"> <br></td></tr>
<tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;"><package></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> </package> <br></td></tr>
<tr><td class="diff-added-lines" style="background-color: #dfd;">
<!-- this is just a route to auto generate sample reports --> <br></td></tr>
<tr><td class="diff-unchanged" > <route id="sendMessage">
<br> <from uri="timer://org.apache.camel.example.loadbalancer?fixedRate=true&amp;period=60000"/>
<br></td></tr>
<tr><td class="diff-snipped" >...<br></td></tr>
<tr><td class="diff-unchanged" > </route> <br>
<br></td></tr>
<tr><td class="diff-added-lines" style="background-color: #dfd;">
<!-- this is the route with the load balancer which sends in round robin to the
2 mina endpoints --> <br></td></tr>
<tr><td class="diff-unchanged" > <route id="loadbalancer">
<br> <from uri="direct:loadbalance"/> <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">
<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 ">
<span class="code-tag"><bean id=<span class="code-quote">"service"</span>
class=<span class="code-quote">"org.apache.camel.example.service.Generator"</span>/></span>
<span class="code-tag"><camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
trace=<span class="code-quote">"false"</span>></span>
<span class="code-tag"><span class="code-comment"><!-- this is just
a route to auto generate sample reports --></span></span>
<span class="code-tag"><route id=<span class="code-quote">"sendMessage"</span>></span>
<span class="code-tag"><from uri=<span class="code-quote">"timer://org.apache.camel.example.loadbalancer?fixedRate=true&amp;period=60000"</span>/></span>
<span class="code-tag"><bean ref=<span class="code-quote">"service"</span>
method=<span class="code-quote">"createReport"</span>/></span>
<span class="code-tag"><to uri=<span class="code-quote">"direct:loadbalance"</span>/></span>
<span class="code-tag"></route></span>
<span class="code-tag"><span class="code-comment"><!-- this is the
route with the load balancer which sends in round robin to the 2 mina endpoints --></span></span>
<span class="code-tag"><route id=<span class="code-quote">"loadbalancer"</span>></span>
<span class="code-tag"><from uri=<span class="code-quote">"direct:loadbalance"</span>/></span>
<span class="code-tag"><loadBalance></span>
<span class="code-tag"><roundRobin/></span>
<span class="code-tag"><to uri=<span class="code-quote">"mina:tcp://localhost:9999?sync=true&amp;allowDefaultCodec=true"</span>/></span>
<span class="code-tag"><to uri=<span class="code-quote">"mina:tcp://localhost:9998?sync=true&amp;allowDefaultCodec=true"</span>/></span>
<span class="code-tag"></loadBalance></span>
<span class="code-tag"><to uri=<span class="code-quote">"log:org.apache.camel.example?level=INFO"</span>/></span>
<span class="code-tag"></route></span>
<span class="code-tag"></camelContext></span>
<span class="code-tag"></beans></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">
<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 ">
<span class="code-tag"><bean id=<span class="code-quote">"service"</span>
class=<span class="code-quote">"org.apache.camel.example.service.Reporting"</span>/></span>
<span class="code-tag"><camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
trace=<span class="code-quote">"false"</span>></span>
<span class="code-tag"><route id=<span class="code-quote">"mina1"</span>></span>
<span class="code-tag"><from uri=<span class="code-quote">"mina:tcp://localhost:9999"</span>/></span>
<span class="code-tag"><setHeader headerName=<span class="code-quote">"minaServer"</span>></span><span
class="code-tag"><constant></span>localhost:9999<span class="code-tag"></constant></span><span
class="code-tag"></setHeader></span>
<span class="code-tag"><bean ref=<span class="code-quote">"service"</span>
method=<span class="code-quote">"updateReport"</span>/></span>
<span class="code-tag"></route></span>
<span class="code-tag"></camelContext></span>
<span class="code-tag"></beans></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">
<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 ">
<span class="code-tag"><bean id=<span class="code-quote">"service"</span>
class=<span class="code-quote">"org.apache.camel.example.service.Reporting"</span>/></span>
<span class="code-tag"><camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
trace=<span class="code-quote">"false"</span>></span>
<span class="code-tag"><route id=<span class="code-quote">"mina1"</span>></span>
<span class="code-tag"><from uri=<span class="code-quote">"mina:tcp://localhost:9998"</span>/></span>
<span class="code-tag"><setHeader headerName=<span class="code-quote">"minaServer"</span>></span><span
class="code-tag"><constant></span>localhost:9999<span class="code-tag"></constant></span><span
class="code-tag"></setHeader></span>
<span class="code-tag"><bean ref=<span class="code-quote">"service"</span>
method=<span class="code-quote">"updateReport"</span>/></span>
<span class="code-tag"></route></span>
<span class="code-tag"></camelContext></span>
<span class="code-tag"></beans></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">
>mina1
mvn exec:java -Pmina1
</pre>
</div></div>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
>mina2
mvn exec:java -Pmina2
</pre>
</div></div>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
>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:>> ***************
********************************
>> Report id : 1
>> Report title : Report Title : 1
>> Report content : This is a dummy report
>> Report reply : Report updated from MINA server running on : localhost:9999
>> ***********************************************
]
[che.camel.example.loadbalancer] example INFO Exchange[BodyType:org.apache.camel.example.model.Report,
Body:>> ***************
********************************
>> Report id : 2
>> Report title : Report Title : 2
>> Report content : This is a dummy report
>> Report reply : Report updated from MINA server running on : localhost:9998
>> ***********************************************
]
[che.camel.example.loadbalancer] example INFO Exchange[BodyType:org.apache.camel.example.model.Report,
Body:>> ***************
********************************
>> Report id : 3
>> Report title : Report Title : 3
>> Report content : This is a dummy report
>> Report reply : Report updated from MINA server running on : localhost:9999
>> ***********************************************
]
...
</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&showCommentArea=true#addcomment">Add
Comment</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
|