6 Replies Latest reply on Mar 14, 2013 3:36 AM by Claus Ibsen

    custom load balancer / failover component handling an exception...

    Richard T Newbie

      hi,

       

      I have a camel route that has an activemq producer that sends messages to a primary

      node while available. if the connection to the primary node fails then the messages should be sent to the backup node.

       

      something like this:

       

      .from("activemqInput")

          .to("mybean")

          .loadbalancer().failover()

               .to("activemq-node1", "activmq-node2");

       

      so this basically works but my problem is that I need to

      sense/determine which node is active when switchover occurs.

       

      so I tried to use the custom LoadBalancerSupport (as per Fuse documentation)

       

      .from(...)

         .to(...)

         .loadbalancer(new MyLoadBalancer())

            .to(....., .....);

       

      expecting that when an exception occurs on one of the nodes that I could catch it and determine which node is the one that failed.

       

      unfortunately, this does not seem to happen....

      my load balancer class follows:

       

      public class ErrorHandler extends LoadBalancerSupport {

           private static final Logger logger = LoggerFactory.getLogger(ErrorHandler.class);

           static int NUM_PROCESSORS = 2;

           int which = 0;

             

          public boolean process(Exchange exchange, AsyncCallback callback) {

              String body = exchange.getIn().getBody(String.class);

              logger.info("ErrorHandler: got message:"+ body);

              System.out.println("ErrorHandler: got message:"+ body);

              try {

                   logger.info("ErrorHandler: going to:"which" processor");

                   System.out.println("ErrorHandler: going to:"which" processor");

                   getProcessors().get(which).process(exchange);

              } catch (Throwable e) {

                   logger.info("ErrorHandler: caught an exception:"+e.getMessage());

                   System.out.println("ErrorHandler: caught an exception:"+e.getMessage());

                   which = (++which) % NUM_PROCESSORS;

                  exchange.setException(e);

              }

              callback.done(true);

              return true;

          }

       

      Edited by: e0richt on Mar 13, 2013 5:41 PM