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

据我了解,应该创建actor以对特定消息使用react,例如:

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

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

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

最佳答案

我认为您仅差一线:

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);
  }
}

10-08 02:56