Camel - 如何在异常时停止路由执行?
问题描述:
有什么办法可以在捕获异常时停止路由执行(在显示日志消息后)?
Is there any way I can stop the route execution (after displaying a log message) when exception is caught?
<doTry>
<process ref="messageProcessor"/>
<doCatch>
<exception>java.lang.IllegalArgumentException</exception>
<log message="some message related to the exception" />
</doCatch>
</doTry>
请提供一种在 Spring DSL 中实现此目的的方法.我已经试过了 <stop/> 但不显示日志消息.
Please provide a way to achieve this in Spring DSL. I've already tried < stop/> but that doesn't display log message.
答
在 doCatch 中添加了一个进程,该进程停止的是 Camel 上下文.
Added a process in doCatch which stop's a Camel context.
<doTry>
<process ref="messageProcessor"/>
<doCatch>
<exception>java.lang.IllegalArgumentException</exception>
<handled>
<constant>true</constant>
</handled>
<setHeader headerName="exceptionStackTrace">
<simple>${exception.stacktrace}</simple>
</setHeader>
<process ref="mandatoryParameterIsNull"/>
</doCatch>
</doTry>
处理器:
@Component
public class MandatoryParameterIsNull implements Processor{
Logger log = Logger.getLogger(MandatoryParameterIsNull.class);
@Override
public void process(Exchange exchange) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Some parameter is mandatory");
log.debug(exchange.getIn().getHeader("exceptionStackTrace"));
}
exchange.getContext().getShutdownStrategy().setLogInflightExchangesOnTimeout(false);
exchange.getContext().getShutdownStrategy().setTimeout(60);
exchange.getContext().stop();
}
}