Question

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:

  1. an onStart hook to initialize some resources
  2. an onStop hook to clean up the resources
  3. a container to manage process-lifetime objects

Suggestions?

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top