【发布时间】:2016-11-30 03:23:08
【问题描述】:
我有一个使用 play 2.5 框架开发的宁静 Web 服务。 我想通过调用自身来启动我的 Web 服务。这是为了确保我的服务完全启动并运行。
我采用的方法是使用eagerBinding。但是使用急切绑定注入的类中的代码会在应用启动之前执行
这是我的 Eagerbinding 代码的样子
@Singleton
class PrimingMe @Inject()(ws: WSClient) {
isServicePrimed
def isServicePrimed: Boolean = {
println("PRIME ME!!!")
val response = ws.url("http://localhost:9000/index").get
.map {
response =>
response.status match {
case 200 => true
case _ => false
}
}
try {
Await.result(response, 5.second)
} catch {
case _ => false
}
}
}
class ServiceInjectionModule extends AbstractModule {
def configure(): Unit = {
bind(classOf[PrimingMe]).asEagerSingleton
}
}
在 application.conf 中
play.modules.enabled += "util.ServiceInjectionModule"
我想用一个虚拟服务调用来启动我的应用程序,这样当真正的流量开始到来时,所有的数据库连接都会建立起来。目前,我对服务的第一次 api 调用比平时花费的时间要长得多。我还有什么其他选择可以实现这一目标。
【问题讨论】:
标签: scala playframework