Akka EventBus example for Java

You need to know how to use the EventBus provided by Akka in Java (not Scala!). The documentation on the website looks incomplete: http://doc.akka.io/docs/akka/2.0.1/java/event-bus.html

As I understand it, an actor must be created to respond to certain messages, for example:

final ActorSystem actorSystem = ActorSystem.create("ServerEvents"); final ActorRef actor = actorSystem.actorOf(new Props(SeverEventHandler.class)); actorSystem.eventStream().subscribe(actor,ServerMessage.class); 

But now it’s not clear how to send a message to the event bus.

Can someone please share some good tutorials / examples, etc.?

+7
source share
2 answers

I think you have only one line:

 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 

While ServerEventHandler should be something like

 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); } } 
+10
source

I do not know for sure whether the @Jan Goyvaerts code has been updated so far, as of September 2019 this line does not work:

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

I think the right way to do this now might be:

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

I am by no means sure of this, please correct me if I am wrong

0
source

All Articles