playframework - Play java 2.5 guice eager loading and stop hook not working -


this code in app/ folder :

 public class guiceconfiguration extends abstractmodule {          @override         protected void configure() {             bind(kafkaconnection.class).aseagersingleton();         }     }   @singleton public class kafkaconnection {      public kafkaconnection(){         try {             serviceutils.startkafka();         } catch (ioexception e) {             playloggerutils.logerror("[global]", this.getclass(), e);         }      }      public kafkaconnection(applicationlifecycle lifecycle) {         lifecycle.addstophook(() -> {             playloggerutils.logdebug("kafka shutting down", this.getclass());             serviceutils.shutdownkafka();             return completablefuture.completedfuture(null);         });      }   } 

in application conf

play.module.enabled += "guiceconfiguration" 

the class being initialized if call inject in controller. not loading @ time of application startup. , stop hook not being executed (tried ctrl+d , kill without force in production mode).

first of all, class should have 1 constructor. if instance created default constructor, not have registered application lifecycle.

further, practice provide interface implementation. kafkaconnection should interface have multiple implementations (allows stub things when necessary).

with respect instance not being loaded eagerly, use .aseagersingleton() in module, @singleton annotation. according guice docs:

                      production    development .aseagersingleton()   eager         eager .in(singleton.class)  eager         lazy .in(scopes.singleton) eager         lazy @singleton            eager         lazy 

@singleton should loaded lazily in development mode. although in production mode, should loaded eagerly.

so working implementation (also on dev environment) this:

public class guiceconfiguration extends abstractmodule {      @override     protected void configure() {         bind(kafkaconnection.class).to(defaultkafkaconnection.class).aseagersingleton();     } }  public interface kafkaconnection {}  // no annotation public class defaultkafkaconnection implements kafkaconnection {      // no default constructor, applicationlifecycle needs     // injected     @inject     public kafkaconnection(applicationlifecycle lifecycle) {         try {             serviceutils.startkafka();         } catch (ioexception e) {             playloggerutils.logerror("[global]", this.getclass(), e);         }         lifecycle.addstophook(() -> {             playloggerutils.logdebug("kafka shutting down", this.getclass());             serviceutils.shutdownkafka();             return completablefuture.completedfuture(null);         });      }  } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -