Future response

19 views
Skip to first unread message

Maatary Okouya

unread,
Feb 18, 2015, 5:54:24 PM2/18/15
to spray...@googlegroups.com
Would it be a good simulation of what is happening when we get a future response ? Indeed nothing is blocking in that code. No thread stays idle. 

I'm just trying to understand if i'm getting a good sense of what is happening on the background. 

Of course down the line we would have Java NIO and some kind of "non-blocking but still blocking thread" at the bottom. 

I think the key here is that the receive here is a kind of call back in itself. When done processing it sets the value of our promise.

Now i don't know if sending Promise like that is ok, but i'm feeling that somehow that's what is suppose to going on under the hood: A call back that trigger the the actual schedule of the call back that the client install. Nothing blocks !!!

case class MSGPromise(msg: String, promise: Promise[String])



class PromiseSeterActor() extends Actor {



override def receive: Receive = {

case MSGPromise(msg:String, p: Promise[String]) => p.success(s"message $msg processed")
case _ =>
}
}



object PromiseApp extends App{


val system = ActorSystem("Promise-system")



def sendStringMessage (msg: String, actor: ActorRef) (implicit executor: ExecutionContext): Future[String] = {

val promise = Promise[String]()

promiseSeterActor ! MSGPromise(msg, promise)

promise.future
}


val promiseSeterActor = system.actorOf(Props[PromiseSeterActor], "PromiseSeterActor")

import system.dispatcher

sendStringMessage("helloMsg", promiseSeterActor) onComplete{

case Success(e) => println(s"a successful response: $e has returned")

case Failure(e) => println("failed")

}


Reply all
Reply to author
Forward
0 new messages