对于这类问题没有一个有效的答案,因为它在很大程度上取决于您的应用程序。一般来说,拥有较小的路线可以很容易地测试您的应用程序和重用逻辑。
您可以重构这样
<camelContext>
<route>
<!-- route starts somehow -->
<choice>
<onWhen>
<simple>${property.name} == 'foo'</simple>
<to uri="direct:handleFoo" />
</onWhen>
<onWhen>
<simple>${property.name} == 'bar'</simple>
<to uri="direct:handleBar" />
</onWhen>
</choice>
</route>
<route id="ThisRouteWillHandleFooCase">
<from uri="direct:handleFoo" />
<to uri="..." />
<!-- do stuff for foo here -->
</route>
<route id="ThisOtherRouteIsForBarCase">
<from uri="direct:handleBar" />
<to uri="..." />
<!-- do stuff for bar here" -->
</route>
</camelContext>
的direct:
组件使得它像调用Java方法您的路线,这是其他途径直接和同步调用。现在您可以轻松测试foo的行为以及bar的行为。
现在想象一下,您需要更新数据库或经常进行Web服务调用:最好有一个单独的路由来完成这个工作并多次调用它。
非常感谢您的回答非常明确,但我想知道直接使用是否有效?它如何在后台工作?我想让我的代码容易进行单元测试,但我不直接使用,因为我不知道它的工作原理 –