如果在单独的类中定义了异常子句,则骆驼异常处理不起作用 [英] Camel Exception handling doesnt work if exception clause is defined in a separate class

查看:19
本文介绍了如果在单独的类中定义了异常子句,则骆驼异常处理不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多条骆驼路线构建一个应用程序,这些路线在内部重新使用了许多常用路线.因此,我试图在几个不同的 Route Builder 类中隔离路由,然后在需要的地方连接路由.

I am trying to build a application with several camel routes which re use many common routes internally. Hence, I am trying to segregate the routes in several different Route Builder classes and then connecting the routes where needed.

例如,所有与发送电子邮件有关的路由都进入一个 EmailRouteBuilder 类,所有处理特定 JMS 队列的路由进入 MyQueueRouteBuilder 类.我想这应该没问题,因为 Camel 不区分类,只查找路由定义.

For eg, all routes pertaining to sending emails go into a EmailRouteBuilder class and all routes dealing with a particular JMS Queue go into MyQueueRouteBuilder class. I suppose this should be alright since Camel doesnt not distinguish between classes and only looks for routes defininition.

此外,我还将几个异常处理路由分组到一个单独的 ExceptionHandlingRouteBuilder 中.

In addition, I am also grouping several exception handling routes into a separate ExceptionHandlingRouteBuilder.

我还通过在 Spring 中定义骆驼上下文来将所有不同的类连接在一起 -

I am also connecting all the different classes together by defining the camel context in Spring like so -

<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
    <propertyPlaceholder id="properties" location="classpath:${env}/autoimport.properties"/>
    <!-- Common Routes -->
    <routeBuilder ref="emailRouteBuilder" />
    <routeBuilder ref="myQueueRouteBuilder" />
  <routeBuilder ref="httpRouteBuilder" />
    <routeBuilder ref="exceptionsRouteBuilder" />
    <routeBuilder ref="customer1RouteBuilder" />
    <routeBuilder ref="customer2RouteBuilder" />

</camelContext>

我的 exceptionsRouteBuilder 包含许多异常子句,例如 -

My exceptionsRouteBuilder contains many exception clauses like -

onException(ConnectException.class)
            .routeId("connectExceptionEP")
            .handled(true)
            .log("Caught Exception: ")
            .to("direct:gracefulExit");

..
..
..

但是,在另一个类中定义的异常似乎存在问题,或者就此而言,在主路由定义之外单独定义.

However, it looks like there is a problem with the exceptions being defined in another class, or for that matter, defined separately out of the main route definition.

我通过查找正在启动的路由(通过 routeId )并检查何时抛出异常,在日志中验证了这一点.

I verified this in the logs by looking for the routes being booted ( by routeId ) and also checking when an exception is thrown.

另外,为了进一步确认,我采用了 http Connect Exception 处理路由,并将其直接放在 httpRouteBuilder 中,然后 lo..!, 异常处理现在可以很好地处理这个异常..

Additionally, to further confirm, I took the http Connect Exception handling route and put that directly in the httpRouteBuilder and lo..! , the exception handling now kicks in just fine for this exception..

我是否在这里遗漏了一些东西以使所有异常都能正常工作,同时在自己的类中很好地定义.?

Am I missing something here to get all exceptions to work while being nicely defined in its own class. ?

我使用的是 Apache Camel 2.9.0,但我也在 2.8.3 中验证了相同的行为.

I am using Apache Camel 2.9.0 , but I verified the same behavior also in 2.8.3.

谢谢,阿南

推荐答案

正确,onException() 子句仅适用于当前 RouteBuilder 的路由定义...

correct, the onException() clauses only apply to the current RouteBuilder's route definitions...

也就是说,您可以通过让所有的 RouteBuilder 扩展 ExceptionRouteBuilder 并调用 super.configure()...来重用这些定义

that said, you can reuse these definitions by having all your RouteBuilders extend the ExceptionRouteBuilder and call super.configure()...something like this

public class MyRouteBuilder extends ExceptionRouteBuilder {
    @Override
    public void configure() throws Exception {
        super.configure();
        from("direct:start").throwException(new Exception("error"));
    }
}
...
public class ExceptionRouteBuilder implements RouteBuilder {
    @Override
    public void configure() throws Exception {
        onException(Exception.class).handled(true).to("mock:error");
    }
}

或者甚至只是在 ExceptionBuilder 类中有一个静态方法来为给定的 RouteBuilder 实例设置子句

or even just have a static method in an ExceptionBuilder class to setup the clauses for a given RouteBuilder instance

public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        ExceptionBuilder.setup(this);
        from("direct:start").throwException(new Exception("error"));
    }
}
...
public class ExceptionBuilder {
    public static void setup(RouteBuilder routeBuilder) {
        routeBuilder.onException(Exception.class).handled(true).to("mock:error");
    }  
}

这篇关于如果在单独的类中定义了异常子句,则骆驼异常处理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆