Skip to content

Commit

Permalink
# Ebean 3420
Browse files Browse the repository at this point in the history
Create EbeanLifecycle component to manage Ebean shutdown
  • Loading branch information
AntoineDuComptoirDesPharmacies committed Jul 9, 2024
1 parent 52130ae commit d61f503
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
7 changes: 5 additions & 2 deletions play-ebean/src/main/java/play/db/ebean/EBeanComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
public interface EBeanComponents extends ConfigurationComponents, DBComponents {

default DynamicEvolutions dynamicEvolutions() {
return new EbeanDynamicEvolutions(
ebeanConfig(), environment(), applicationLifecycle(), evolutionsConfig());
return new EbeanDynamicEvolutions(ebeanConfig(), environment(), evolutionsConfig());
}

default EbeanConfig ebeanConfig() {
return new DefaultEbeanConfig.EbeanConfigParser(config(), environment(), dbApi()).get();
}

default EbeanLifecycle ebeanLifecycle() {
return new EbeanLifecycle(applicationLifecycle());
}

default EvolutionsConfig evolutionsConfig() {
return new DefaultEvolutionsConfigParser(configuration()).parse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import javax.inject.Inject;
import javax.inject.Singleton;
import play.Environment;
import play.api.db.evolutions.DynamicEvolutions;
import play.api.db.evolutions.Evolutions$;
import play.api.db.evolutions.EvolutionsConfig;
import play.inject.ApplicationLifecycle;

/** A Play module that automatically manages Ebean configuration. */
@Singleton
Expand All @@ -36,19 +34,11 @@ public class EbeanDynamicEvolutions extends DynamicEvolutions {

@Inject
public EbeanDynamicEvolutions(
EbeanConfig config,
Environment environment,
ApplicationLifecycle lifecycle,
EvolutionsConfig evolutionsConfig) {
EbeanConfig config, Environment environment, EvolutionsConfig evolutionsConfig) {
this.config = config;
this.environment = environment;
this.evolutionsConfig = evolutionsConfig;
start();
lifecycle.addStopHook(
() -> {
databases.forEach((key, database) -> database.shutdown(false, false));
return CompletableFuture.completedFuture(null);
});
}

/** Initialise the Ebean servers/databases. */
Expand Down
26 changes: 26 additions & 0 deletions play-ebean/src/main/java/play/db/ebean/EbeanLifecycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package play.db.ebean;

import io.ebean.event.ShutdownManager;
import java.util.concurrent.CompletableFuture;
import javax.inject.Inject;
import javax.inject.Singleton;
import play.api.db.evolutions.DynamicEvolutions;
import play.inject.ApplicationLifecycle;

/** A Play module that automatically manages Ebean configuration. */
@Singleton
public class EbeanLifecycle extends DynamicEvolutions {
@Inject
public EbeanLifecycle(ApplicationLifecycle lifecycle) {
ShutdownManager.deregisterShutdownHook();
lifecycle.addStopHook(
() -> {
ShutdownManager.shutdown();
return CompletableFuture.completedFuture(null);
});
}
}
3 changes: 2 additions & 1 deletion play-ebean/src/main/java/play/db/ebean/EbeanModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class EbeanModule extends Module {
public Seq<Binding<?>> bindings(Environment environment, Configuration configuration) {
return seq(
bind(DynamicEvolutions.class).to(EbeanDynamicEvolutions.class).eagerly(),
bind(EbeanConfig.class).toProvider(DefaultEbeanConfig.EbeanConfigParser.class).eagerly());
bind(EbeanConfig.class).toProvider(DefaultEbeanConfig.EbeanConfigParser.class).eagerly(),
bind(EbeanLifecycle.class).toSelf().eagerly());
}
}

0 comments on commit d61f503

Please sign in to comment.