Home > Mobile >  Akka Java Receptionist ask pattern example
Akka Java Receptionist ask pattern example

Time:10-05

I need to find an actor from non-actor class having System object using receptionist. I can only find Scala examples but none has java example of ask pattern.

CodePudding user response:

Here is Akka's documentation for interacting with actors outside of the actor system.

There is a tab for Java.

enter image description here

CodePudding user response:

It would look something like (apologies if the Java is atrocious):

// TargetActor.Command is just a placeholder
ActorSystem<Void> system;
ServiceKey<TargetActor.Command> key;

CompletionStage<Receptionist.Listing> result =
    AskPattern.ask(
        system.receptionist(),
        replyTo -> Receptionist.find(key, replyTo),
        Duration.ofSeconds(10),  // ask will fail if no reply received in this time
        system.scheduler()
    );

You can then use the usual CompletionStage methods (e.g. whenComplete and friends) to extract the Receptionist.Listing and take appropriate action.

  • Related