我正在使用Play Framework 2.1.1和一个生成java.util.concurrent.Future结果的外部Java库.我正在使用scala future而不是Akka,我认为这是Play 2.1的正确选择.如何在保持代码无阻塞的同时将java.util.concurrent.Future包装到scala.concurrent.Future中?
def geConnection() : Connection = {
// blocking with get
connectionPool.getConnectionAsync().get(30000, TimeUnit.MILLISECONDS)
}
Run Code Online (Sandbox Code Playgroud)
上面的代码返回一个连接,但使用了get,所以它变成了阻塞
def getConnectionFuture() : Future[Connection] = {
future {
// how to remove blocking get and return a scala future?
connectionPool.getConnectionAsync().get(30000, TimeUnit.MILLISECONDS)
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想要一个scala函数,它返回连接作为未来,如上面的代码,但没有通过get阻止代码.我需要在函数中添加什么才能使其无阻塞.
任何指针都会很棒.