Using Akka Dispatchers for Handling Futures

402 views Asked by At

I have a Spray based HTTP Service. I have a stream that runs inside this HTTP application. Now since this stream does a lot of I/O, I decided to use a separate thread pool. I looked up the Akka documentation to see what I could do so that my thread pool is configurable. I came across the Dispatcher concept in Akka. So I tried to use it as below in my application.conf:

akka {
  io-dispatcher {
    # Dispatcher is the name of the event-based dispatcher
    type = Dispatcher
    # What kind of ExecutionService to use
    executor = "fork-join-executor"
    # Configuration for the fork join pool
    fork-join-executor {
      # Min number of threads to cap factor-based parallelism number to
      parallelism-min = 2
      # Parallelism (threads) ... ceil(available processors * factor)
      parallelism-factor = 2.0
      # Max number of threads to cap factor-based parallelism number to
      parallelism-max = 10
    }
    # Throughput defines the maximum number of messages to be
    # processed per actor before the thread jumps to the next actor.
    # Set to 1 for as fair as possible.
    throughput = 20
  }
}

In my Actor, I tried to lookup this configuration as:

context.system.dispatchers.lookup("akka.io-dispatcher")

When I ran my service, I get the following error:

[ERROR] [05/03/2016 12:59:08.673] [my-app-akka.actor.default-dispatcher-2] [akka://my-app/user/myAppSupervisorActor] Dispatcher [akka.io-dispatcher] not configured
akka.ConfigurationException: Dispatcher [akka.io-dispatcher] not configured
    at akka.dispatch.Dispatchers.lookupConfigurator(Dispatchers.scala:99)
    at akka.dispatch.Dispatchers.lookup(Dispatchers.scala:81)

My questions are:

  1. Is this io-dispatcher thread pool that I create, is it meant to be only used for Actor's? My intention was to use this thread pool for my streams which gets instantiated by one of the Actor. I then pass this thread pool to my stream.

  2. How could I create an ExecutionContext by just loading the dispatcher from the application.conf? Should I use any specific library that would read my configuration for the thread pool and give me an ExecutionContext?

1

There are 1 answers

0
joesan On BEST ANSWER

The configuration is correct. All You need to do is to pass the loaded configuration file to the Akka ActorSystem like:

ActorSystem("yourActorSystem", ConfigFactory.load())