我有一個演員阿卡:發送將來消息給演員
def receive = {
case All() => {
val collection: BSONCollection = db("ping")
val future:Future[List[Ping]] = collection.find(BSONDocument()).cursor[Ping].toList()
val zender = sender
future onComplete {
case Success(list) => zender ! list
case Failure(throwable) => zender ! List()
}
}
}
內部下面的代碼,我不喜歡我怎麼也得使用的onComplete函數將結果返回給發件人演員。我想知道是否有可能將其轉化成這樣的:
def receive = {
case All() => {
val collection: BSONCollection = db("ping")
val future:Future[List[Ping]] = collection.find(BSONDocument()).cursor[Ping].toList()
"sender ! future" // one option
"future.map(list => sender ! list)" //Another option. I know it's not map, but maybe another function
}
}
我覺得這個流動與將來的鏈接更好。
不是你的第一種方法嗎? – 2013-12-18 07:18:15
@Alex是的,但管道更優雅。 – Luciano 2013-12-18 11:46:36
好的。但是你說**我不喜歡我如何使用onComplete函數將結果發送回發件人actor。**並且您的代碼完全是這樣 - 您將結果發送回發件人actor 。 – 2013-12-18 11:52:48