簡體   English   中英

如何在Camel errorHandler中訪問異常?

[英]How to access exception in Camel errorHandler?

用例

我想以可重用的方式記錄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
    }

}

駱駝配置

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM