[英]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.