Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2 thread pools : default pool VS Akka Pool with Java

According to the documentation, the easier way to use an actor is :

Promise promiseOfInt = Akka.future( new Callable() { public Integer call() { return ... ; } } );

But the doc also says that with the Java API it will always use the same thread pool (play default thread pool).

The documentation says that there is also an Akka pool for actors. How can I send this actor tasks to the Akka thread pool, not to block common user actions? So I would be able to tune the Akka pool and keep the default pool small.

Thanks, Loic

like image 678
Loic Avatar asked Nov 13 '22 05:11

Loic


1 Answers

The Java Akka plugin (play.libs.Akka) forwards to the Scala plugin (play.api.libs.Akka), which in turn starts a new Actor system based on the app's configuration. (That's all the plugin does.)

So you would configure that ActorSystem and all its dispatchers (a dispatcher is also an ExecutionContext) using your normal application.conf file based on the akka config key. These dispatchers are the thread pools the documentation is referring to.

The default thread pool is used when you import play.api.libs.concurrent.Execution.default. This is a Scala-only API. In Java, this ExecutionContext is always used automatically as soon as you touch futures and promises. This thread pool is actually based on Play's internal actor system, configured via the play config key.

So in summary, all your actors, when created via the Akka plugin's ActorSystem, will automatically use the Akka thread pool (inside an actor you can refer to that using context.dispatcher).

Outside actors, you will be using Play's default/internal thread pool.

like image 175
Marius Soutier Avatar answered Nov 15 '22 08:11

Marius Soutier