Search code examples
scalaplayframeworkplayframework-2.3

Not able to lookup a custom dispatcher


I am using Play 2.3.7 and I need to use Actors from inside the controller. The following code is working fine

implicit val system = ActorSystem()
implicit val dispatcher = system.dispatcher
val future = (IO(Http) ? Get(url).withHeaders(...).mapTo[HttpResponse]
val result = Await.results(future, Duration.Inf)

Now I make the following change to my conf/application.conf

play {
  akka {
    actor {
      default-dispatcher {
        type = Dispatcher
        executor = "thread-pool-executor"
        thread-pool-executor {
          fixed-pool-size = 128
        }
      }
      foo-dispatcher {
        type = Dispatcher
        executor = "thread-pool-executor"
        thread-pool-executor {
          fixed-pool-size = 128
        }
      }
    }
  }
}

And now, change my code to

implicit val system = ActorSystem()
implicit val dispatcher = system.dispatchers.lookup("foo-dispatcher")
val future = (IO(Http) ? Get(url).withHeaders(...).mapTo[HttpResponse]
val result = Await.results(future, Duration.Inf)

I get an exception with message [foo-dispatcher] not configured


Solution

  • Reference the full path:

    implicit val dispatcher = system.dispatchers.lookup("play.akka.actor.foo-dispatcher")
    

    If you want to use system.dispatchers.lookup("foo-dispatcher"), define foo-dispatcher outside of the play namespace:

    play {
      akka {
        actor {
          default-dispatcher {
            type = Dispatcher
            executor = "thread-pool-executor"
            thread-pool-executor {
              fixed-pool-size = 128
            }
          }
        }
      }
    }
    
    foo-dispatcher {
      type = Dispatcher
      executor = "thread-pool-executor"
      thread-pool-executor {
        fixed-pool-size = 128
      }
    }