骆驼 - 使用结束()

时间:2015-11-19 10:17:31

标签: java spring apache-camel

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

以下作品:

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

等等,

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

2 个答案:

答案 0 :(得分:13)

不!致电end()以“结束”骆驼路线不是最佳做法,不会产生任何功能上的好处。

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

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

调用end()是必需的,一旦调用了开始自己的块的处理器定义,并且最突出的包括TryDefinitions又名doTry()ChoiceDefinitions又名choice() ,但也熟知split(), loadBalance(), onCompletion()recipientList()等功能。

答案 1 :(得分:6)

如果要结束正在运行的特定路线,则必须使用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"/>