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

Playframework 调用启动模块[Play Framework]时,默认Ebean服务器为空,playframework,ebean,playframework-2.5,akka-actor,Playframework,Ebean,Playframework 2.5,Akka Actor,我在sbt Play ebean 3.0.0中使用Play Framework 2.5.10 我的问题 在应用程序启动时,我需要为我的模型中的每个对象设置Akka角色。唯一的官方方法是注册一个启动模块。但有时在调用启动模块时,默认的Ebean服务器尚未初始化 我丑陋的解决方案 启动模块: public class StartupModule extends AbstractModule implements AkkaGuiceSupport { @Override protected

我在sbt Play ebean 3.0.0中使用Play Framework 2.5.10

我的问题
在应用程序启动时,我需要为我的模型中的每个对象设置Akka角色。唯一的官方方法是注册一个启动模块。但有时在调用启动模块时,默认的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服务器初始化之前,有没有更好的方法不用暴力尝试就可以做到这一点?

我遇到了同样的问题,我通过将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");
    }
}

也许你可以试着在你的模块上增加一个延迟来使用Ebean。看看akka调度程序。我有一个类似的问题,我通过这样做解决了它。@pedroct92我不想只是延迟MainActor.class的构造,我想100%确定Ebean.default存在:-)。现在,我将它封装在一个线程中,每次尝试后都会有1秒的睡眠时间。