Last active
April 19, 2018 09:23
-
-
Save nraychaudhuri/9381621 to your computer and use it in GitHub Desktop.
Returning Play response from Akka actors without using ask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package controllers | |
| import play.api.mvc._ | |
| import scala.concurrent._ | |
| import akka.actor.{Props, Actor} | |
| import play.api.Play.current | |
| import play.api.libs.concurrent.Akka | |
| import scala.concurrent.ExecutionContext.Implicits._ | |
| object Application extends Controller { | |
| val computeActor = Akka.system.actorOf(Props[BizActor]) | |
| def index = Action.async { | |
| val p = Promise[String] | |
| val replyTo = Akka.system.actorOf(Props(new Actor { | |
| def receive = { | |
| case reply: String => | |
| p.success(reply) | |
| context.stop(self) | |
| } | |
| })) | |
| computeActor.tell(msg = "do work for me", sender = replyTo) | |
| p.future.map(response => Ok(response)) | |
| } | |
| } | |
| class BizActor extends Actor { | |
| def receive = { | |
| case _ => sender ! "I am done with work" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Nilanjan
I have a question on the above code
Will the usage of a Promise allocate a separate thread for its processing (like how its for a Future)? If that is the case, this code will allocate a thread per user request (to the index operation) right? Could you please explain a bit on that?
Thank you
Anand