调用启动模块时,默认Ebean服务器为null [Play Framework]

问题描述 投票:1回答:1

我正在使用Play Framework 2.5.10和sbt-play-ebean 3.0.0。

我的问题 我需要在应用程序启动时为我的一个模型中的每个对象设置Akka actor。唯一正式的方法是注册启动模块。但有时默认的Ebean服务器在调用启动模块时尚未初始化。

我丑陋的解决方案

启动模块:

public class StartupModule extends AbstractModule implements AkkaGuiceSupport {

  @Override
  protected void configure() {
    bindActor(MainActor.class, "main-actor");
  }
}

MainActor类的构造函数:

@Inject
public MainActor(ActorSystem system) {
  this.system = system;

  boolean ebeanReady = false;
  EbeanServer ebeanServer = null;

  do  {
    try {
      ebeanServer = Ebean.getDefaultServer();
    } catch (PersistenceException e) {
      Logger.error("Ebean not ready!");
    }

    if (ebeanServer != null) {
      ebeanReady = true;
      Logger.info("Ebean ready!");
      Ebean.runCacheWarming();
    }

  } while (!ebeanReady);

  for (Model model : Model.find.all()) {
     foo(model);
  }

}

在没有暴力破解尝试直到Ebean服务器初始化之前,有没有更好的方法来做到这一点?

playframework ebean playframework-2.5 akka-actor
1个回答
1
投票

我遇到了同样的问题,我通过将Ebean Dynamic Evolutions绑定到模块来解决这个问题:

public class StartupModule extends AbstractModule implements AkkaGuiceSupport 
{
    @Override
    protected void configure() {
         bind(DynamicEvolutions.class).to(EbeanDynamicEvolutions.class).asEagerSingleton();
         bindActor(MainActor.class, "main-actor");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.