2017-04-20 76 views
0

我試圖處理使用自定義處理器的異常。我的路線建設者看起來是這樣的:模仿Apache Camel DefaultErrorHandler

onException(Exception.class) 
    .maximumRedeliveries(0) 
    .process(exceptionProcessor) 
    .handled(true) 
    .to("file:C:\\Mahesh\\delete\\failedrequests"); 

我的異常處理器看起來是這樣的:

public class ExceptionProcessor implements Processor 
{ 
    private static final Logger LOGGER = ExLoggerFactory.getLogger(ExceptionProcessor.class); 

    @Override 
    public void process(Exchange exchange) throws Exception 
    { 
     Exception ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); 
     Object messageHistroy = exchange.getProperty(Exchange.MESSAGE_HISTORY, Object.class); 

     LOGGER.error("\n\nMessage History\n---------------------------------------------------------------------------------------------------------------------------------------\n" 
        + messageHistroy 
        +"\n\nStacktrace\n---------------------------------------------------------------------------------------------------------------------------------------" 
        ); 
     ex.printStackTrace(); 
    } 
} 

正如你所看到的,上面我試圖模仿DefaultErrorHandler的行爲:它首先打印消息歷史記錄然後堆疊跟蹤。

但是,我的ExceptionProcessor不像DefaultErrorHandler那樣以相同的方式打印消息歷史記錄。 DefaultErrorHandler打印這樣的:

Message History 
--------------------------------------------------------------------------------------------------------------------------------------- 
RouteId    ProcessorId   Processor                  Elapsed (ms) 
[route1   ] [route1   ] [file://C:%5CMahesh%5Cdelete%5Ccamelsource          ] [  121] 
[route1   ] [process1   ] [[email protected]               ] [  120] 
[     ] [to1    ] [file:C:\Mahesh\delete\badworkitems           ] [  98] 

而我ExceptionProcessor打印這樣的:

Message History 
--------------------------------------------------------------------------------------------------------------------------------------- 
[DefaultMessageHistory[routeId=route1, node=process2], DefaultMessageHistory[routeId=null, node=process1]] 

問:看來,我的處理器是不是能夠得到處理器和第三排第三列到端點。我如何得到這個。

此外,打印消息歷史記錄之前,也DefaultErrorHandler此打印:

o.a.camel.processor.DefaultErrorHandler : Failed delivery for (MessageId: ID-01HW865638-58603-1492677082431-0-7 on ExchangeId: ID-01HW865638-58603-1492677082431-0-8). Exhausted after delivery attempt: 1 caught: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries 
at [Source: C:\Mahesh\delete\camelsource\file1.json; line: 4, column: 3]. Processed by failure processor: FatalFallbackErrorHandler[Channel[sendTo(file://C:%5CMahesh%5Cdelete%5Cbadworkitems)]] 

問:正如你所看到的,DefaultFaultHandler還能夠打印消息ID,交換ID,源文件名和發送 - 到文件夾名稱。我想知道從哪裏得到這些信息。對於源文件名,我嘗試了所有Exchange.FILE_*屬性,但全部打印爲null

回答

0

只要看看駱駝源代碼如何打印該信息,因爲它使用了您也可以訪問的消息歷史記錄。你可以在github上找到源代碼。

https://github.com/apache/camel/