Does Play! Framework provide a container for objects whose lifetime should be the same as the process?
Play 2.0 has GlobalSettings that provide onStart and onStop, but no apparent "container" for process-lifetime objects.
For Play 1.2.7, I need:
Suggestions?
You can use play jobs with @OnApplicationStart
and @OnApplicationStop
for initialization and cleanup:
http://www.playframework.com/documentation/1.2.7/jobs#anameconceptsBootstrapjobsa
Another way is to write your own plugin (which allows you to hook in into even more play processes like beforeActionInvocation, etc.):
public class ApplicationPlugin extends PlayPlugin {
@Override
public void onApplicationStart() { }
@Override
public void onApplicationStop() { }
@Override
public void beforeInvocation() { }
@Override
public void beforeActionInvocation(Method actionMethod) {
// etc. ...
}
The plugin also needs to be prioritized in the conf/play.plugins
file:
1000:my.java.package.ApplicationPlugin
Depending on your object you want to store, you could put your "process-lifetime objects" into the database or just a HashMap? I was thinking about the cache (http://www.playframework.com/documentation/1.2.7/cache) as well, but I'm not sure if that is the best idea (e.g. because of expiration timeouts).