如何访问 Camel errorHandler 中的异常?

How to access exception in Camel errorHandler?

用例

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

示例代码

下面的代码强制抛出一个异常,然后将其路由到错误处理程序。我期待 exchange.getException() 到 return 抛出的异常,但在这种情况下它是 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
    }

}

骆驼配置

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

问题

经过一些挖掘,我发现原来的异常在 Exchange 中存储为 属性。

Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);