2

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?

Jürgen Zornig
  • 1,174
  • 20
  • 48

1 Answers1

5

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

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
Leo
  • 2,097
  • 21
  • 35
  • 2
    Thanks, thats what I am looking for...find it quite hard to get answers from play documentation if you do not know what you have to exactly search for (lacks an overview/index) – Jürgen Zornig Jan 05 '14 at 12:35
  • Somehow initialization only happens after the first request hits play in my case.... – matanster Jan 17 '15 at 10:31
  • 1
    @matt check this http://stackoverflow.com/questions/18316584/globalsettings-onstart-fires-only-after-first-request . There is also beforeStart. – Leo Jan 17 '15 at 13:21