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

标签 apache-camel

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

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

关于apache-camel - Camel - 如何在异常时停止路由执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32379367/

相关文章:

apache-camel - 如何将参数传递给 Camel 路线?

java - 使用 @SpringBootTest 进行 Spring Boot 和 Camel 测试

java - Camel : Iterate the elements in the header, 并行处理

java - Camel Jetty 将使用 Spring Boot 端口

java - Camel路由设置变量

java - Camel-Twitter 直接和基于事件之间的区别

java - Camel 和 Google App Engine 没有 Spring?

java - Camel 漂亮印花

spring-boot - 使用 Spring Boot 2.2.4 更新到 Camel 3.0.0-RC3 后,Camel 无法在文本属性中找到 {{xxx}}

java - 为动态 uri 创建 ProducerTemplate