0

我正在寻找一种在多个 RouteTemplates 生成的所有路由上添加全局异常处理的方法。我尝试了以下方式,但 onException 块没有被添加到路由中。

你能帮我理解我做错了什么吗?

谢谢

public abstract class BaseRouteBuilder extends RouteBuilder {
    @Override
    public void configure(){
        this.onException(IllegalStateException.class)
                .log("global onException")
                .maximumRedeliveries(2)
                .redeliveryDelay(100)   
                .logStackTrace(true)
                .to("direct:retryChannel");
    }
}
public static class SampleRouteTemplate extends BaseRouteBuilder {
        @Override
        public void configure() {
            super.configure();
            this.routeTemplate("myTemplate")
                    .templateParameter("parameter1")
                    .from("direct:start")
                    .setHeader("parameter1", constant("value1"))
                    .log("RouteCompleted");
        }
    }
4

1 回答 1

0

尝试为路由提取一个单独的方法,例如configureRoute()并在configure() method. 还要使子类成为非静态的。

public abstract class BaseRouteBuilder extends RouteBuilder {
    @Override
    public final void configure(){
        onException(IllegalStateException.class)
            .log("global onException")
            .maximumRedeliveries(2)
            .redeliveryDelay(100)   
            .logStackTrace(true)
            .to("direct:retryChannel");
            
        configureRoute();
    }
    
    public abstract void configureRoute();
}
public class SampleRouteTemplate extends BaseRouteBuilder {
        @Override
        public void configureRoute() {
            this.routeTemplate("myTemplate")
                .templateParameter("parameter1")
                .from("direct:start")
                .setHeader("parameter1", constant("value1"))
                .log("RouteCompleted");
        }
    }
}
于 2021-07-26T09:17:07.080 回答