Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global onStart on Play Framework 2.3.7 not working?

Sorry if this question turns to be silly, but I simply cannot find my mistake and I checked already plenty of posts here in SO and other sites. I have setup a Play 2.3.7 project using Java. I have created a Global.java file in the common package under the app directory. In that file I override onStart (and other hooks) but I don't get them to work. They simply do not execute at all. Here's the Global.java file:

package common;

import play.Application;
import play.GlobalSettings;
import play.Logger;

public class Global extends GlobalSettings {

    @Override
    public void beforeStart(Application application) {
        Logger.error("good bye cruel world");
        super.beforeStart(application);
        throw new RuntimeException("WTF");
    }

    @Override
    public void onStart(Application application) {
        Logger.error("good bye cruel world");
        super.onStart(application);
        throw new RuntimeException("WTF");
    }

    @Override
    public void onStop(Application application) {
        Logger.error("good bye cruel world");
        super.onStop(application);
        throw new RuntimeException("WTF");
    }
}

And inside the application.conf, here's the relevant part, which is commented by default:

# Define the common.Global object class for this application.
# Default to common.Global in the root package.
# application.global=common.Global

What can be the problem? Thanks.

like image 479
ale64bit Avatar asked Dec 25 '14 02:12

ale64bit


2 Answers

It looks like you forgot to uncomment the application.global setting.

The following code worked just fine for me.

Global.java file:

package common;

import play.Application;
import play.GlobalSettings;
import play.Logger;

public class Global extends GlobalSettings {

    @Override
    public void beforeStart(Application application) {
        Logger.error("good bye cruel world");
        super.beforeStart(application);
    }

    @Override
    public void onStart(Application application) {
        Logger.error("good bye cruel world");
        super.onStart(application);
    }

    @Override
    public void onStop(Application application) {
        Logger.error("good bye cruel world");
        super.onStop(application);
    }
}

application.conf file:

# Define the Global object class for this application.
# Default to Global in the root package.
application.global=common.Global
like image 112
MikeSchneeberger Avatar answered Sep 24 '22 03:09

MikeSchneeberger


The Global object must reside in the default package, so you need to remove package common.

As stated in the first paragraph of the documentation.

like image 42
Michael Zajac Avatar answered Sep 23 '22 03:09

Michael Zajac