8

需要一些关于如何在 Java(不是 Scala!)中使用 Akka 提供的 EventBus 的建议。网站上的文档似乎不完整:http ://doc.akka.io/docs/akka/2.0.1/java/event-bus.html

据我了解,应该创建actor以对特定消息做出反应,例如:

final ActorSystem actorSystem = ActorSystem.create("ServerEvents");
final ActorRef actor = actorSystem.actorOf(new Props(SeverEventHandler.class));
actorSystem.eventStream().subscribe(actor,ServerMessage.class);

但是现在还不清楚如何向事件总线发送消息。

有人可以分享一些好的教程/示例/等吗?

4

2 回答 2

11

我认为你只是短线:

final ActorSystem actorSystem = ActorSystem.create("ServerEvents");
final ActorRef actor = actorSystem.actorOf(new Props(SeverEventHandler.class));
actorSystem.eventStream().subscribe(actor,ServerMessage.class);

actorSystem.eventStream().publish(new ServerMessage()); <<== add this

虽然 ServerEventHandler 应该类似于

public class ServerEventHandler extends UntypedActor {
  @Override
  public void onReceive(final Object message) {
    System.out.println("Got event in thread: " + Thread.currentThread().getName());
    System.out.println("Event: " + message);
  }
}
于 2012-06-11T13:01:20.897 回答
0

我不确定@Jan Goyvaerts 的代码是否仍然是最新的,截至 2019 年 9 月,这条线不起作用:

final ActorRef actor = actorSystem.actorOf(new Props(SeverEventHandler.class));

我认为现在这样做的正确方法可能是:

ActorRef newActor = system.actorOf(Props.create(AATestAkkaEventBus.class));

这个我也不是很清楚,如有错误请指正

于 2019-09-03T08:37:12.553 回答