Need help with eager instantiation of singletons


(rapido sai) #1

Hi
I need some help with eager instantiation of singletons, I have tried most of the advice from internet but still cant get it to work, not sure what I am doing wrong.
I need instatantiation after the app starts and before any requests arrive, its because the app serves request every hour or so , but the metrics module I instantiate should send the jvm metrics after starting the server. The app may not get requests till a hour or so.
I tried this:

package modules;
import stuff;
public class MetricRegistryModule extends AbstractModule {

     protected void configure() {
	   bind(AppStartup.class).asEagerSingleton();
     }
}

package modules;
import stuff;
class AppStartup{
     public AppStartup(){
        Logger.debug("  should load before requests come,
                 but this gets printed only after the first request ");
     }
}

//in application.conf
play.modules.enabled += "modules.MetricRegistryModule"

and then wrote instatiation of metrics moduels in AppStartup constructor, it still this waits for the first requests and loads the module.

I also tried changing the mode from production to dev , prod , they did not work either.I am not sure if I am doing something wrong here.


application.mode=prod
%prod.application.mode=prod

application.mode=dev
%prod.application.mode=prod


Also I am using gradle as there is no choice, because the build script is too complicated for me to edit without breaking or changing entire thing to sbt, I haven’t tried sbt. But I guess the build tool should not be a problem.

Is there something new in this version of play I need to be aware of.


(Schmitt Christian) #2

this only happens on prod mode. adding application.mode=prod in Development mode (i.e. calling run) won’t change the mode.
You need to run sbt runProd which will run the production mode of play and eagerly initialize your stuff.


(rapido sai) #3

Is there any way for me to make it work in dev mode.


(rapido sai) #4

Thanks for answering, really appreciate it.


(rapido sai) #5

The reason I ask for running in dev is I am trying to integrate into an existing product and I am not sure I can change the build script and deply script, if I were to change the build scripts ,it brings in whole other set of problems, given I dont have access to people who wrote the scripts.


#6

I just stumbled upon this and don’t fully understand, but maybe this is what you need? https://www.playframework.com/documentation/2.6.x/Deploying#running-a-production-server-in-place

It basically simulates prod, locally. So, easy to quickly test how prod behaves on your dev machine.