onstart method of Global.java getting executed twi

2019-09-17 01:55发布

问题:

I have just upgraded my application from play framework 2.3.9 to 2.4.6. Everything is working fine, but onstart(Application app) method getting executed twice. As i have created some scheduler in the onstart method, they are also getting executed twice.

Global.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 file is as below

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 is as below:

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", "***", "***", "****")

Note: I am using jpa in the project. Any help would be highly appreciated.

回答1:

I had the same issue on production, not on dev mode.

I was running production on heroku on multiple dynos and the Global.java (and my background jobs triggered from Global.java) were executed as many times as my number of dynos.

Here is an answer on how to execute it only once: Play 2 Heroku startup with multiple dynos