首页 文章

关闭由超时未来创建的资源

提问于
浏览
1

当我们有创建资源的方法时,有时需要只等待指定的持续时间 . 例如,我们要等待10秒才能连接到数据库 .

我试图使用future和Await.result来获得它 . 不幸的是,Await.result在指定时间后抛出异常,但不会杀死正在进行的未来 . 所以在超时之后我们最终会遇到TimeoutException,但如果将来最终完成,我们没有任何能力关闭返回的结果 . 例:

import java.io.Closeable
import java.util.concurrent.TimeoutException

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import scala.concurrent.future

object Test {
    object ConnectionManager {
        class Connection extends Closeable {
        println("Connected")
        override def close = println("DB closed")
        }

        def connect = {
        println("Connecting to DB...")
        Thread.sleep(1000 * 7)
        new Connection
        }
    }

    def main(args: Array[String]): Unit = {
        val f = future {
        ConnectionManager.connect
        }
        try {
        val result = Await.result(f, 5 seconds)
        result.close
        } catch {
        case e: TimeoutException => println("Connection timeout")
        }
        Thread.sleep(10 * 1000)
        println("Finished")
    }
}

结果是:

连接到DB ...连接超时已连接完成

因此创建了连接但从未关闭

1 回答

  • 1

    您可以将 f.onSuccess { case c => c.close } 添加到 catch 子句中 .

相关问题