第 4 章 配置
Camel Quarkus 自动配置和部署 Camel 上下文 bean,默认情况下,根据 Quarkus 应用程序生命周期启动/停止。在 Quarkus 的扩展阶段,配置步骤会在构建期间发生,并由 Camel Quarkus 扩展(可以使用 Camel Quarkus 特定的 quarkus.camel.* 属性进行调优)。
Quarkus .camel.* 配置属性记录在单独的扩展页面中,例如,请参阅 Camel Quarkus Core。
完成配置后,会在 RUNTIME_INIT 阶段编译并启动最小的 Camel 运行时。
4.1. 配置 Camel 组件 复制链接链接已复制到粘贴板!
4.1.1. application.properties 复制链接链接已复制到粘贴板!
要通过属性配置 Apache Camel 的组件和其他方面,请确保您的应用程序直接或传输依赖于 camel-quarkus-core。因为大多数 Camel Quarkus 扩展依赖于 camel-quarkus-core,因此通常不需要显式添加它。
Camel -quarkus-core 将 Camel 主流功能引入到 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 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 扩展 示例。
在您生成自己的组件实例时,不建议在 observer 方法中配置组件,因此不建议在 watchr 方法中配置这些组件。
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")
LogComponent log() {
DefaultExchangeFormatter formatter = new DefaultExchangeFormatter();
formatter.setShowExchangePattern(false);
formatter.setShowBodyType(false);
LogComponent component = new LogComponent();
component.setExchangeFormatter(formatter);
return component;
}
}
- 1
- 如果方法的名称相同,则可以省略
@Named注释的"log"参数。