【问题标题】:What is the best way to design routes? Is it good to have multiple routes?设计路线的最佳方式是什么?有多条路线好不好?
【发布时间】:2017-05-25 10:33:56
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: routes apache-camel jbossfuse


    【解决方案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 服务调用:最好有一条路线来完成这项工作并多次调用它。

    【讨论】:

    • 非常感谢您的回答非常清楚,但我想知道使用直接是否有效?它是如何在后台工作的?我想让我的代码易于单元测试,但我避免使用直接,因为我不知道它是如何工作的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多