简体   繁体   English

Akka EventBus for Java示例

[英]Akka EventBus for Java example

I need some advice of how to use EventBus provided by Akka in Java (not Scala!). 我需要一些关于如何在Java中使用Akka提供的EventBus的建议(而不是Scala!)。 I've seen the doc in http://doc.akka.io/docs/akka/2.0.1/java/event-bus.html 我在http://doc.akka.io/docs/akka/2.0.1/java/event-bus.html上看过该文档

and I tried to do it myself, so I got these code here: 我自己试着这样做,所以我在这里得到了这些代码:

public class Subscriber {

public static void main(String args[]){
    final ActorSystem actorSystem = ActorSystem.create("ServerEvents");
    final ActorRef actor = actorSystem.actorOf(new Props(ServerEventHandler.class));
    actorSystem.eventStream().subscribe(actor,ServerMessage.class);
    actorSystem.eventStream().publish(new ServerMessage());
}
  }


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

the question is, I known that 问题是,我知道

actorSystem.eventStream().subscribe(actor,ServerMessage.class);
actorSystem.eventStream().publish(new ServerMessage());

ServerMessage() is the channel and message to sub/pub, but what is the exactly content in Class ServerMessage?? ServerMessage()是sub / pub的通道和消息,但Class ServerMessage中的确切内容是什么?

it would be appreciated if you guys can help 如果你们能提供帮助,我们将不胜感激

thanks! 谢谢!

ServerMessage is the sample event class. ServerMessage是示例事件类。 Basically you can put any instance of any class in there (so your own event implementation), as long as you have an actor that is subscribed to that type of event. 基本上你可以把任何类的任何实例放在那里(所以你自己的事件实现),只要你有一个订阅了那种类型的事件的actor。

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

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