One way to avoid blocking while waiting for a response is to send using the ask-aka method ?, which returns Future(as opposed to !which returns ()).
Using the methods onSuccessor foreach, you can specify the actions that must be performed if / when the future is completed with a response. To use this, you need to mix in AskSupport:
class ApplicationApi(asyncIo : ActorRef) extends Actor with AskSupport {
def receive = {
case request: GetUser =>
val replyTo = sender
asyncIo ? AsyncGet("http://app/user/" + request.userId) onSuccess {
case response: AsyncResponse =>
val user = parseUser(response.body)
replyTo ! GetUserResponse(user)
}
}
, ApplicationApi, . .
, , sender , .
trait FromSupport { this: Actor =>
case object from {
def unapply(msg: Any) = Some(msg, sender)
}
}
class MyActor extends Actor with FromSupport {
def receive = {
case (request: GetUser) from sender =>
}
}