Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guizmaii/36d35d03ab9aea2665d4 to your computer and use it in GitHub Desktop.
Save guizmaii/36d35d03ab9aea2665d4 to your computer and use it in GitHub Desktop.
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 {
//using Promise as a write handle for the response from actor.
//we will use this promise to create a future (read handle)
//which will be returned from this action. This future will complete
//when the promise is successfully populated with value
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)
//transforming the actor response to Play result
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