作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以将参数传递给 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/
我是一名优秀的程序员,十分优秀!