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

Camel - How to stop route execution on exception?

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

        <doTry>
            <process ref="messageProcessor"/>
            <doCatch>
                <exception>java.lang.IllegalArgumentException</exception>
                <log message="some message related to the exception" />
            </doCatch>
        </doTry>

请提供在 Spring DSL 中实现此目的的方法。我已经尝试过 但它不显示日志消息。

在 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>

处理器:

@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();
}
}

如果创建OnException Route? 像这样:

onException(Exception.class)
        .log("YOUR LOG EXCEPTION");

from("direct:start")...

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

onException(SomeException.class)
.log("Uh oh...")
.stop()

在您的 DSL 中

或:

exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);

在你的处理器中()

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

停止路由当前执行,但路由仍在监听 route.onException(Throwable.class).process(ThrowableProcessor.NAME).handled(true);

以上回答告诉你如何处理异常。但是,要阻止路由抛出异常,请指定:

?throwExceptionOnFailure=false

在 URI 中。

示例:

.to(http://localhost:8000/4d-analytics-mock-service/iso/retrieve?throwExceptionOnFailure=false)