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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With