gpt4 book ai didi

java - Camel Java DSL Route with Choice 仅针对最后一个条件进行

转载 作者:行者123 更新时间:2023-11-30 07:02:34 25 4
gpt4 key购买 nike

我有很多选择,但该路线仅适用于最后一个条件。对于其他情况,路线被卡住,不会继续前进。

public class CamelChoiceTest {

private CamelContext context;

@Before
public void initializeContext() throws Exception {

RouteBuilder builder = new RouteBuilder() {
public void configure() {

from("direct:test")
.choice()
.when(header("number").isEqualTo("one")).to("direct:one")
.when(header("number").isEqualTo("two")).to("direct:two")
.when(header("number").isEqualTo("three")).to("direct:three")
.endChoice()
.log("only final condition reaches here");

from("direct:one").log("one is selected");
from("direct:two").log("two is selected");
from("direct:three").log("three is selected");
}
};

context = new DefaultCamelContext();
context.addRoutes(builder);
context.setTracing(true);
context.start();
}

private void send(String header){
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setHeader("number", header);
exchange.getIn().setBody("test", String.class);
ProducerTemplate producerTemplate = context.createProducerTemplate();
// Send the request
producerTemplate.send("direct:test", exchange);
}

@Test
public void testOne() throws Exception {
send("one");
}

@Test
public void testTwo() throws Exception {
send("two");
}

@Test
public void testThree() throws Exception {
send("three");
}
}

执行的时候会打印日志“only final condition reaches here”。当条件也重新排序时,它正在打印最后一个条件。

我认为这是 Java DSL 的问题。当我在 XML 中创建相同的内容时,它工作正常,

<camel:camelContext id="testCamelContext" trace="true"
streamCache="true">
<camel:route>
<camel:from uri="direct:test" />
<camel:choice>
<camel:when>
<camel:simple>${header.number} == 'one'</camel:simple>
<camel:to uri="direct:one" />
</camel:when>
<camel:when>
<camel:simple>${header.number} == 'two'</camel:simple>
<camel:to uri="direct:two" />
</camel:when>
<camel:when>
<camel:simple>${header.number} == 'three'</camel:simple>
<camel:to uri="direct:three" />
</camel:when>
</camel:choice>
<camel:to uri="bean:routeBean?method=receive" />
</camel:route>
</camel:camelContext>

最佳答案

在您的示例中,when 条件似乎评估正确,但缺少测试“one”和“two”的最终日志语句。

使用 .end() 代替 .endCoice():

  • 使用 .endChoice() 以“返回”到基于内容的路由器,即使用 .endChoice() 结束 when 条件如果代码块不是简单的语句,参见here有关此问题的更多信息。
  • 使用.end() 来结束整个choice block 。

关于java - Camel Java DSL Route with Choice 仅针对最后一个条件进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com