Apache camel 更改weave语句的顺序时,Camel adviceWith的行为不同

Apache camel 更改weave语句的顺序时,Camel adviceWith的行为不同,apache-camel,Apache Camel,出于演示目的,我有以下路线 from("direct:external") .routeId("external") .to("http4://www.third-party.com/foo").id("ext"); 为了测试,我想 *将http4:endpoint替换为direct:endpoint *在路由的末尾添加一个mock:endpoint以进行验证 我在RouteBuilder中添加了以下建议 context.getRouteDefinition("external

出于演示目的,我有以下路线

from("direct:external")
    .routeId("external")
    .to("http4://www.third-party.com/foo").id("ext");
为了测试,我想 *将http4:endpoint替换为direct:endpoint *在路由的末尾添加一个mock:endpoint以进行验证

我在RouteBuilder中添加了以下建议

context.getRouteDefinition("external").adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        weaveAddLast().to("mock:result");
        weaveByToUri(".*http4://.*")
            .replace()
            .to("direct:foo");
    }
});
这一条似乎有效,但如果我改变
weave*
语句的顺序,就像这样

public void configure() throws Exception {
    weaveByToUri(".*http4://.*")
        .replace()
        .to("direct:foo");
    weaveAddLast().to("mock:result");
}
它给了我以下的错误

java.lang.IllegalArgumentException:没有匹配的输出:*路由中:路由(外部)[[From[direct:external]]->[pipeline->[[To[direct:foo]]]]]]]


实际上,我希望得到相同的结果,与顺序无关。

这里需要注意的一点是,
weave*
调用只知道原始的RouteBuilder。因此,当您首先执行
weaveByUri()
调用时,它将
.to(“http4://www.third-party.com/foo”)
替换为
.to(“direct:foo”)
,这恰好是路由中的最后一个端点。现在,当您执行
weaveAddLast()
调用时,它会查找
“http4://www.third-party.com/foo”
,但找不到它,因为它被
“direct:foo”
替换。这将导致引发异常

因此,假设在
“http4…”
端点之后有另一个端点,因此它不再是路线中的最后一个端点,那么您的
adviceWith()
应该可以工作。例如,如果您的原始路线如下所示,它将起作用:

from("direct://external")
  .routeId("external")
  .to("http4://www.third-party.com/foo")
  .id("ext")
  .to("direct://bar")
;

我应该注意的是,我认为这是一个bug,顺序应该无关紧要。

对于Camel的大部分用法,我从未使用过编织函数来实现您想要实现的功能。使用
interceptSendToEndpoint(fromEndpoint).skipSendToOriginalEndpoint().to(toEndpoint)怎么样?因为camel测试包实际上带来的痛苦多于好处,所以我创建了一个camel测试支持包,它使用起来稍微容易一点。请查看,附示例:完美答案,很遗憾你在赏金超时之前没有回答(这会给你500分)。谢谢我记录了一张罚单,看看我们是否可以改进/修复此问题: