【发布时间】: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