This code is 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 is being initialized only if I call inject in controller. It is not loading at time of application startup. And the stop hook is also not being executed (tried with ctrl+d and just kill without force in production mode).
First of all, your class should have only one constructor. If an instance is created with the default constructor, it will definitely not have registered to the application lifecycle.
Further, it is good practice to provide an interface with an implementation.
KafkaConnectionshould be an interface which could have multiple implementations (allows you to stub things when necessary).With respect to the instance not being loaded eagerly, you use
.asEagerSingleton()in your module, as well as the@Singletonannotation. According to the Guice docs:@Singleton should be loaded lazily in Development mode. Although in production mode, it should be loaded eagerly.
So a working implementation (also on your dev environment) could look something like this: