Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I put startup code in Play Framework?

I want to run some code on startup to pre-cache some stuff, and would also like to start a Timer to re-cache some things outside the critical path of a web request. Is this possible with Play Framework, and where so I put this code?

like image 464
sanity Avatar asked Feb 05 '11 15:02

sanity


People also ask

What is activator in Play Framework?

The activator command can be used to create a new Play application. Activator allows you to select a template that your new application should be based off. For vanilla Play projects, the names of these templates are play-scala for Scala based Play applications, and play-java for Java based Play applications.

What is sbt in Play Framework?

sbt file defines settings for your project. You can also define your own custom settings for your project, as described in the sbt documentation. In particular, it helps to be familiar with the settings in sbt.


2 Answers

You need to create a bootstrap job which will be executed by Play at application start time.

@OnApplicationStart public class Bootstrap extends Job {      public void doJob() {        //do stuff     }     } 

Read more about how to do this in the Play Manual.

like image 145
dogbane Avatar answered Sep 20 '22 05:09

dogbane


For playframework 2.6+, please refer to playframework doc: Eager bindings

For playframework 2.0 to 2.5, use GlobalSettings, as the following code:

import play.*;  public class Global extends GlobalSettings {    @Override   public void onStart(Application app) {     Logger.info("Application has started");   }      @Override   public void onStop(Application app) {     Logger.info("Application shutdown...");   }   } 

more information, go to playframework docs: JavaGlobal

like image 42
navins Avatar answered Sep 19 '22 05:09

navins