2017-05-25 79 views
0

我正在使用JBoss Fuse,如果我有多个路由或单个路由,我很困惑。假设我们有两个条件,我们将根据条件执行不同的操作。例如什么是设计路线的最佳途径?有多条路线是否好?

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

回答

1

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

您可以重构这样

<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服务调用:最好有一个单独的路由来完成这个工作并多次调用它。

+0

非常感谢您的回答非常明确,但我想知道直接使用是否有效?它如何在后台工作?我想让我的代码容易进行单元测试,但我不直接使用,因为我不知道它的工作原理 –