gpt4 book ai didi

apache-camel - 如何将参数传递给 Camel 路线?

转载 作者:行者123 更新时间:2023-12-04 14:23:48 24 4
gpt4 key购买 nike

是否可以将参数传递给 Camel 路由?例如,在下一个代码片段中:

public class MyRoute extends RouteBuilder {
public void configure() throws Exception {
from("direct:start")
.to("cxf:bean:inventoryEndpoint?dataFormat=PAYLOAD");
}
}

dataFormat 的值在硬代码中,但是,如果我想动态设置它怎么办?从调用路由的代码中传递一个值。我知道这可以添加构造函数并在其中传递参数,如下所示:

public class MyRoute extends RouteBuilder {

private String type;

public MyRoute(String type){
this.type = type;
}

public void configure() throws Exception {
from("direct:start")
.to("cxf:bean:inventoryEndpoint?dataFormat=" + type);
}
}

还有别的办法吗?

非常感谢!

最佳答案

正如您所提到的,如果参数从 Camel 的角度来看是静态的,您可以使用构造函数(或 setter 或任何其他 Java/Framework 工具)。

这些参数在应用程序中是可配置的,但在应用程序启动后它们不再改变。因此,Camel 路由处理的每条消息都使用相同的值。

相反,当参数是动态的——即它们可以针对每条处理过的消息而改变时,您可以使用 Camel 的动态端点 toD()。这些端点地址可以包含在运行时计算的表达式。例如路线

from("direct:start")
.toD("${header.foo}");

将消息发送到动态端点并从名为 foo 的消息 header 中获取值。或者使用你的例子

.toD("cxf:bean:inventoryEndpoint?dataFormat=${header.dataFormat}");

这样您就可以通过标题为每条消息单独设置数据格式。

您可以在 this Camel documentation page 上找到有关动态端点的更多信息

关于apache-camel - 如何将参数传递给 Camel 路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50053467/

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