Play 2.6 ThreadPools exemple not relevant?


(Badin Guillaume) #1

Hello,
I try to recreate different thread pool executors for my play 2.6 apps.
But with the injector system I don’t understand how to.

The example is here : https://www.playframework.com/documentation/2.6.x/ThreadPools#many-specific-thread-pools

object Contexts {
  implicit val simpleDbLookups: ExecutionContext = akkaSystem.dispatchers.lookup("contexts.simple-db-lookups")
  implicit val expensiveDbLookups: ExecutionContext = akkaSystem.dispatchers.lookup("contexts.expensive-db-lookups")
  implicit val dbWriteOperations: ExecutionContext = akkaSystem.dispatchers.lookup("contexts.db-write-operations")
  implicit val expensiveCpuOperations: ExecutionContext = akkaSystem.dispatchers.lookup("contexts.expensive-cpu-operations")
}

How to have an akkaSystem val ?


(Schmitt Christian) #2

In fact, that example looks wrong.

If you want to have multiple ExecutionContexts and wire them via Guice you can do the following:

class ExecutionContextProvider @Inject()(akkaSystem: ActorSystem) extends Provider[ExecutionContext] {
   override lazy val get = akkaSystem.dispatchers.lookup("contexts.simple-db-lookups")
}

class ExectionContextModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = Seq(
bind[ExecutionContext].qualifiedWith("db").toProvider[ExecutionContextProvider]
}

// Usage:
class MyDbClass @Inject()()(implicit @Named("db") executionContext: ExecutionContext) {}

i.e you can add multiple bind[ExecutionContext].qualifiedWith("OTHER_NAME") to your project and access them with @Named("OTHER_NAME").

I created a Issue that the example could be improved: https://github.com/playframework/playframework/issues/8460


(Marcos Pereira) #3

Yes, as @schmitch said, the example is outdated.

I would say the correct way to do it now is to use a CustomExecutionContext (Scala & Java docs).

Best.