scalaakkafutureakka-streamexecutioncontext

How to run futures in their own execution context and not on the Actor System dispatcher. [Scala|Akka]


How do we use src/main/resources/application.conf for the same where the dedicated dispatcher in our code would automatically lookup in application.conf for the dispatcher


Solution

  • Here's two example configurations for execution contexts you can put in your application.conf:

    background-scheduled-tasks-dispatcher {                                                
      type = Dispatcher                                                                    
      executor = "fork-join-executor"    
      fork-join-executor {
        parallelism-min = 2
        parallelism-factor = 2.0
        parallelism-max = 10
      }
      throughput = 1
    }
    
    blocking-io-ec {
      type = Dispatcher
      executor = "thread-pool-executor"
      thread-pool-executor {
        fixed-pool-size = 50
      }
      throughput = 1
    }
    

    Here's an example of how to access one of them inside your app:

    val ec :ExecutionContext = actorSystem.dispatchers.lookup("blocking-io-ec")
    

    You can prefix the line with implicit val ec or you can use it explicitly

    someFuture.map(myFunc)(ec)