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

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

问题描述

我想建立几个骆驼的路线从而重新在内部使用许多常见的路由的应用程序。
因此,我试图隔离在几个不同的路线生成器的类路径,然后连接在需要的路由。

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类。
我想这应该是正常的,因为骆驼犯规没有阶级区分,只查找路线defininition。

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.

我也通过定义骆驼背景春季像这样连接所有不同种类的一起 -

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连接异常处理路径,并将该直接在httpRouteBuilder瞧..! ,异常处理,现在踢就好了此异常。

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的骆驼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...

这是说,你可以通过让所有RouteBuilders延长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天全站免登陆