Search code examples
scalapromiseakka-actor

Akka Actors: ask pattern vs Promise


Lately I've found myself wrapping actors in classes so that I get back a little of the typesafety I lose when dealing with ActorRefs. The problem is, at the end, that not only I need to send a specific message, I also need to cast the response to the expected result. So I thought that I could send messages to actors that contain Promise so that they could report the result eventually.

Is that a bad idea? It looks pretty neat to me... Is typesafe and works just as good. Why hasn't anyone come with the idea? Is there anything wrong with it that I haven't noticed?

ask pattern based solution

case class GetUser(id:Long)

(actorRef ! GetUser(1l)).mapTo[User]

class UserRepoActor extends Actor{
  def receive={
   case GetUser(id)=>
     sender() ! getUser(id)
  }

  ...
}

Promise based solution

case class GetUser(id: Long, resp: Promise[User])

val req = GetUser(1l,Promise())
actorRef ! req
req.resp.future // No casting!!

class UserRepoActor extends Actor{
  def receive={
   case GetUser(id,resp)=>
     response.success(getUser(id))
  }

  ...
}

Solution

  • There is nothing wrong. Very close approach is used in akka typed with the only difference: a single-use ActorRef[T] is being sent instead of Promise[T]