Skip to content

Instantly share code, notes, and snippets.

@nraychaudhuri
Last active April 19, 2018 09:23
Show Gist options
  • Select an option

  • Save nraychaudhuri/9381621 to your computer and use it in GitHub Desktop.

Select an option

Save nraychaudhuri/9381621 to your computer and use it in GitHub Desktop.

Revisions

  1. nraychaudhuri revised this gist Mar 6, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions PlayAkkaExample.scala
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,10 @@ 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 = {
    @@ -21,6 +25,7 @@ object Application extends Controller {
    }
    }))
    computeActor.tell(msg = "do work for me", sender = replyTo)
    //transforming the actor response to Play result
    p.future.map(response => Ok(response))
    }

  2. nraychaudhuri created this gist Mar 6, 2014.
    34 changes: 34 additions & 0 deletions PlayAkkaExample.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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"
    }
    }