【问题标题】:Camel - How to stop route execution on exception?Camel - 如何在异常时停止路由执行?
【发布时间】:2015-09-03 15:08:31
【问题描述】:

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

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

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

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

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

    【讨论】:

    • 你有没有在 Java DSL 中找到解决方案?
    【解决方案2】:

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

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

    在你的 DSL 中

    或:

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

    在你的 Processor() 中

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

    【讨论】:

    • 具有生产者 (log(...)) 的全局范围 onException()(在任何路由之外)创建一个额外的路由,其中​​的 stop() 仅停止此错误处理路由本身,而不是在最初抛出的异常。
    【解决方案3】:

    如果你创建一个 OnException 路由? 像这样:

    onException(Exception.class)
            .log("YOUR LOG EXCEPTION");
    
    from("direct:start")...
    

    【讨论】:

    • 对这段代码的一些解释可能会有所帮助。
    • 对不起,我写得很快。如果您使用路由 OnException,则会为所有路由错误创建一个通用异常,因此如果您只需要停止执行,我相信这是记录某些内容并停止路由的最佳方式。
    • 请记住,OnException 路由是普通路由,因此您可以根据需要使用进程或 bean。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多