Camel end vs endChoice - 不是通常的查询

时间:2015-09-03 17:49:28

标签: java apache-camel

首先,是的,我已经搜索了,是的,我已经阅读了每个人都指向的相同的Apache文档。 :-)我认为有一些混乱,我想我知道答案,所以让我列举一个我认为正确的例子,按照我认为的答案来做。谢谢。哦,我知道有些endChoice()行并不是绝对必要的,而且Camel会想出来,但是我喜欢干净地描述的块,除非有一些理由不使用它们。

.choice()
    .when(X1)
        // do stuff
        .choice()
            .when(Y)
                //do more stuff
            .endChoice()  // close inner when block
        .end() // close inner choice block
    .endChoice()  // close first outer when
    .when(X2)
        // do other stuff
    .endChoice()  // close second outer when
.end() // close outer choice

所以,我原来看看API,我认为end()用于关闭诸如choice和split之类的东西,而endChoice()用于关闭选择选项,比如when和otherwise。它看起来更像后者实际上是一个返回ChoiceDefinition的end()。这使名称变得更好。

但是,如果我取出标有'close inner choice block'的end(),这意味着我继续下一行,即endChoice()。这会关闭内部选择块吗?鉴于此,when(X2)仍然在when(X1)块内。所以,我认为我需要用endChoice()替换end()而不是删除它。结果如下:

.choice()
    .when(X1)
        // do stuff
        .choice()
            .when(Y)
                //do more stuff
            .endChoice()  // close inner when block
        .endChoice() // close inner choice block
    .endChoice()  // close first outer when
    .when(X2)
        // do other stuff
    .endChoice()  // close second outer when
.end() // close outer choice

这是在Camel中处理这个问题的方法吗?或者有一种更简单的方法,我只是缺少?谢谢你的时间。

2 个答案:

答案 0 :(得分:18)

简短回答: 我会打电话给自己,所以没有其他人必须这样做,答案是你做错了,不应该有嵌套的选择。

LONG ANSWER: 我继承了一个复杂的路线建设者,并试图清理它以使其更清晰。但是直截了当并且放入end()或endChoice()只会破坏事物。并且,是的,上述修复仍然破坏了一些事情。我不明白骆驼怎么知道要去哪个街区。研究并试图找到嵌套的好例子最终推动了Camel 真正为嵌套选择而设计的事实。它允许它,但由于Java的限制,它做得不好。所以我尝试删除我的嵌套选择。虽然这是可能的,但这意味着丑陋的冗余条件,例如:

choice()
  .when(x and a)
    //do stuff xa
  .when(x not a)
    // do other x stuff
  .when(y and a)
    // do y stuff

只有我的至少还有另一个级别。进一步思考和回忆我读过的东西带来了第二点启蒙。骆驼的重点是指挥路线。块的每个选择应该只是将进程指向路由。它不应该是思考,处理或任何东西。最后,我们的小组将重构以将路由构建器中的大部分逻辑移除到bean。我们将努力实现的设计将是简单的:

   from(uri)
     .bean(class, method)  // do any processing
     .choice()
       .when(header("result").isEqualTo("A")
          .to(routeA)
       .endChoice()
       .when(header("result").isEqualTo("B")
          .to(routeB)
       .endChoice()
       .when(header("result").isEqualTo("C")
          .to(route)
       .endChoice()
      .end()

我的建议是避免嵌套选择。特别复杂的。您可能会让它工作,但是当您必须稍后进行更改时,您将无法信任它。如果您发现自己想要使用嵌套选项,请检查您要完成的操作并确定它是否真的属于路径构建器。

答案 1 :(得分:0)

来晚了,但可能会有所帮助。

嵌套选择定义与Camel一起使用时效果很好。只有终止符是错误的:

  • .endChoice()->关闭“何时”谓词
  • .end()->关闭整个“选择”块

我知道,语法有点混乱。

所以在您的情况下:

.choice()
    .when(X1)
        // do stuff

        .choice()
            .when(Y)
                //do more stuff
            .endChoice() // close Y condition

        .end() // close inner choice block

    .endChoice() // close X1 condition

    .when(X2)
        // do other stuff
    .endChoice() // close X2 condition

    .otherwise()
        // default case
    .endChoice() // close default condition

.end()

实际上,您不必关闭所有when谓词,仅需要关闭多个子路由。以我的拙见,对缩进一丝不苟的做法很有帮助。