Camel - 如何在异常时停止路由执行?

Vij*_*ana 6 apache-camel

有什么办法可以在捕获异常时停止路由执行(在显示日志消息后)?

        <doTry>
            <process ref="messageProcessor"/>
            <doCatch>
                <exception>java.lang.IllegalArgumentException</exception>
                <log message="some message related to the exception" />
            </doCatch>
        </doTry>
Run Code Online (Sandbox Code Playgroud)

请提供一种在 Spring DSL 中实现此目的的方法。我已经尝试过 < stop/> ,但是没有显示日志消息。

Vij*_*ana 6

在 doCatch 中添加了一个进程,该进程停止的是 Camel 上下文。

        <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>
Run Code Online (Sandbox Code Playgroud)

处理器:

@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();
}
}
Run Code Online (Sandbox Code Playgroud)

  • 您在 Java DSL 中找到过解决方案吗? (3认同)

Chr*_*eau 5

有很多方法可以解决这个问题。除了接受的答案之外,您可能还想使用以下选项之一:

onException(SomeException.class)
.log("Uh oh...")
.stop()
Run Code Online (Sandbox Code Playgroud)

在你的 DSL 中

或者:

exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)

在你的 Processor() 中

在这里查看有关此主题的官方文档: http: //camel.apache.org/intercept.html

  • 具有生产者 (`log(...)`) 的全局范围 `onException()` (在任何路由之外)会创建一个额外的路由,并且其中的 `stop()` 仅停止此错误处理路由本身,而不是该路由最初抛出异常的地方。 (5认同)