Archief

Dev

PlayFramework + GAE OnApplicationStart code

Google App Engine works in mysterious ways! The default @OnApplicationStart doJob of play framework apparantly doesn’t work. But there’s a workaround if you want to have code executed when your application is booted. For instance, to inject bootstrap data in your database.

The solution I found makes use of the Plugins functionality of play. A plugin can execute code on application start, exactly what we need!

So here’s my code to create a one-class-plugin:

package plugins;

import play.*;
import play.Play.Mode;
import play.test.Fixtures;

public class Bootstrap extends PlayPlugin {

  @Override
  public void onApplicationStart()
  {
    if (Play.mode == Mode.DEV)
    {
      Logger.info(“Loading data………….”);
      Fixtures.deleteAllModels();
      Fixtures.loadModels(“data.yml”);
    }
    Logger.info(“App loaded!”);
  }

}

And then one little extra: put a “play.plugins” file in your /app directory with the following contents:

4242:plugins.Bootstrap

This registers the plugin with a priority of 4242 with Play. SpringPlugin, for instance, has a priority of 1000.

That’s it! No ugly url’s to call after a deploy, this code gets executed automagically.

#gae #playframework #plugin