Camel Exception handling doesnt work if exception clause is defined in a separate class

前端 未结 2 1325
面向向阳花
面向向阳花 2021-01-04 18:24

I am trying to build a application with several camel routes which re use many common routes internally. Hence, I am trying to segregate the routes in several different Rou

相关标签:
2条回答
  • 2021-01-04 18:42

    Based on the accepted answer, I found a cleaner way to implement exception handling, so you don't have to call super.configure() in every route. Just call a method that handles onException in the constructor of the base class.

    //Base class that does exception handling
    public abstracExceptionRouteBuildert class BaseAbstractRoute extends RouteBuilder {
      protected BaseAbstractRoute() {
        handleException();
      }
    
      private void handleException() {
        onException(Exception.class).handled(true).to("mock:error");
      }
    }
    
    //Extend the base class
    public class MyRouteBuilder extends BaseAbstractRoute {
      @Override
      public void configure() throws Exception {
        from("direct:start").throwException(new Exception("error"));
      }
    }
    
    0 讨论(0)
  • 2021-01-04 18:46

    correct, the onException() clauses only apply to the current RouteBuilder's route definitions...

    that said, you can reuse these definitions by having all your RouteBuilders extend the ExceptionRouteBuilder and call super.configure()...something like this

    public class MyRouteBuilder extends ExceptionRouteBuilder {
        @Override
        public void configure() throws Exception {
            super.configure();
            from("direct:start").throwException(new Exception("error"));
        }
    }
    ...
    public class ExceptionRouteBuilder implements RouteBuilder {
        @Override
        public void configure() throws Exception {
            onException(Exception.class).handled(true).to("mock:error");
        }
    }
    

    or even just have a static method in an ExceptionBuilder class to setup the clauses for a given RouteBuilder instance

    public class MyRouteBuilder extends RouteBuilder {
        @Override
        public void configure() throws Exception {
            ExceptionBuilder.setup(this);
            from("direct:start").throwException(new Exception("error"));
        }
    }
    ...
    public class ExceptionBuilder {
        public static void setup(RouteBuilder routeBuilder) {
            routeBuilder.onException(Exception.class).handled(true).to("mock:error");
        }  
    }
    
    0 讨论(0)
提交回复
热议问题