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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32382322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:07:00  来源:igfitidea点击:

Camel end vs endChoice - not the usual query

javaapache-camel

提问by Scott Law

First, yes, I have searched and, yes, I have read the same Apache document every one points to. :-) I think there is a bit of confusion and I think I know an answer, so let me lay out an example of what I thought was correct, follow it with what I think the answer is. Thanks. Oh, and I do know that some of the endChoice() lines are not strictly necessary and that Camel will figure it out, but I like the blocks to be cleanly delineated, unless there is some reason not to use them.

首先,是的,我已经搜索过,是的,我已经阅读了每个人指向的同一个 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

So, my original look at the API, I thought that end() was for closing things like choice and split and that endChoice() was for closing choice options like when and otherwise. It looks more like the latter is actually an end() that returns a ChoiceDefinition. Which makes the name a little better.

所以,我最初对 API 的看法是,我认为 end() 用于关闭诸如选择和拆分之类的内容,而 endChoice() 用于关闭诸如何时和否则之类的选择选项。后者看起来更像是一个返回 ChoiceDefinition 的 end()。这使名称更好一点。

But, if I take out the end() labeled 'close inner choice block', this means I carry on to the next line, an endChoice(). Does this then close the inner choice block? Given that, the when(X2) is still within the the when(X1) block. So, I think that I need to replace the end() with an endChoice() rather than removing it. So the result would look like:

但是,如果我取出标记为“关闭内部选择块”的 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

So is this the way to handle this in Camel? Or is there a simpler way that I am just missing? Thanks for your time.

那么这是在骆驼中处理这个的方式吗?或者有没有更简单的方法我只是想念?谢谢你的时间。

回答by Scott Law

SHORT ANSWER:I will call myself on this so no one else has to, the answer is that you are doing it wrong and should not have nested choices.

简短回答:我会在这一点上自称,所以没有其他人必须这样做,答案是你做错了,不应该有嵌套的选择。

LONG ANSWER:I inherited a complicated route builder and was trying to clean it up to make it clearer. But straightening and putting in either end() or endChoice() just broke things. And, yes, the above fix still broke things. I did not understand how Camel knew which block to go to. Research and trying to find good examples of nesting eventually drove home the fact that Camel is notreally designed for nesting choices. It allows it, but due to limitations in Java, it does not do it well. So I tried removing my nested choices. While this would have been possible, it would have meant ugly redundant conditionals, like:

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

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

Only mine would have had at least another level. Further thought and recalling things that I had read brought about the second bit of enlightenment. The whole point of Camel is directing routes. Each choice's when block should just be pointing the process to a route. It should not be thinking, processing, or anything. In the end, our group is going to be refactoring to remove most of the logic from the route builder to a bean. The design we will be working towards will be something simple:

只有我的至少会有另一个级别。进一步的思考和回忆我读过的东西带来了第二点启蒙。骆驼的全部意义在于指导路线。每个选择的 when 块应该只是将过程指向一条路线。它不应该是思考、处理或任何事情。最后,我们的团队将进行重构,将大部分逻辑从路由构建器移除到 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()

My advice to you is to avoid nesting choices. Particularly complicated ones. You might get it to work, but you will not be able to trust it when you have to make changes later. If you find yourself tempted to use nested choices, examine what you are trying to accomplish and decide if it really belongs in a route builder.

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