Soumya Simanta
Soumya Simanta

Reputation: 11741

Sending a Future as a message between Akka actors

I'm invoking a web service that returns an Option[Future[String]] inside an Akka actor. Now I need to spin a new child actor and pass this Option[Future[String]] and stop the actor.In the child actor I want to wait for the future to complete so that I can do something with the result. Is it okay to pass Futures around Akka actors?

Upvotes: 1

Views: 961

Answers (1)

Viktor Klang
Viktor Klang

Reputation: 26579

Warning: Not compiled, see doc.akka.io if in doubt.

import akka.pattern.pipeTo
def receive = {
  case something =>
    webService.call() foreach { _ pipeTo context.actorOf(Props[Child]) }
}

The idea is that you do not need the child actor if the web service doesn't return anything (None), and if it returns Some(future) you send the result of that Future to the newly created child actor. The newly created child actor can then self-terminate once it receives the result of the webservice call.

Upvotes: 3

Related Questions