骆驼 AdviceWithRouteBuilder 弃用

camel AdviceWithRouteBuilder deprecation

我正在使用 camel 2.15.1 并尝试使用 adviceWith() 但我不断收到弃用警告。以下是相关片段:

routeDefinition.adviceWith(camelContext, new AdviceWithRouteBuilder(){
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("direct:doSomething")
                .skipSendToOriginalEndpoint()
        }
    });

我知道我可以通过将 camelContext 强制转换为 ModelCamelContext 来避免弃用警告,但是这样的强制转换有点难闻。铸造是正确的处理方式吗?

https://camel.apache.org/advicewith.html

这些弃用已在未来的版本中删除,因为我们真的只是希望人们从 CamelContext.

访问他们需要的所有内容

但是它有一个 adapt 方法,所以你可以在没有类型转换的情况下适应类型

ModelCamelContext mcc = context.adapt(ModelCamelContext.class);

这解决了我的单元测试问题:

public void testMe() throws Exception{
    CamelContext context = this.context();

    // Get the route that we're after by ID.
    RouteDefinition route = context.getRouteDefinition("<routeID>");

    //override the default send endpoint.
    route.adviceWith(context.adapt(ModelCamelContext.class), new RouteBuilder() {
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:overrideme")
                .skipSendToOriginalEndpoint()
                .to("mock:inbound");
        }
    });
}

adviceWith 方法已按说明移动 here