PlayFramework 2.6 关闭钩子
PlayFramework 2.6 shutdown hook
Play Framework 2.6
Mongo3.6.2
Mongo Scala 驱动程序 2.2.0
每次关闭应用程序时,我都想删除 MongoDB 数据库。我有以下代码,实现生命周期停止挂钩,但是当 SIGTERM 发送到应用程序时,它不会删除数据库。我做错了什么?
@Singleton
class Repo @Inject() (lifecycle: ApplicationLifecycle) {
val codecRegistry: CodecRegistry = fromRegistries(fromProviders(classOf[MyCollection]), DEFAULT_CODEC_REGISTRY )
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb").withCodecRegistry(codecRegistry)
.......
lifecycle.addStopHook { () => {
database.drop().toFuture()
}
}
}
等待数据库删除 Future
完成(并关闭 MongoClient
):
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.duration.Duration
...
lifecycle.addStopHook { () =>
val result = Await.result(database.drop().toFuture(), Duration(10, TimeUnit.SECONDS))
Future.successful(mongoClient.close())
}
Play Framework 2.6
Mongo3.6.2
Mongo Scala 驱动程序 2.2.0
每次关闭应用程序时,我都想删除 MongoDB 数据库。我有以下代码,实现生命周期停止挂钩,但是当 SIGTERM 发送到应用程序时,它不会删除数据库。我做错了什么?
@Singleton
class Repo @Inject() (lifecycle: ApplicationLifecycle) {
val codecRegistry: CodecRegistry = fromRegistries(fromProviders(classOf[MyCollection]), DEFAULT_CODEC_REGISTRY )
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb").withCodecRegistry(codecRegistry)
.......
lifecycle.addStopHook { () => {
database.drop().toFuture()
}
}
}
等待数据库删除 Future
完成(并关闭 MongoClient
):
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.duration.Duration
...
lifecycle.addStopHook { () =>
val result = Await.result(database.drop().toFuture(), Duration(10, TimeUnit.SECONDS))
Future.successful(mongoClient.close())
}