如何在Camel errorHandler中访问异常?

时间:2015-05-21 15:35:50

标签: java exception-handling apache-camel

使用案例

我希望以可重用的方式记录Camel中特定路由的未处理异常。具体来说,我想从异常中提取足够的信息并将其写入数据库,然后由某些监控软件读取。通过使用死信队列,我能够获得在那里路由的特定路由的所有未处理的异常。不幸的是,当它被传递给记录器时,交换中的异常总是null

示例代码

下面的代码强制抛出异常,然后将其路由到错误处理程序。我期待exchange.getException()返回抛出的异常,但在这种情况下它是null

Java代码

public class JobRunner {

    public void run(Exchange exchange) {
        throw new RuntimeException("Hello, World!");
    }

    public void processException(Exchange exchange) {
        Exception e = exchange.getException();
        // e is null
    }

}

Camel Config

<camelContext xmlns="http://camel.apache.org/schema/spring">

    <errorHandler id="jobErrorHandler"
        type="DeadLetterChannel"
        deadLetterUri="seda:errorHandler" />

    <route id="routeErrorHandler">
        <from uri="seda:errorHandler" />
        <bean ref="jobRunner" method="processException" />
    </route>

    <route id="scheduled_job" errorHandlerRef="jobErrorHandler">
        <from uri="quartz2://test/job?cron=0+*/5+*+*+*+?" />
        <bean ref="jobRunner" method="run" />
    </route>

</camelContext>

问题

  • 如何将异常路由到错误处理程序?

1 个答案:

答案 0 :(得分:0)

经过一番挖掘后,我发现原始异常存储为Exchange中的属性。

9 132185894 132209127 DUP 99 23234