Akka EventBus for Java的示例

jde*_*lop 7 java akka event-bus

需要一些关于如何在Java中使用Akka提供的EventBus的建议(而不是Scala!).网站上的文档似乎不完整: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);
Run Code Online (Sandbox Code Playgroud)

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

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

Jan*_*rts 10

我想你只是一条线:

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
Run Code Online (Sandbox Code Playgroud)

虽然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);
  }
}
Run Code Online (Sandbox Code Playgroud)