【问题标题】:camel choice based on actual route根据实际路线选​​择骆驼
【发布时间】:2015-05-28 13:05:03
【问题描述】:

我有两条单独的路线。两条路线基本相同。不同的是源文件夹和目标文件夹。如果发生错误,两条路由都会抛出相同的异常。这我在一个 onException 块中捕获。 在此,我想根据实际路线将文件写入文件夹。所以我想基于 routeId 构建一个 choiche()....when(...) 。问题是,我怎样才能让 routeId 在何时使用它。 下面显示了一些我认为它如何工作的代码。但不是。也许有人有一个想法。

           onException(SomeValidationException.class)
                        .handled(true)
                        .useOriginalMessage()
                        .choice()
                        .when(exchangeProperty("routeId").convertToString().isEqualTo("Route1"))
                        .to("file:data/error/Route1")
                        .when(exchangeProperty("routeId").convertToString().isEqualTo("Route2"))
                        .to("file:data/error/Route2")
                        .otherwise()
                        .log(LoggingLevel.ERROR, "I always get here")
                        .end();

                from("file:data/in/Route1")
                        .routeId("Route1")
                        .routePolicy(getRoutingPolicy())
                        .to("direct:RouteWhichWillThrowException")
                        .to("file:data/out/Route1");

                from("file:data/in/Route2")
                        .routeId("Route2")
                        .routePolicy(getRoutingPolicy())
                        .to("direct:RouteWhichWillThrowException")
                        .to("file:data/out/Route2");

【问题讨论】:

    标签: routing apache-camel choice


    【解决方案1】:

    我认为您的用例实际上最好使用如下的路由错误处理程序:

    from("file:data/in/Route1").routeId("Route1")
        .onException(SomeValidationException.class)
            .handled(true)
            .useOriginalMessage()
            .to("file:data/error/Route1")
            .log(LoggingLevel.ERROR, "I always get here")
         .end()
        .routePolicy(getRoutingPolicy())
        .to("direct:RouteWhichWillThrowException")
        .to("file:data/out/Route1");
    
    from("file:data/in/Route2").routeId("Route2")
        .onException(SomeValidationException.class)
            .handled(true)
            .useOriginalMessage()
            .to("file:data/error/Route2")
            .log(LoggingLevel.ERROR, "I always get here")
         .end()
         .routePolicy(getRoutingPolicy())
         .to("direct:RouteWhichWillThrowException")
         .to("file:data/out/Route2");
    

    然而,虽然这在重用方面是可行的,但如果您的实现除了 inputURI、outputURI 和 errorURI 之外总是相同的,那么这种情况就很糟糕了。我建议创建一个模板路由,然后创建它的新实例。

    class MyCustomRouteBuilder extends RouteBuilder {
        private String myRouteId;
        private String inputURI;
        private String outputURI;
    
        @Override
        public void configure() {
            from(inputURI).routeId(myRouteId)
                .onException(SomeValidationException.class)
                    .handled(true)
                    .useOriginalMessage()
                    .to(errorURI)
                    .log(LoggingLevel.ERROR, "I always get here")
                 .end()
                 .routePolicy(getRoutingPolicy())
                 .to("direct:RouteWhichWillThrowException")
                 .to(outputURI);
        }
    }
    
    //different route builder configure
    MyCustomRouteBuilder route1 = new MyCustomRouteBuilder();
    route1.setRouteId("");
    route1.setInputURI("");
    route1.setOutputURI("");
    route1.setErrorURI("");
    context.addRoutes(route1);
    

    【讨论】:

    • 啊,最简单的答案就是最好的。我已经在额外的课程中获得了路线:/非常感谢。这有帮助
    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多