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

标签 apache-camel

是否可以将参数传递给 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/

相关文章:

osgi - Fabric8 或 JbossFuse 中实例的自动启动

apache-camel - Camel 工作单元

java - Camel netty组件: Failed to create selector

spring-boot - Camel JUnit 测试 : wrong results with parallel execution

java - Spring Boot + Apache Camel + Freemarker 自定义模板加载器

java - 使用 Camel 从数据库读取并写入文件

java - apache Camel Web 服务客户端

java - 如何使用camel-jetty组件上传图片

reflection - Camel 在aggregationStrategy处访问prevRouteContext

caching - 将拦截器添加到 Camel 路线