Pergunta

I have code a loadbalancer that generates a report every 10 seconds and sends it to a MINA server on localhost:9991 or to a MINA server on localhost:9992 should the first one fail. Once the MINA servers receive the report, they change it and send it back to the loadbalancer, which then print the body of the report.

public class MyApp_A {

    public static void main(String... args) throws Exception {
        // create CamelContext
        CamelContext context = new DefaultCamelContext();


    ConnectionFactory connectionFactory = 
            new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    context.addComponent("jms", jmsComponentClientAcknowledge(connectionFactory));

        context.addRoutes(
                new RouteBuilder(){

                    @Override
                    public void configure() throws Exception {
                        from("timer://org.apache.camel.example.loadbalancer?period=10s")
                        .beanRef("service.Generator", "createReport")
                        .to("direct:loadbalance");

                        from("direct:loadbalance")
                            .loadBalance().failover()
                                // will send to A first, and if fails then send to B afterwards
                                .to("mina:tcp://localhost:9991?sync=true")
                                .to("mina:tcp://localhost:9992?sync=true")
                            .log("${body}")
                        .end();
                    }
                }
                );

        context.start();
    }
}

However, once I execute the loadbalancer it finishes immediately. I tried fixing this problem by adding an infinite loop after the context.start() call:

while(true){
    /* Dummy for loop, so our server does not die immediately after 
    * being born and can answer and send requests.
     * */
}

However this is a terrible solution because the program will simply get stuck on the loop and will stop generating reports and sending them.

How do I fix this? How do I keep my loadbalancer running while being able to generate requests and print the reports that it receives?

Foi útil?

Solução

There is an class called Main provided in camel-core to do exactly what you are looking for. Take a look to this website: http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top