【问题标题】:Camel's endChoice(): What's the point? (not the difference from end())Camel 的 endChoice():有什么意义? (不是与 end() 的区别)
【发布时间】:2021-09-15 14:11:48
【问题描述】:

我在这里找到了 2 个问题,询问 Camel 的 .end() 和 .endChoice() 之间的区别。这对我来说不是问题。我很清楚 .end() 用于完成 .choice() 块,而 .endChoice() 用于完成 .when() 块。

但是,我的问题是……毕竟,使用 .endChoice()s 有什么意义?只是为了让代码更清晰吗?

看看这些例子(请不要指出它的正确性或可用性,我的意思是理解机制,而不是要求干净的代码模型):

代码示例 1:

from("file:files/input")
            .choice()
                .when(simple("${file:ext} ends with 'xml'"))
                    .log("LOG 1")
                    .choice()
                        .when(simple("${file:ext} ends with 'xml'"))
                            .log("LOG 1 nested")
                        .endChoice()
                        .when(simple("${file:ext} ends with 'xml'"))
                            .log("LOG 2 nested")
                        .endChoice()
                .endChoice()
                .when(simple("${file:ext} ends with 'xml'"))
                    .log("LOG 2")
                .endChoice()
                .otherwise()
                    .log("NOT AN XML FILE")
            .end().to("file:files/output");

代码示例 2:

from("file:files/input")
            .choice()
                .when(simple("${file:ext} ends with 'xml'"))
                    .log("LOG 1")
                    .choice()
                        .when(simple("${file:ext} ends with 'xml'"))
                            .log("LOG 1 nested")
                        .when(simple("${file:ext} ends with 'xml'"))
                            .log("LOG 2 nested")
                .when(simple("${file:ext} ends with 'xml'"))
                    .log("LOG 2")
                .otherwise()
                    .log("NOT AN XML FILE")
            .end().to("file:files/output");

我测试了将 xml 文件放在文件夹中,两个代码的作用完全相同。不管你是否使用 .endChoice()s,.when()s 总是表现得像“elseif”语句,也就是说,响应是“LOG 1”和“LOG1 nested”,“LOG2”和“LOG 2 nested” “从不打印。如果文件不是xml,则按预期触发否则路由。

希望我的要求很清楚。最好的问候。

【问题讨论】:

    标签: java apache-camel spring-camel


    【解决方案1】:

    .endChoice 只是一种解决有关 Camel DSL 限制的方法

    您的示例运行良好,因为您的 when 块的内容非常简单。如果您将更复杂的 EIP 放入其中,您可能会遇到没有.endChoice 时您的代码无法编译的情况。

    this example on the Camel website有这样的案例。

    我也遇到过即使.endChoice 也无济于事的罕见情况。但通常在这种情况下,路由无论如何都太复杂了,应该拆分

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 2014-08-12
      • 2017-08-31
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多