CustomExecutionContext - Should I mark is @Singleton?

Hello all,
I’m implementing a REST API Application with Play Framework.
The application will using blocking JDBC call, so I’d like to using a custom execution context like in Document here:

public class MyExecutionContext extends CustomExecutionContext {

  public MyExecutionContext(ActorSystem actorSystem) {
    // uses a custom thread pool defined in application.conf
    super(actorSystem, "my.dispatcher");

Then I use it like that:

public class Application extends Controller {

  private MyExecutionContext myExecutionContext;

  public Application(MyExecutionContext myExecutionContext) {
    this.myExecutionContext = myExecutionContext;

  public CompletionStage<Result> index() {
    // Wrap an existing thread pool, using the context from the current thread
    Executor myEc = HttpExecution.fromThread((Executor) myExecutionContext);
    return supplyAsync(() -> intensiveComputation(), myEc)
        .thenApplyAsync(i -> ok("Got result: " + i), myEc);

  public int intensiveComputation() {
    return 2;

But I noticed that the MyExecutionContext is not marked @Singleton, so as I know, It will create an instance of MyExecutionContext each time any class uses it is initiated. Then that might lead a memory issue.

So should I mark the MyExecutionContext class @Singleton?

Thank you so much!

That is what I do, i.e. making the execution context as a singleton. That was the recommendation/example given to me by a member of the Play team back in Play 2.5 days. And if I recall correctly, I think older example projects or documentation mentions about singleton, although I cannot find the newer 2.7 stating that anymore, so I am not sure if things have changed. Perhaps someone from the Play team can help clarify? Maybe @marcospereira?

1 Like

Hey @TranNgocKhoa & @chrono_b,

It does not matter that much since the actor system is a singleton, and then your custom execution context will do a lookup for the same Akka dispatcher every time it is created. Of course, it won’t hurt to annotate it as a singleton, but this is not the kind of thing that will put significant memory pressure on your application.



Thank you all.