【问题标题】:Using Typed Default Property Placeholders defined in blueprint but resolved in Camel Java DSL使用在蓝图中定义但在 Camel Java DSL 中解决的类型化默认属性占位符
【发布时间】:2019-12-20 23:48:24
【问题描述】:

我在 Karaf 中使用蓝图来引导 CamelContext 的启动,这反过来又配置了 Java DSL(Camel 版本 2.21.2)中定义的路由。在 blueprint.xml 中,我定义了一些默认属性占位符来利用配置管理 OSGi 服务:

<cm:property-placeholder persistent-id="foo.MyRoute">
    <cm:default-properties>
        <cm:property name="log.message" value="Hello world"/>
        <cm:property name="response.code" value="200"/>
    </cm:default-properties>
</cm:property-placeholder>

在 Java DSL 中,我可以使用 {{log.message}} 占位符,因为它是一个字符串:

from("timer:timer").log("{{log.message}}")

但是,我想弄清楚的是如何设置占位符的类型,以便在以下情况下使用它们:

.setHeader(Exchange.HTTP_RESPONSE_CODE).constant("{{response.code}}")

这是一个人为的例子,但我在这里尝试将标头设置为 int/Integer 类型。

占位符是否专门用于端点 URI 定义?或者我应该将解析的字符串转换为我需要的类型?我假设我在这里遗漏了一些东西,或者没有按预期使用占位符......

我已阅读 https://camel.apache.org/manual/latest/using-propertyplaceholder.html,尤其是后面的部分:在 XML DSL 中为任何类型的属性使用属性占位符,我看到支持自动类型转换,但它似乎只适用于 EIP 选项:

from("direct:start")
    .multicast()
    .placeholder("stopOnException", "stop")
    .to("mock:a")
    .throwException(new IllegalAccessException("Damn"))
    .to("mock:b");

任何帮助都将不胜感激,因为我经常阅读文档,现在这些词已经不再有意义了!

【问题讨论】:

    标签: apache-camel blueprint-osgi


    【解决方案1】:

    您可以使用@PropertyInject 在路由构建器类中的字段上注入属性,该字段可以是类型:

     @PropertyInject("myPropertyKey")
     private int myValue;
    

    然后你可以通过常量在你的路由中使用该字段。

    但是 Camel 通常能够在需要时从一种类型转换为另一种类型,例如,在该标头示例中,您可以将其设置为字符串值,这没关系。

    你也可以在可以指定类型的地方使用simple

    .setHeader("foo", simple("{{foo}}, int.class))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2020-04-12
      • 2018-10-25
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      相关资源
      最近更新 更多