what does replyTo refer to here?

234 views Asked by At

I am reading this Akka document, https://doc.akka.io/docs/akka/current/typed/actor-lifecycle.html#spawning-children, and this code snippet,

CompletionStage<ActorRef<HelloWorld.Greet>> greeter =
    AskPattern.ask(
        system,
        replyTo ->
            new SpawnProtocol.Spawn<>(HelloWorld.create(), "greeter", Props.empty(), replyTo),
        timeout,
        system.scheduler());

This is to use the Ask pattern to create a child actor. My question is about this piece,

replyTo ->
            new SpawnProtocol.Spawn<>(HelloWorld.create(), "greeter", Props.empty(), replyTo),

It is a lamda representing a factory function, which takes a replyTo parameter, and returns an actor. But what is replyTo here? where does it come from and what does it refer to? Having a hard time to have a very clear picture about it.

Thanks for any help!

1

There are 1 answers

0
lee On

Finally figure this out, so let me answer my own question.

Basically, the Ask pattern produces a future that represents the reply from the actor that is being asked. In other words, we ask, and we get a future back that represents a placeholder, which will be fulfilled by the real response when it is ready.

So how does Akka know how to fulfill this future? That is where the replyTo parameter fits in. Akka actually creates a temporary actor in the actor system, the replyTo parameter is used to represent this temporary actor. When the actor being asked replies to the message it receives from an Ask, this temporary actor starts to run and it completes the future with the response replied from the actor that is being asked.

Hope this makes sense, and hope this helps someone else who has the same question. Akka is great, but I really hope they have a better documentation so we don't have to search so hard.