Failover example of the base Apache Camel LoadBalancer

To get started, I just want to let you know that I am new to Camel, and most recently I understood its basic concepts.

I am trying to create a basic working example using Apache-Camel with ActiveMQ as a broker and using the jms component as a loadbalancer client using the failover construct. All this is done using only DSL DS (if possible).

The example consists of 4 main applications called MyApp-A, MyApp-B, MyApp-C and MyApp-D. In a normal scenario, MyApp-A reads a file from my computer and then converts it into a message. Then it sends this message to MyApp-B, and MyApp-B sends it to MyApp-C.

enter image description here

However, there is a failure scenario. In this case, MyApp-A does not send a message to MyApp-B. Then it sends a message to MyApp-D, which in turn sends it to MyApp-C.

enter image description here

Below is my code for MyApp-A

public class MyApp-A { public static void main(String args[]) throws Exception { // create CamelContext CamelContext context = new DefaultCamelContext(); // connect to embedded ActiveMQ JMS broker ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); // add our route to the CamelContext context.addRoutes(new RouteBuilder() { @Override public void configure() { from("file:data/inbox?noop=true")loadbalancer().failover().to("MyApp-B:incomingOrders").to("MyApp-D:incomingOrders").end(); } }); // start the route and let it do its work context.start(); Thread.sleep(10000); // stop the CamelContext context.stop(); } } 

I considered using camel-ftp , but that would not work because MyApp-C did not know that MyApp-B was dead and would not know what it should have received from MyApp-D.

Now I have a few problems and questions:

  • How to send a message (in this case, a file) from MyApp-A to MyApp-B, which is another application? What should I use in the .to(String) method for Java DSL?
  • How do I actually encode MyApp-B? How can I get a message from A (this is another application, maybe on a different computer) and send it to MyApp-C (I assume that if I learn how to send from MyApp-A to MyApp-B, I will know how send from MyApp-B to MyApp-C)?
  • How will MyApp-A detect that MyApp-B failed?
  • Which component of a camel should be used?

If you could provide any feedback on my code and how to fix this problem, I would be more grateful.

+4
source share
1 answer

After much effort, I found a way to implement this based on the loadbalancer example provided by apache.

I uploaded the eclipse project to my github account, you can check its work here:

Although my example respects the general intended architecture, it has several differences, as described below:

  • It uses Spring DSL instead of Java DSL
  • MyApp-A is a loadbalancer. Every 10, it generates a report (instead of reading the file) and sends it to MyApp-B.
  • MyApp-B corresponds to MINA 1 server on localhost: 9991
  • MyApp-C corresponds to MINA 3 server on localhost: 9993
  • MyApp-D corresponds to MINA 2 server on localhost: 9992
  • When MyApp-C receives a report, it sends it back to MyApp-A

In addition, it is also unclear when, where, and why MyApp-C responds to MyApp-A with a modified report. This behavior is not specified in the Spring DSL code, and so far no one has been able to explain to me why this is even happening.

Thus, two problems remain:

  • How to do it using Java DSL
  • Why does MyApp-C respond to MyApp-A and how does it do it?

If you're interested, here is the README.txt I created with the exact description of the problem:

Load balancing with MINA example

This example shows how you can easily use the Camel-MINA component to develop a solution that allows a fail-safe solution that redirects requests when the server is turned off. These servers are simple TCP / IP servers built using the Apache MINA infrastructure run in separate JVMs.

In this example, the load balancer will generate a report every 10 seconds and send this report to the MINA server running on local: 9991. Then this server sends the report to MINA server running on localhost: 9993, which then returns the client report so that it can print it to the console. Each MINA server, modify the message body so that you can see the routes that the report should have used. If for some reason (say you press CTRL + C), the MINA server running on localhost: 9991 is dead, then the loadbalancer will automatically start using the MINA server running on localhost: 9992. After this MINA server receives the report he will send it to the MINA server running on localhost: 9993 as nothing happened. If localhost: 9991 returns again, then the loadbalancer will start using it again.

The load balancer will always try to use localhost: 9991 until trying to use localhost: 9992 no matter what.

Running example

To compile and install the project in your maven repo, run the following command in the root of the project

mvn clean install

To run this example, run the following command in the appropriate folder:

minΓ€1 and:
mvn exec: java -Pmina1

mina2: mvn exec: java -Pmina2

mina3: mvn exec: java -Pmina3

load balancer: mvn exec: java -Ploadbalancer

If you encounter any problems, let us know on the Camel forum
http://camel.apache.org/discussion-forums.html


Pedro Martins!


EDIT

In a previous post, I had 2 questions: 1. How to do this in java dsl 2. why mini-servers send answers.

In the end, I will attack problem 1, but I just want to say that the solution to problem 2 is here: http://camel.465427.n5.nabble.com/Load-balancing-using-Mina-example-with-Java-DSL -td5742566.html # a5742585

Kudos to Mr. Klaus for the answer and suggestions.


EDIT

Both problems are now resolved, and they are both in the same git repository. Hope my code helps people.

+1
source

All Articles