Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Camel onException

I want to catch all Exception from routes.

I add this OnExeption :

onException(Exception.class).process(new MyFunctionFailureHandler()).stop();

Then, I create the class MyFunctionFailureHandler.

public class MyFunctionFailureHandler  implements Processor {

@Override
public void process(Exchange exchange) throws Exception {
    Throwable caused;

    caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);

    exchange.getContext().createProducerTemplate().send("mock:myerror", exchange);
   }

}

Unfortunately, it doesn't work and I don't know why.

if there is an Exception, the program must stop.

How can I know why this code doesn't work!!

Thanks.

like image 245
Kikou Avatar asked Jan 07 '13 14:01

Kikou


1 Answers

I used this on my route:

public class MyCamelRoute extends RouteBuilder {

   @Override
   public void configure() throws Exception {

        from("jms:start")
           .process(testExcpProcessor)

       // -- Handle Exceptions
       .onException(Exception.class)
         .process(errorProcessor)
         .handled(true)

       .to("jms:start");
   }
}

and in my errorProcessor

public class ErrorProcessor implements Processor {

  @Override
  public void process(Exchange exchange) throws Exception {


    Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);

    if(cause != null){
        log.error("Error has occurred: ", cause);

        // Sending Error message to client
        exchange.getOut().setBody("Error");
    }else

        // Sending response message to client
        exchange.getOut().setBody("Good");
  }
}

I hope it helps

like image 121
Aalkhodiry Avatar answered Oct 29 '22 14:10

Aalkhodiry