Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This example show how you can easily use the camel-mina component to design a solution
allowing to distribute message
workload on several servers. Those
These servers are simple TCP/IP servers created by the Apache MINA framework and running in
separate Java Virtual MachineJVMs. The Load load balancer EIP pattern of Camel which is used top of them allows to
send in a Round Robin model
mode the messages created from a camel Bean component
respectively to 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 (sync=true on the Mina endpoint)9992.

The demo starts when every one minute5th seconds, a Report object is created from the camel loadbalancer load balancer server.
This object is send by the
camel loadbalancer load balancer to a MINA server and object is serialized.
One of the two MINA servers (localhost:99999991 and localhost:99989992) receives
the object and enrich it the message
by setting the field reply of the Report object. The reply is send back by the MINA server to the waiting callerclient,
who will display in its log the content of the Report object 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">

    <!-- this is just a route to auto generate sample reports -->
    <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>
    
    <!-- this is the route with the load balancer which sends in round robin to the 2 mina endpoints -->
    <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:

...

and check the result in the log of loadbalancer

...

console of load balancer