[ Initializing expensive objects on startup of an play app ]
I am trying to implement a routing service with play framework 2.2. Therefore I have a pretty expensive graph singleton object which needs to be instantiated when the play app is deployed and started up. The instantiation of the graph needs approx. 10 to 15 minutes, loading all the nodes and edges and restrictions into memory, and I don't want to trigger that when the first API GET Request comes in for sure.
Furthermore I want to be able to react if the first GET request comes in and the graph isn't loaded yet, returning an error code (HTTP 503 or something like that).
Just an idea: Should this be done in Application()
Constructor? I am asking because Application has no Constructor out of the box and I guess this has some intention. Should the constructor be private then?
Answer 1
Definitely not in Application
.
Write it in Global.onStart
:
(Java) http://www.playframework.com/documentation/2.2.x/JavaGlobal
(Scala) https://www.playframework.com/documentation/2.2.x/ScalaGlobal