1

我刚刚将我的应用程序从 play framework 2.3.9 升级到了 2.4.6。一切正常,但 onstart(Application app) 方法被执行了两次。由于我在 onstart 方法中创建了一些调度程序,它们也被执行了两次。

全球.java

public class Global extends GlobalSettings {

public void onStart(Application app) {
    Logger.info("Application has started");
    JPA.withTransaction(() -> {
        if (ConfigHelper.getGlobalValue("install").equalsIgnoreCase("xyz")) {
            Logger.info("Starting pqr scheduler");
            ActorRef myActor = Akka.system().actorOf(
                    Props.create(PQR.class));
            FiniteDuration delay = FiniteDuration.create(0, TimeUnit.SECONDS);
            FiniteDuration frequency = FiniteDuration.create(10, TimeUnit.MINUTES);
            Akka.system()
                    .scheduler()
                    .schedule(delay, frequency, myActor, "start", Akka.system().dispatcher(), myActor);
        }

            });
}


public void onStop(Application app) {
    Logger.info("Application shutdown...");
}


}

plugin.sbt 文件如下

logLevel := Level.Warn


resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.6")
addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.6")

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "2.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")


addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.1")

addSbtPlugin("net.ground5hark.sbt" % "sbt-css-compress" % "0.1.3")

addSbtPlugin("net.ground5hark.sbt" % "sbt-closure" % "0.1.3")

addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-gzip" % "1.0.0")

build.sbt 如下:

name := "project name"

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  javaJdbc, javaJpa, cache, javaWs,
  "org.hibernate" % "hibernate-entitymanager" % "4.3.9.Final",
  "mysql" % "mysql-connector-java" % "5.1.35",
  "com.amazonaws" % "aws-java-sdk-ses" % "1.9.38",
  "com.amazonaws" % "aws-java-sdk-s3" % "1.9.38",
  "org.freemarker" % "freemarker" % "2.3.22"
)

resolvers += "Sonatype" at "url"

credentials += Credentials("Repository Manager", "***", "***", "****")

注意:我在项目中使用 jpa。任何帮助将不胜感激。

4

1 回答 1

0

我在生产上遇到了同样的问题,而不是在开发模式下。

我在多个 dyno 上的 heroku 上运行生产,而 Global.java(以及我从 Global.java 触发的后台作业)的执行次数与我的 dyno 数量一样多。

这是关于如何只执行一次的答案: Play 2 Heroku startup with multiple dynos

于 2018-01-12T09:01:24.160 回答