Camel-如何在异常情况下停止路由执行? [英] Camel - How to stop route execution on exception?

查看:402
本文介绍了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();
}
}

这篇关于Camel-如何在异常情况下停止路由执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆