60.9. Camel 事件到 CDI 事件


可从 Camel 2.17 开始

Camel 提供一组 管理事件 ,可订阅侦听 Camel 上下文、服务、路由和交换事件。Camel CDI 将这些 Camel 事件无缝转换为 CDI 事件,这些事件可以通过 CDI 监控器方法,例如:

void onContextStarting(@Observes CamelContextStartingEvent event) {
    // Called before the default Camel context is about to start
}
Copy to Clipboard Toggle word wrap

从 Camel 2.18 开始,可以观察特定路由的事件(RouteAddedEventRouteStartedEventRouteStoppedEventRouteRemovedEvent)应该有一个显式定义,例如:

from("...").routeId("foo").to("...");

void onRouteStarted(@Observes @Named("foo") RouteStartedEvent event) {
    // Called after the route "foo" has started
}
Copy to Clipboard Toggle word wrap

当 CDI 容器中存在多个 Camel 上下文时,Camel 上下文 belifiers (如 @ContextName )可用于优化观察器方法解析到特定 Camel 上下文,例如: http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#observer_resolution

void onRouteStarted(@Observes @ContextName("foo") RouteStartedEvent event) {
    // Called after the route 'event.getRoute()' for the Camel context 'foo' has started
}
 
void onContextStarted(@Observes @Manual CamelContextStartedEvent event) {
    // Called after the Camel context qualified with '@Manual' has started
}
Copy to Clipboard Toggle word wrap

同样,如果存在多个上下文,可以使用 @Default qualifier 观察 Camel 上下文的 Camel 事件,例如: 

void onExchangeCompleted(@Observes @Default ExchangeCompletedEvent event) {
    // Called after the exchange 'event.getExchange()' processing has completed
}
Copy to Clipboard Toggle word wrap

在该示例中,如果没有指定限定符,则会隐式假设 @Any qualifier,以便接收所有 Camel 上下文的对应事件。

请注意,仅在部署中检测到监听 Camel 事件的观察方法,并且每个 Camel 上下文中检测到对 Camel 事件的支持才会激活。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat