128.5. DeadEvent 注意事项

请记住,由于 Guava EventBus 设计的限制,您无法指定监听器接收的事件类,而无需创建 @Subscribe 方法标注的类。这个限制意味着,带有 eventClass 选项的端点实际侦听所有可能的事件(java.lang.Object),并在运行时过滤相应的消息。以下 snipped 演示了 Camel 代码库中的相应摘录。

@Subscribe
public void eventReceived(Object event) {
  if (eventClass == null || eventClass.isAssignableFrom(event.getClass())) {
    doEventReceived(event);
...

这个方法的缺点是 Camel 使用的 EventBus 实例永远不会生成 com.google.common.eventbus.DeadEvent 通知。如果您希望 Camel 仅侦听精确指定的事件(因此启用 DeadEvent 支持),请使用 listenerInterface 端点选项。Camel 将通过您在通过后一选项指定的接口创建动态代理,仅侦听由接口处理程序方法指定的消息。以下演示了带有单一方法处理仅 SpecificEvent 实例的监听程序接口示例。

package com.example;

public interface CustomListener {

  @Subscribe
  void eventReceived(SpecificEvent event);

}

上方出现的监听程序可以在端点定义中使用,如下所示:

from("guava-eventbus:busName?listenerInterface=com.example.CustomListener").to("seda:queue");