阿卡 - 问问模式 - 结合结果

时间:2016-06-03 16:34:00

标签: scala akka akka-persistence

我正在设计一个演员系统。 centerActor需要等待来自actor1和actor2的结果才能响应客户端。

在centerActor中:

def receive: Receive ={
  case request => {
    implicit val timeout = Timeout(1.seconds)
    val ask1 = actor1 ? Update1.mapTo[Int]
    val ask2 = actor2 ? Update2.mapTo[String]
    val response =
      (for(x <- ask1; y <- ask2) yield Done).recover{case _ => FailReply}
    response pipeTo sender
  }
}

在此设计中,我的发件人(客户端)无法接收Done条消息。我不知道为什么。 所以我在想第二个设计:

def receive: Receive = {
  implicit val timeout = Timeout(1.seconds)
  case request => {
    val ask1 = actor1 ? Update1.mapTo[Int]
    val ask2 = actor2 ? Update2.mapTo[String]
    val response =
      (for(x <- ask1; y <- ask2) yield Done).recover{case _ => FailReply})
    response pipeTo self
    context.become(waiting(sender))
  }
}

def waiting(s: ActorRef) = {

    case Done => 
         s ! Done
         unstashAll()
         context.unbecome()
    case FailReply => s ! FailReply
    case _ => stash()

}

0 个答案:

没有答案