我刚刚将我的应用程序从play framework 2.3.9升级到2.4.6。一切都运行正常,但启动两次的启动(应用程序应用程序)方法。由于我在onstart方法中创建了一些调度程序,因此它们也会被执行两次。
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文件如下
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。任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
我在制作时遇到了同样的问题,而不是开发模式。
我在heroku上运行多个dynos上的生产,Global.java(以及从Global.java触发的后台作业)被执行的次数与我的dynos数量相同。
以下是关于如何只执行一次的答案: Play 2 Heroku startup with multiple dynos