从 Play 2.6 开始,建议对execution context
.
默认执行上下文注入:
Foo.scala
class Foo @Inject()()(implicit ec:ExecutionContext) {
def bar() = {
WS.url(url)
.get
.map { response => // error occurs on this line
response.status match {
case 200 => Right(response.json)
case status => Left(s"Problem accessing api, status '$status'")
}
}
}
自定义执行上下文注入:
应用程序.conf
# db connections = ((physical_core_count * 2) + effective_spindle_count)
fixedConnectionPool = 9
database.dispatcher {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = ${fixedConnectionPool}
}
}
DatabaseExecutionContext.scala
@Singleton
class DatabaseExecutionContext @Inject()(system: ActorSystem) extends CustomExecutionContext(system,"database.dispatcher")
Foo.scala
class Foo @Inject()(implicit executionContext: DatabaseExecutionContext ) { ... }
更多信息在:
https://www.playframework.com/documentation/2.6.x/Migration26#play.api.libs.concurrent.Execution-is-deprecated
https://www.playframework.com/documentation/2.6.x/Highlights26#CustomExecutionContext -and-Thread-Pool-Sizing