骆驼 - 使用 end()

Camel - using end()

对每条路线使用 end() 是最佳做法吗?

以下作品:

from("jms:some-queue")      
    .beanRef("bean1", "method1")
    .beanRef("bean2", "method2")

这个也是,

from("jms:some-queue")      
    .beanRef("bean1", "method1")
    .beanRef("bean2", "method2")
    .end()

当你想结束正在运行的特定路由时,你必须使用 end() 。在 onCompletion

的例子中可以得到最好的解释
from("direct:start")
.onCompletion()
    // this route is only invoked when the original route is complete as a kind
    // of completion callback
    .to("log:sync")
    .to("mock:sync")
// must use end to denote the end of the onCompletion route
.end()
// here the original route contiues
.process(new MyProcessor())
.to("mock:result");

这里必须结束,表示onCompletion相关的操作已经完成,要继续原来的死记硬背操作。

如果您使用 XML DSL 而不是 java,这将变得更加清晰易懂。因为在此您不必使用结束标记。 XML 的结束标记将负责编写 end()。下面是用 XML DSL

编写的完全相同的示例
<route>
<from uri="direct:start"/>
<!-- this onCompletion block will only be executed when the exchange is done being routed -->
<!-- this callback is always triggered even if the exchange failed -->
<onCompletion>
    <!-- so this is a kinda like an after completion callback -->
    <to uri="log:sync"/>
    <to uri="mock:sync"/>
</onCompletion>
<process ref="myProcessor"/>
<to uri="mock:result"/>

不! 调用 end() 到 "end" 骆驼路线 不是最佳做法 并且不会'不会产生任何功能性好处。

对于常见的 ProcessorDefinition 函数,如 to()bean()log(),它只会导致对 endParent() 方法的调用,从 Camel 源代码中可以看出, 做的很少:

public ProcessorDefinition<?> endParent() { return this; }

调用 end() 是必需的,一旦你调用了开始自己的块的处理器定义,最显着的包括 TryDefinitions aka doTry()ChoiceDefinitions aka choice(),但也有众所周知的函数,例如 split(), loadBalance(), onCompletion()recipientList().