Search code examples
playframeworkakkaplayframework-2.2

Custom Akka dispatcher in Play 2.2.x


I'm trying to configure custom Akka dispatchers in Play 2.2.x in the application.conf configuration file like this:

contexts.my-dispatcher {
  fork-join-executor {
    parallelism-max = 4
  }
}

In addition, I'm specifying either withDispatcher programmatically

Akka.system.actorOf(Props(classOf[MyActor], arg1, arg2)
  .withDispatcher("contexts.my-dispatcher"), name = "MyActor")

or, preferably, I would like to do it in the configuration directly

akka.actor.deployment {
  /MyActor {
    dispatcher = contexts.my-dispatcher
  }
}

Both cases don't seem to work for me as in the log file (application.log; configured via logback; logging via ActorLogging) I see the default-dispatcher-[X] being used. Example:

2013-12-30 11:37:40,999 - [DEBUG] - from x.y.z.MyActor in application-akka.actor.default-dispatcher-7
Some message

When I'm specifying a wrong dispatcher name on purpose, I get the "Dispatcher [wrong-name] not configured, using default-dispatcher". Since I don't get this warning with the above configuration/code, I'm assuming that in general it should be ok. Can someone shed some light on what is going on here?

The logback appender configuration (application-logger.xml) is:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>${application.home}/logs/application.log</file>
  <encoder>
    <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
  </encoder>
</appender>

Solution

  • ActorLogging is asynchronous. When you log using ActorLogging, it sends a message to the logging actor, which by default runs on the default dispatcher. Logback logs the thread that called it, which will be the ActorLogging actors thread, not your actors thread. Logback has no knowledge of actors, so it has no idea which dispatcher the log message originally came from.

    Try logging directly using logback (or slf4j), you should see the right dispatcher then.