262.27. 使用自定义功能


可从 Camel 2.14.1 开始

Properties 组件允许插件第三方功能,在解析属性占位符时使用。然后,这些函数能够执行自定义逻辑来解析占位符,如在数据库中查找、执行自定义计算或不用。函数的名称成为占位符中使用的前缀。这在以下示例代码中给出了最佳说明

<bean id="beerFunction" class="MyBeerFunction"/>

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
  <propertyPlaceholder id="properties">
    <propertiesFunction ref="beerFunction"/>
  </propertyPlaceholder>

  <route>
    <from uri="direct:start"/>
    <to uri="{`{beer:FOO}`}"/>
    <to uri="{`{beer:BAR}`}"/>
  </route>
</camelContext>
Copy to Clipboard Toggle word wrap
注意

来自 camel 2.19.0,位置属性(on propertyPlaceholder)并不是更强制的

在这里,我们有一个 Camel XML 路由,其中定义了 & lt;propertyPlaceholder > 以使用自定义功能,我们称为 bean id - 也就是 beerFunction。由于er 函数使用 "beer" 作为其名称,因此占位符语法可以通过以 beer:value 开始来触发 beer 函数。

这个功能的实现只有两种方法,如下所示:

public static final class MyBeerFunction implements PropertiesFunction {

    @Override
    public String getName() {
        return "beer";
    }

    @Override
    public String apply(String remainder) {
        return "mock:" + remainder.toLowerCase();
    }
}
Copy to Clipboard Toggle word wrap

该函数必须实施 org.apache.camel.component.properties.PropertiesFunction 接口。method getName 是函数的名称,eg beer。应用 方法是我们实施自定义逻辑。由于示例代码来自单元测试,它只是返回一个值以引用模拟端点。

要从 Java 代码中注册自定义功能,如下所示:

PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
pc.addFunction(new MyBeerFunction());
Copy to Clipboard Toggle word wrap

 

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat