2015-09-03 51 views
3

當異常被捕獲時,有什麼方法可以停止路由執行(顯示日誌消息之後)?駱駝 - 如何停止異常的路由執行?

 <doTry> 
      <process ref="messageProcessor"/> 
      <doCatch> 
       <exception>java.lang.IllegalArgumentException</exception> 
       <log message="some message related to the exception" /> 
      </doCatch> 
     </doTry> 

請提供一種在Spring DSL中實現這一點的方法。我已經嘗試過< stop />但不顯示日誌消息。

回答

2

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

你有沒有找到Java DSL中的解決方案? – KDoyle

0

如果您創建一個onException的路線? 像這樣:

onException(Exception.class) 
     .log("YOUR LOG EXCEPTION"); 

from("direct:start")... 
+0

關於這段代碼的一些解釋可能是有益的。 – Pyves

+0

對不起,我寫得很快。 如果使用路由OnException,則會爲所有路由錯誤創建一個通用異常,所以如果您只需停止執行,我相信這是記錄某些內容並停止路由的最佳方式。 –

+0

只是爲了記住,OnException路由是一條正常的路由,所以如果你願意,你可以使用一個進程或一個bean。 –

0

有很多方法可以解決這個問題。除了公認的答案,你可能還需要使用下列之一:你的處理器()

有內

exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE); 

:您的DSL

或內

onException(SomeException.class) 
.log("Uh oh...") 
.stop() 

看看這個主題的官方文檔在這裏:http://camel.apache.org/intercept.html