使用在蓝图中定义但在 Camel Java DSL 中解析的类型化默认 属性 占位符

Using Typed Default Property Placeholders defined in blueprint but resolved in Camel Java DSL

我在 Karaf 中使用蓝图 bootstrap 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}}")

这是一个有点人为的例子,但我在这里想做的是将 header 设置为 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");

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

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

 @PropertyInject("myPropertyKey")
 private int myValue;

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

但 Camel 通常能够在需要时从一种类型转换为另一种类型,例如,在那个 header 示例中,您可以将其设置为字符串值,这没问题。

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

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