Advertisement
mitrakov

Play: Create own Execution Context

Feb 24th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.18 KB | None | 0 0
  1. import java.util.concurrent.Executors
  2. import javax.inject.Inject
  3. import scala.concurrent.ExecutionContext
  4. import akka.actor.ActorSystem
  5. import play.api.libs.concurrent.CustomExecutionContext
  6.  
  7. // way1: extend CustomExecutionContext and pass the name of your Akka dispatcher
  8. class MyExecutionContext @Inject()(as: ActorSystem) extends CustomExecutionContext(as, "my-context")
  9.  
  10. // way2: use Akka lookup method (in fact 100% the same as way1)
  11. class Aaa @Inject()(as: ActorSystem) {
  12.   implicit val ec: ExecutionContext = as.dispatchers.lookup("my-context")
  13. }
  14.  
  15. // way3: use standard Java Library: call "fromExecutor" or "fromExecutorService" methods of ExecutionContext
  16. class Bbb {
  17.   implicit val ec: ExecutionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
  18. }
  19.  
  20.  
  21.  
  22. // application.conf:
  23. my-context {
  24.   for-join-executor {
  25.     # The parallelism factor is used to determine thread pool size using the
  26.     # following formula: ceil(available processors * factor). Resulting size
  27.     # is then bounded by the parallelism-min and parallelism-max values.
  28.     parallelism-min = 8
  29.     parallelism-max = 16
  30.     parallelism-factor = 2.0
  31.     task-peeking-mode = "FIFO"
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement