Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Load balancing using Mina example

This example show shows how you can easily use the camelCamel-minaMINA component to design a solution
allowing to distribute for distributing message
workload on onto several servers. Those
These servers are simple TCP/IP servers created by the Apache MINA framework and running run in
separate Java Virtual MachineJVMs. The Load load balancer EIP pattern of Camel which is used on top of them allows to send for
sending in a Round Robin model
mode the messages created from a Camel Bean component respectively to
alternatively between each server running on localhost:99999991 and localhost:9998.

MINA has been configured to send over the wire objects serialized and this is what is showed also in this example.
The advantage of this approach is that you don't need to use CORBA or Java RMI for the communication between the different JVMs.
The example has been configured to use InOut EIP pattern.

9992.

Within this demo every ten secondsThe demo starts when every one minute, a Report object is created from the camel loadbalancer Camel load balancer server.
This object is send sent by the
camel loadbalancer Camel load balancer to a MINA server and where the object is then serialized.
One of the two MINA servers (localhost:99999991 and localhost:99989992) receives
the object and enrich it enriches
the message by setting the field reply of the Report object. The reply is send sent back by the MINA
server to the waiting caller,
who will display in its log the content of the Report objectclient, which then logs the reply on the console.

Description of the routes

Wiki Markup
{snippet:id=e1|lang=xml|title=Load balancer|url=camel/trunk/examples/camel-example-loadbalancing/src/main/resources/META-INF/spring/camel-context-loadbalancer.xml}
Wiki Markup
{snippet:id=e1|lang=xml|title=Mina Server 1|url=camel/trunk/examples/camel-example-loadbalancing/src/main/resources/META-INF/spring/camel-context-mina1.xml}
Wiki Markup
{snippet:id=e1|lang=xml|title=Mina Server 2|url=camel/trunk/examples/camel-example-loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml}
Code Block
xmlxml
titleLoad balancer (the caller)

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:camel="http://camel.apache.org/schema/spring"
 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 ">
 
<bean id="service" class="org.apache.camel.example.service.Generator"/> 

<camelContext xmlns="http://camel.apache.org/schema/spring" trace="false">
    <package>
        com.intuit.ai.step.camel 
    </package>
    <route id="sendMessage">
    	<from uri="timer://org.apache.camel.example.loadbalancer?fixedRate=true&amp;period=60000"/>
    	<bean ref="service" method="createReport"/>
    	<to uri="direct:loadbalance"/>
    </route>
    
    <route id="loadbalancer">
        <from uri="direct:loadbalance"/>
        <loadBalance>
            <roundRobin/>
            <to uri="mina:tcp://localhost:9999?sync=true&amp;allowDefaultCodec=true"/>
            <to uri="mina:tcp://localhost:9998?sync=true&amp;allowDefaultCodec=true"/>
        </loadBalance>
        <to uri="log:org.apache.camel.example?level=INFO"/>
    </route>
</camelContext>

</beans>
Code Block
xmlxml
titleMina Server 1

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:camel="http://camel.apache.org/schema/spring"
 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 ">
 
<bean id="service" class="org.apache.camel.example.service.Reporting"/> 

<camelContext xmlns="http://camel.apache.org/schema/spring" trace="false">
    <route id="mina1">
        <from uri="mina:tcp://localhost:9999"/>
        <setHeader headerName="minaServer"><constant>localhost:9999</constant></setHeader>
		<bean ref="service" method="updateReport"/>
    </route>
</camelContext>

</beans>
Code Block
xmlxml
titleMina Server 2

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:camel="http://camel.apache.org/schema/spring"
 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 ">
 
<bean id="service" class="org.apache.camel.example.service.Reporting"/> 

<camelContext xmlns="http://camel.apache.org/schema/spring" trace="false">
    <route id="mina1">
        <from uri="mina:tcp://localhost:9998"/>
        <setHeader headerName="minaServer"><constant>localhost:9999</constant></setHeader>
		<bean ref="service" method="updateReport"/>
    </route>
</camelContext>

</beans>

How to run the example

To compile and install the project in your maven repo, execute the following command on the root of the project:

Code Block
mvn clean install 

To run the example, then execute now the following command in the respective folder:

...

and check the result in the log of loadbalancer

...

console of load balancer.