Вопрос

So, recently I started learning Camel. As part of the process I decided to go through all the examples (listed HERE and available when you DOWNLOAD the package with all the examples and docs) and to see what I could learn.

One of the examples, Load Balancing using Mina caught my attention because it uses a Mina in different JVM's and it simulates a load balancer with round robin.

I have a few problems with this example. First it uses the Spring DSL, instead of the Java DSL which my project uses and which I find a lot easier to understand now (mainly also because I am used to it). So the first question: is there a version of this example using only the Java DSL instead of the Spring DSL for the routes and the beans?

My second question is code related. The description states, and I quote:

Within this demo every ten seconds, a Report object is created from the Camel load balancer server. This object is sent by the Camel load balancer to a MINA server where the object is then serialized. One of the two MINA servers (localhost:9991 and localhost:9992) receives the object and enriches the message by setting the field reply of the Report object. The reply is sent back by the MINA server to the client, which then logs the reply on the console.

So, from what I read, I understand that the MINA server 1 (per example) receives a report from the loadbalancer, changes it, and then it sends that report back to some invisible client. Upon checking the code, I see no client java class or XML and when I run, the server simply posts the results on the command line. Where is the client ?? What is this client?

The MINA-1 server code presented here:

<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.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">

    <route id="mina1">
      <from uri="mina:tcp://localhost:9991"/>
      <setHeader headerName="minaServer">
        <constant>localhost:9991</constant>
      </setHeader>
      <bean ref="service" method="updateReport"/>
    </route>

  </camelContext>

</beans>

I don't understand how the updateReport() method magically prints the object on my console. What if I wanted to send message to a third MINA server? How would I do it? (I would have to add a new route, and send it to the URI of the 3rd server correct?)

I know most of these questions may sound dumb, but I would appreciate if anyone could help me. A Java DSL version of this would really help me.

Это было полезно?

Решение 2

In the previous post I had 2 questions: 1. How to add another mina server 2. why are the mina servers sending replies.

Namphibian answered problem 1 but not problem 2. However, I have found the solution here: http://camel.465427.n5.nabble.com/Load-balancing-using-Mina-example-with-Java-DSL-td5742566.html#a5742585

Kudos to Mr. Claus for the answer and suggestions.

PS: If you edit your post to contain this information, I will gladly pick it as an answer.

Другие советы

The client is in this route.

<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.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">

  <route id="sendMessage">
    <from uri="timer://org.apache.camel.example.loadbalancer?period=10s"/>
    <bean ref="service" method="createReport"/>
    <to uri="direct:loadbalance"/>
  </route>

    <!-- use failover load balancer in round robin mode, to automatic failover to next server
         in case of failure -->
  <route id="loadbalancer">
      <from uri="direct:loadbalance"/>
      <loadBalance inheritErrorHandler="false">
        <failover roundRobin="true"/>
        <to uri="mina:tcp://localhost:9991?sync=true"/>
        <to uri="mina:tcp://localhost:9992?sync=true"/>
      </loadBalance>
    <log message="${body}"/>
  </route>
  </camelContext>
</beans>

Note that there is a time component:

<from uri="timer://org.apache.camel.example.loadbalancer?period=10s"/>

This component calls the method createReport of the service bean which is the class type: org.apache.camel.example.service.Generator . This is the client.

To add a additional MINA server use the following Spring DSL.

 <loadBalance inheritErrorHandler="false">
    <failover roundRobin="true"/>
    <to uri="mina:tcp://localhost:9991?sync=true"/>
    <to uri="mina:tcp://localhost:9992?sync=true"/>
    <to uri="mina:tcp://localhost:9993?sync=true"/>
 </loadBalance>

Then create a third MINA consumer like this:

<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.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">

   <route id="mina2">
    <from uri="mina:tcp://localhost:9993"/>
     <setHeader headerName="minaServer">
       <constant>localhost:9993</constant>
     </setHeader>
     <bean ref="service" method="updateReport"/>
    </route>

  </camelContext>

</beans>

Note that this would require you to start the MINA3 server additionally as well when running the example. The client and the load balancer routes (2 routes) are in the same camel file.

I would suggest that you learn how to read the Spring DSL as it is really worth the effort. Also if you are not familiar with Spring you do need a primer on it. The dependency injection part is especially important.

One last recommendation would be to buy yourself a copy of Camel In Action. It is really a great way to start working with Camel.

If you need further clarification please ask away.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top