我想对我的数据库进行两次调用,这需要一段时间才能返回结果,而且我不想阻塞当前线程。我使用 Akka Futures 来包装数据库调用。
我不想等待(阻塞)两个调用返回,而是指定一个要调用的回调函数,然后它可以呈现响应。我怎么做?这是我的控制器代码:
def showPie = IsAuthenticated(Roles.validator) { user => implicit request =>
val eventUid = request.session.get(EventUid).get
val printed = Akka.future(TicketRepository.getCountForState(eventUid, "Printed"))
val validated = Akka.future(TicketRepository.getCountForState(eventUid, "Validated"))
//this would be evil, because it would block: Ok(views.html.pie(printed.await(1000).get, validated.await(1000).get))
//create a promise for all the promised results
val promise = Promise.sequence(List(printed, validated))
//this doesnt work, but how can I make it work WITHOUT blocking this thread?
promise.callWhenResultIsReady(Ok(view.html.pie(promise.get))
}