2 回答

TA貢獻1811條經(jīng)驗 獲得超5個贊
根據(jù)https://people.apache.org/~dkulp/camel/try-catch-finally.html(參見Camel error handling is disabled部分),使用doTry .. doCatch .. doFinally
Camel Error Handler 時不適用。因此,OnException
不會觸發(fā)任何。
如果你想用 捕獲異常,OnException
你應(yīng)該直接拋出它而不是在DoTry .. DoCatch
. 現(xiàn)在您可能想創(chuàng)建兩個onException
,一個處理Exception.class
,一個處理JAXBException.class
。
onException(Exception.class) .handled(true) .log("Globally Caught CustomException") .end(); onException(JAXBException.class) .handled(true) .throwException(new CustomException()) .end();
但是第一個onException
不會被調(diào)用,因為Camel 不允許在已經(jīng)處理錯誤的情況下進行進一步的錯誤處理。這是由 完成的org.apache.camel.processor.FataFallbackErrorHandler
,它捕獲新的異常,記錄警告,將其設(shè)置為 Exchange 上的異常,并停止任何進一步的路由(Camel In Action,第二版)。

TA貢獻1784條經(jīng)驗 獲得超7個贊
試試這個,我剛剛修改了您的代碼以重現(xiàn)它似乎有效:
onException(Exception.class)
.handled(true)
.log("Globally Caught CustomException")
.end();
from("timer://simpleTimer?period=1000")
.setBody(simple("Timer at ${header.firedTime}"))
.to("direct:demo");
from("direct:demo")
.doTry()
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
throw new JAXBException("Some Exception");
}
})
.doCatch(JAXBException.class)
.log("Locally Caught JAXBException")
.throwException(new CustomException())
.endDoTry();
日志輸出:
添加回答
舉報