所以,我有一个 Camel 上下文,它有一个 DeadLetterChannel
,在上下文级别没有重新传递。在上下文中我有一条路线:
from("direct:input")
.routeId("enrichHeader")
.setHeader("myHeader").constant("someValue)
.to("direct:output");
我有一个单元测试,其中我 interceptSendToEndpoint("direct:output")
并引发异常。
然后在单元测试中我执行 ProducerTemplate.sendBody(theBody)
。
现在,按照上面配置的异常消息将被发送到死信 channel 。一切都好。但是,如果我配置一个loggingErrorHandler,我会得到奇怪的行为。因此,如果我按如下方式更新上述路线:
from("direct:input")
.routeId("enrichHeader")
.errorHandler(
loggingErrorHandler("com.myco.myproject")
.level(LoggingLevel.WARN))
.setHeader("myHeader").constant("someValue)
.to("direct:output");
当我现在运行测试时,我遇到了两个意外的行为。首先,异常仍然记录在 ERROR
级别,而不是配置的 WARN
级别。其次,异常被传播回 ProducerTemplate.sendBody(theBody) 调用。
一方面,ERROR
处的日志记录显然很奇怪。看起来奇怪的另一部分是处理程序正在传播异常。我本以为它会将其标记为已处理并完成处理。
这是预期的行为吗?似乎缺少 LoggingErrorHandler
的文档。
最佳答案
LoggingErrorHandler
只是记录异常。
如果你想跳过异常,你必须定义一个策略,如下所示
onException(com.myco.myproject.MyException.class).continued(true);
或者你必须使用 doTry()..doCatch()
就像 java 中的 try{...} catch{...}
block 一样。
例如:
from("direct:input").routeId("enrichHeader")
.doTry()
.setHeader("myHeader").constant("someValue)
.to("direct:output")
.doCatch(MyException.class)
.process(new MYExceptionLoggerAndHandler())
.end();
关于java - Camel 中 LoggingErrorHandler 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15408530/