第 4 章 配置


Camel Quarkus 会自动配置和部署 Camel Context bean,默认情况下根据 Quarkus 应用程序生命周期启动/停止。配置步骤在 Quarkus 的增强阶段进行,它由 Camel Quarkus 扩展驱动,该扩展可使用 Camel Quarkus 特定的 quarkus.camel114 属性进行调整。

注意

quarkus.camel prerequisites 配置属性记录在单独的扩展页面中 - 例如,请参阅 Camel Quarkus Core

完成配置后,会在 RUNTIME_INIT 阶段编译并启动最小的 Camel Runtime。

4.1. 配置 Camel 组件

4.1.1. application.properties

要通过属性配置 Apache Camel 的组件和其他方面,请确保您的应用程序直接或传输地依赖于 camel-quarkus-core。因为大多数 Camel Quarkus 扩展依赖于 camel-quarkus-core,因此您通常不需要显式添加它。

camel-quarkus-core 从 Camel Main 引入到 Camel Quarkus 的功能。

在以下示例中,您可以通过 application.propertiesLogComponent 中设置特定的 ExchangeFormatter 配置:

camel.component.log.exchange-formatter = #class:org.apache.camel.support.processor.DefaultExchangeFormatter
camel.component.log.exchange-formatter.show-exchange-pattern = false
camel.component.log.exchange-formatter.show-body-type = false

4.1.2. CDI

您还可以使用 CDI 以编程方式配置组件。

推荐的方法是观察 ComponentAddEvent 并在路由和 CamelContext 启动前配置组件:

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import org.apache.camel.quarkus.core.events.ComponentAddEvent;
import org.apache.camel.component.log.LogComponent;
import org.apache.camel.support.processor.DefaultExchangeFormatter;

@ApplicationScoped
public static class EventHandler {
    public void onComponentAdd(@Observes ComponentAddEvent event) {
        if (event.getComponent() instanceof LogComponent) {
            /* Perform some custom configuration of the component */
            LogComponent logComponent = ((LogComponent) event.getComponent());
            DefaultExchangeFormatter formatter = new DefaultExchangeFormatter();
            formatter.setShowExchangePattern(false);
            formatter.setShowBodyType(false);
            logComponent.setExchangeFormatter(formatter);
        }
    }
}

4.1.2.1. 生成 @Named 组件实例

或者,您也可以使用 @Named producer 方法创建和配置组件。这可以正常工作,因为 Camel 使用组件 URI 方案从其 registry 中查找组件。例如,如果 LogComponent Camel 查找名为 bean 的日志

警告

请注意,虽然生成 @Named 组件 Bean 通常可以正常工作,但可能会导致某些组件出现微小的问题。

Camel Quarkus 扩展可以执行以下操作之一或多个:

  • 传递默认 Camel 组件类型的自定义子类型。请参阅 Vert.x WebSocket 扩展 示例。
  • 对组件执行一些特定于 Quarkus 的自定义。请参阅 JPA 扩展 示例。

当您生成自己的组件实例时,不会执行这些操作,因此建议在观察器方法中配置组件是推荐的方法。

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import org.apache.camel.component.log.LogComponent;
import org.apache.camel.support.processor.DefaultExchangeFormatter;

@ApplicationScoped
public class Configurations {
    /**
     * Produces a {@link LogComponent} instance with a custom exchange formatter set-up.
     */
    @Named("log") 1
    LogComponent log() {
        DefaultExchangeFormatter formatter = new DefaultExchangeFormatter();
        formatter.setShowExchangePattern(false);
        formatter.setShowBodyType(false);

        LogComponent component = new LogComponent();
        component.setExchangeFormatter(formatter);

        return component;
    }
}
1
如果方法的名称相同,可以省略 @Named 注释的 "log" 参数。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.