Play 2.6 以降では、guice 依存性注入を使用することが推奨されています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