Camel-在全局onException中捕获引发的异常

时间:2019-07-02 11:32:08

标签: java spring exception apache-camel

我正在尝试在全局onException中捕获自己的异常。在捕获到Jaxb异常后,我抛出了异常。但是,CustomException不会被onException捕获

onException(Exception.class)
    .handled(true)
    .log("Globally Caught CustomException") 
    .end();

from("start:direct")
    .doTry()
        .unmarshal(soapMessage)
    .doCatch(JAXBException.class)
        .log("Locally Caught JAXBException")
        .throwException(new CustomException()
    .endDoTry();

2 个答案:

答案 0 :(得分:0)

尝试一下,我刚刚修改了您的代码以重现它的工作原理:

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

日志输出: enter image description here

答案 1 :(得分:0)

根据https://people.apache.org/~dkulp/camel/try-catch-finally.html(请参见禁用骆驼错误处理),当不使用SocketIOClient{/}: Adding handler for event: connect SocketIOClient{/}: Adding handler for event: image SocketIOClient{/}: Handling event: error with data: ["Tried emitting when not connected"] SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] SocketIOClient{/}: Joining namespace / SocketManager: Tried connecting socket when engine isn't open. Connecting SocketManager: Adding engine SocketManager: Manager is being released SocketEngine: Starting engine. Server: http://127.0.0.1:1337 SocketEngine: Handshaking SocketIOClient{/}: Client is being relea2 SocketEnginePolling: Doing polling GET http://127.0.0.1:1337/socket.io/?transport=polling&b64=1 SocketEnginePolling: Got polling response SocketEnginePolling: Got poll message: 96:0{"sid":"4k2wFGd250H88zmYAAAE","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:40 SocketEngine: Got message: 0{"sid":"4k2wFGd250H88zmYAAAE","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} SocketEngine: Got message: 40 SocketEngine: Writing poll: has data: false SocketEnginePolling: Sending poll: as type: 2 SocketEnginePolling: Created POST string: 1:2 SocketEnginePolling: POSTing SocketEngine: Engine is being released 骆驼错误处理程序时。因此,不会触发任何doTry .. doCatch .. doFinally

如果您想用OnException捕获异常,则应该直接将其抛出,而不要放在OnException内。现在您可能会考虑创建两个DoTry .. DoCatch,一个处理onException,另一个处理Exception.class

JAXBException.class

但是不会再次调用第一个onException(Exception.class) .handled(true) .log("Globally Caught CustomException") .end(); onException(JAXBException.class) .handled(true) .throwException(new CustomException()) .end(); ,因为 Camel在已经处理错误的同时不允许进一步的错误处理onException可以做到这一点,org.apache.camel.processor.FataFallbackErrorHandler可以捕获新的异常,并记录警告,将其设置为Exchange上的异常,然后停止任何进一步的路由(Camel In Action,第二版)。