我有下面的演员
class FooActor(name: String) extends Actor {
def receive = {
case msg: Bar =>
val x : Future[List[T]] = makeDBCall()
x pipeTo sender()
case _ =>
}
}我想要的是,在pipeTo操作成功地将列表发送给发送者之后,actor实例自行终止。
我有点犹豫要不要把自己放进去!因为我不理解pipeTo是如何工作的,所以我只能在下一行输入毒丸消息。
如果pipeTo阻塞,直到将来完成,那么可能会把自己!下一行的毒丸是安全的。
但是如果pipeTo是非阻塞的,并且它通过回调的方式执行,那么调用self!下一行的毒丸会过早地杀死演员。
发布于 2017-07-12 22:54:24
查看pipeTo is implemented如何
final class PipeableFuture[T](val future: Future[T])(implicit executionContext: ExecutionContext) {
def pipeTo(recipient: ActorRef)(implicit sender: ActorRef = Actor.noSender): Future[T] = {
future andThen {
case Success(r) ⇒ recipient ! r
case Failure(f) ⇒ recipient ! Status.Failure(f)
}
}
// ...基本上,它在未来完成后将消息发送给接收者,并返回另一个未来。所以它是异步的,并且是自我放置的!下一行的PoisonPill不会像您期望的那样工作。
不过,下面这样的代码应该能起到作用:
x.pipeTo(sender()).onSuccess { case _ =>
self ! PoisonPill
}https://stackoverflow.com/questions/45060510
复制相似问题