java的onstart方法执行了两次

java的onstart方法执行了两次

本文介绍了将playframework升级到2.4.6后,Global.java的onstart方法执行了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的应用程序从播放框架2.3.9升级到了2.4.6.一切工作正常,但是onstart(Application app)方法执行了两次.当我在onstart方法中创建了一些调度程序后,它们也被执行了两次.

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

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文件如下

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如下:

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

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

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

推荐答案

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

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

我正在多个dyno上在heroku上运行生产,并且执行了Global.java(以及从Global.java触发的后台作业)与我的dyno一样多.

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.

以下是有关如何仅执行一次的答案:播放具有多个测功的2 Heroku启动

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

这篇关于将playframework升级到2.4.6后,Global.java的onstart方法执行了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:12