Routes 设计路线的最佳方式是什么?有多条路线好吗?

Routes 设计路线的最佳方式是什么?有多条路线好吗?,routes,apache-camel,jbossfuse,Routes,Apache Camel,Jbossfuse,我正在使用JBoss Fuse,我不知道我应该有多条路由还是一条路由。假设我们有2个条件,我们将根据条件执行不同的操作。比如说 <camelContext> <route> <choice> <onWhen> <simple>${property.name} == 'foo'</simple> ....do something </onWhen> <onWhen> <simple>${pr

我正在使用JBoss Fuse,我不知道我应该有多条路由还是一条路由。假设我们有2个条件,我们将根据条件执行不同的操作。比如说

<camelContext>
<route>
<choice>
<onWhen>
<simple>${property.name} == 'foo'</simple>
....do something
</onWhen>
<onWhen>
<simple>${property.name} == 'bar'</simple>
...do something
</onWhen>
</route>
</camelContext>

${property.name}=='foo'
……做点什么
${property.name}=='bar'
…做点什么

这类问题没有一个单一的有效答案,因为它在很大程度上取决于您的应用程序。一般来说,使用较小的路由可以很容易地测试应用程序和重用逻辑

你可以像这样重构你的路线

<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>

${property.name}=='foo'
${property.name}=='bar'
direct:
组件使其类似于调用Java方法,它是对另一个路由的直接同步调用。现在,您可以轻松地测试foobar的行为


现在想象一下,您需要经常更新数据库或拨打web服务电话:最好使用一条路线来完成这项工作并多次呼叫。

非常感谢您的回答非常清楚,但我想知道使用direct是否有效?它在后台是如何工作的?我想使我的代码易于进行单元测试,但我避免使用direct,因为我不知道它到底是如何工作的