0
votes

In integrating Akka with Play (have to use Java 7), what I want is for a controller action to fire-and-forget a message to an actor. However, I want the actor to send the response of it's work to the client that sent the http request.

I've successfully tried this with use of ask() in my controller... and want to change it to use tell(). It was "easy" as ask returns a Future.

I know that I need to return a Promise.

I "understand" Future in Akka - just not able to stitch a story together on:

  • I'm imagining that in the controller, I can compose a message and pass the Http.Context - current() ? - I actually want the Http.Request, Http.Response and maybe the Http.Session
  • send this message to a router actor using tell
  • what does the controller return ?? where is my Promise< Result> - am I to create a Future that wraps the tell() ? If so, how?
  • a worker actor does the work (which could mean serializing some JSON object over Http.Response) and sends a reply to the sender -- which who ?? Play Action?? Imagining that it's a Future in the controller, I would then map the Future< ?> to a Promise< Result> ??

(something about the above list isn't jiving...)

Why struggle with this? I want to follow the advise in using tell() - http://techblog.net-a-porter.com/2013/12/ask-tell-and-per-request-actors/

Any help appreciated.

Thanks, s-

1

1 Answers

5
votes

You can implement it in the same way as the article suggests. Create a per-request actor which on success or failure would complete a promise for which the future is being returned in the play controller.

So your controller would look something like this (sorry, code is in Scala and not compiled):

def index = Action.async {
  val responsePromise = Promise[String]
  Akka.system.actorOf(Props(classOf[MyPerRequestActor], responsePromise))
  responsePromise.future.map(Ok(_))
}