对每条路由使用end()是最佳实践吗?
以下是工作原理:
from("jms:some-queue")
.beanRef("bean1", "method1")
.beanRef("bean2", "method2")这也是,
from("jms:some-queue")
.beanRef("bean1", "method1")
.beanRef("bean2", "method2")
.end()发布于 2015-11-19 20:39:29
否!调用end()来“结束”一个驼峰路由不是一个最佳实践,并且不会产生任何功能上的好处。
对于常见的ProcessorDefinition函数,如to()、bean()或log(),它只是简单地调用endParent()方法,从Camel源代码中可以看出,它做的事情很少:
public ProcessorDefinition<?> endParent() { return this; }
一旦您调用了开始自己的块的处理器定义,并且最突出的包括TryDefinitions (又称doTry() )和ChoiceDefinitions (又称choice() ),但也有像split(), loadBalance(), onCompletion()或recipientList()这样的众所周知的函数,就需要调用end()。
发布于 2015-11-19 18:42:01
当您想要结束正在运行的特定路由时,必须使用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");您必须在此处添加end,以指示与onCompletion相关的操作已完成,并且您正在恢复原始rote上的操作。
如果您使用的是XML而不是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"/>
https://stackoverflow.com/questions/33800993
复制相似问题