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:
https://www.playframework.com/documentation/2.7.x/JavaAsync#Using-CustomExecutionContext-and-HttpExecution
public class MyExecutionContext extends CustomExecutionContext {
@Inject
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;
@Inject
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!