我使用akka已经有一段时间了.我开始在我的代码中看到一些模式来解决异步io的延迟回复.这个实现好吗?还有另一种方法可以做一个没有阻止的延迟回复吗?
class ApplicationApi(asyncIo : ActorRef) extends Actor {
// store senders to late reply
val waiting = Map[request, ActorRef]()
def receive = {
// an actore request for a user, store it to late reply and ask for asyncIo actor to do the real job
case request : GetUser =>
waiting += (sender -> request)
asyncIo ! AsyncGet("http://app/user/" + request.userId)
// asyncio response, parse and reply
case response : AsyncResponse =>
val user = parseUser(response.body)
waiting.remove(response.request) match {
case Some(actor) …Run Code Online (Sandbox Code Playgroud)