繁体   English   中英

Akka EventBus for Java的示例

[英]Example of Akka EventBus for Java

需要一些关于如何在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);

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

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

我想你只是一条线:

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

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

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

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

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

我对此并不确定,如果我错了,请纠正我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM