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

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

    e0richt

      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

        • 1. Re: custom load balancer / failover component handling an exception...
          e0richt

          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 6:30 PM

          • 2. Re: custom load balancer / failover component handling an exception...
            davsclaus

            If there is an exception its stored on the exchange when the process method returns. So you need to check for that

             

            Exception cause = exchange.getException();
            if (cause != null) {
               ... 
            }
            

             

            • 3. Re: custom load balancer / failover component handling an exception...
              e0richt

              ok, I will try that...

              However, I am curious then why the example code (which I basically copied from the fuse documentation) has the try... catch code at all then?

               

              also interestingly enough if you use the exception handler option for the route you can't seem to get the exception through that mechanism (x = ex.getException()...)

              you actually have to use a header property to get the exception...

              • 4. Re: custom load balancer / failover component handling an exception...
                e0richt

                yup, that suggestion works...

                 

                kinda weird how one has to use ex.getException() for some things and use the header property for others...

                • 5. Re: custom load balancer / failover component handling an exception...
                  davsclaus

                  If you have a copy of Camel in Action book then read chapter 5. Its explained there.

                  • 6. Re: custom load balancer / failover component handling an exception...
                    davsclaus

                    No its not. The header has a caused exception that an error handler has handled / dealt with.

                     

                    The getException is when an exception just occurred and hasn't been handled by an error handler.