You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Camel NotifyBuilder抛出异常时不匹配问题排查

问题原因与解决方案

核心原因

  1. direct:route-b 匹配失败
    当消息在direct:route-b中抛出异常后,全局Dead Letter Channel会拦截异常并转发到死信队列,但此时direct:route-b对应的Exchange状态是失败(failed),而非whenDone(1)所匹配的"完成"状态。尽管文档说明whenDone包含失败消息,但这里Exchange的生命周期被错误处理器中断,并未触发done事件,因此whenDone无法匹配。

  2. direct:dead-letter 匹配失败
    NotifyBuilder默认不监听错误处理器内部发起的路由操作(比如死信队列的消息转发),必须显式开启该监听才能捕获死信队列的处理事件。

修复后的测试代码

@Test
public void testNotify() {
    NotifyBuilder notifierA = new NotifyBuilder(camelContext)
            .from("direct:route-a")
            .whenDone(1)
            .create();
    // 改用whenFailed匹配route-b的失败事件
    NotifyBuilder notifierB = new NotifyBuilder(camelContext)
            .from("direct:route-b")
            .whenFailed(1)
            .create();
    // 添加includeErrorHandlers()监听错误处理器发起的路由
    NotifyBuilder notifierDead = new NotifyBuilder(camelContext)
            .includeErrorHandlers()
            .from("direct:dead-letter")
            .whenDone(1)
            .create();

    producerTemplate.sendBody("direct:route-a", null);

    assertThat(list).containsExactly("a", "b", "dead");
    assertThat(notifierA.matches(5, TimeUnit.SECONDS)).isTrue();
    assertThat(notifierB.matches(5, TimeUnit.SECONDS)).isTrue();
    assertThat(notifierDead.matches(5, TimeUnit.SECONDS)).isTrue();
}

补充说明

  • whenDone仅匹配生命周期正常结束的Exchange(无论成功或失败),但当异常被错误处理器接管时,原路由的Exchange生命周期被中断,只会触发failed事件。
  • includeErrorHandlers()是捕获死信队列路由事件的关键,它让NotifyBuilder覆盖错误处理器内部的路由操作。

内容的提问来源于stack exchange,提问作者lance-java

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 12:06:09