import scala.concurrent.duration.Duration
import scala.concurrent.duration.Duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.Future._
import scala.concurrent.ExecutionContext.Implicits.global
object TestClass {
final case class Record(id: String)
final case class RecordDetail(id: String)
final case class UploadResult(result: String)
val ids: Seq[String] = Seq("a", "b", "c", "d")
def fetch(id: String): Future[Option[Record]] = Future {
Thread sleep 100
if (id != "b" && id != "d") {
Some(Record(id))
} else None
}
def fetchRecordDetail(record: Record): Future[RecordDetail] = Future {
Thread sleep 100
RecordDetail(record.id + "_detail")
}
def upload(recordDetail: RecordDetail): Future[UploadResult] = Future {
Thread sleep 100
UploadResult(recordDetail.id + "_uploaded")
}
def notifyUploaded(results: Seq[UploadResult]): Unit = println("notified " + results)
def main(args: Array[String]): Unit = {
//for each id from ids, call fetch method and if record exists call fetchRecordDetail
//and after that upload RecordDetail, collect all UploadResults into seq
//and call notifyUploaded with that seq and await result, you should see "notified ...." in console
// In the following line of code how do I pass result of fetch to fetchRecordDetail function
val result = Future.traverse(ids)(x => Future(fetch(x)))
// val result: Future[Unit] = ???
Await.ready(result, Duration.Inf)
}
}
我的问题是,我不知道在main
中放入哪些代码以使其按照评论中的说明运行。总而言之,我有一个ids:Seq[String]
,我希望每个id都通过异步方法fetch
,fetchRecordDetail
,upload
,最后是整个Seq
来来notifyUploaded
。
答案 0 :(得分:2)
我认为最简单的方法是:
def main(args: Array[String]): Unit = {
//for each id from ids, call fetch method and if record exists call fetchRecordDetail
//and after that upload RecordDetail, collect all UploadResults into seq
//and call notifyUploaded with that seq and await result, you should see "notified ...." in console
def runWithOption[A, B](f: A => Future[B], oa: Option[A]): Future[Option[B]] = oa match {
case Some(a) => f(a).map(b => Some(b))
case None => Future.successful(None)
}
val ids: Seq[String] = Seq("a", "b", "c", "d")
val resultSeq: Seq[Future[Option[UploadResult]]] = ids.map(id => {
for (or: Option[Record] <- fetch(id);
ord: Option[RecordDetail] <- runWithOption(fetchRecordDetail, or);
our: Option[UploadResult] <- runWithOption(upload, ord)
) yield our
})
val filteredResult: Future[Seq[UploadResult]] = Future.sequence(resultSeq).map(s => s.collect({ case Some(ur) => ur }))
val result: Future[Seq[UploadResult]] = filteredResult.andThen({ case Success(s) => notifyUploaded(s) })
Await.ready(result, Duration.Inf)
}
这个想法是你首先通过所有方法获得Seq[Future[_]]
map
(这里使用for-comprehension完成)。这是一个重要的技巧是实际传递Seq[Future[Option[_]]]
。通过Option[_]
辅助方法将runWithOption
传递到整个链中可以大大简化代码,而无需阻止直到最后阶段。
然后,您将Seq[Future[_]]
转换为Future[Seq[_]]
,并过滤掉ids
阶段失败的fetch
的结果。最后你申请notifyUploaded
。
P.S。请注意,此代码中没有错误处理,并且不清楚在不同阶段出现错误时如何表现它。