简体   繁体   中英

Akka EventBus for Java example

I need some advice of how to use EventBus provided by Akka in Java (not Scala!). I've seen the doc in 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??

it would be appreciated if you guys can help

thanks!

ServerMessage is the sample event class. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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