第 4 章 配置
Camel Quarkus 会自动配置和部署 Camel Context bean,它默认根据 Quarkus 应用程序生命周期启动/停止。配置步骤在 Quarkus 的增强阶段进行,由 Camel Quarkus 扩展驱动,该扩展可以使用 Camel Quarkus 特定的 quarkus.camel prerequisites
属性进行调优。
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.properties
在 LogComponent
中设置特定的 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 jakarta.enterprise.context.ApplicationScoped; import jakarta.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 jakarta.enterprise.context.ApplicationScoped;
import jakarta.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"
参数。