播放应用程序生命周期
Play application lifecycle
最近我将使用 Akka Actors 的 Play 应用程序更改为 classic Scala 应用程序。使用 Play,我会像这样覆盖应用程序的全局设置:
import play.api._
import play.api.Play.current
object Global extends GlobalSettings {
var system: ActorSystem = _
override def onStart(app: Application) : Unit = {
super.onStart(app)
Logger.info("onStart")
system = ActorSystem("SysActor")
override def onStop(app: Application) : Unit = {
super.onStop(app)
if (system != null) {
system.terminate
Logger.info(system + " shutdown !")
}
}
}
在 classic scala 应用程序中,我定义了一个要在 sbt run
命令处执行的主要 class 但它是一种检测方法吗,比如 Play Scala
, 运行 应用程序的关闭或停止?请注意,我使用 Docker 在 Amazon EC2 上发布了该应用程序。
一种方法是注册一个关闭挂钩,例如:
object MyApp {
val system = ActorSystem("SysActor") // instantiation can be moved to main
private val t = new Thread {
override def run() = {
if (system != null) {
system.terminate
Logger.info(system + " shutdown !")
}
}
}
Runtime.getRuntime.addShutdownHook(t)
def main(args: Array[String]): Unit = { ... }
}
当 JVM 收到关闭信号时调用关闭挂钩(但请注意它不是 guaranteed,具体取决于信号)
最近我将使用 Akka Actors 的 Play 应用程序更改为 classic Scala 应用程序。使用 Play,我会像这样覆盖应用程序的全局设置:
import play.api._
import play.api.Play.current
object Global extends GlobalSettings {
var system: ActorSystem = _
override def onStart(app: Application) : Unit = {
super.onStart(app)
Logger.info("onStart")
system = ActorSystem("SysActor")
override def onStop(app: Application) : Unit = {
super.onStop(app)
if (system != null) {
system.terminate
Logger.info(system + " shutdown !")
}
}
}
在 classic scala 应用程序中,我定义了一个要在 sbt run
命令处执行的主要 class 但它是一种检测方法吗,比如 Play Scala
, 运行 应用程序的关闭或停止?请注意,我使用 Docker 在 Amazon EC2 上发布了该应用程序。
一种方法是注册一个关闭挂钩,例如:
object MyApp {
val system = ActorSystem("SysActor") // instantiation can be moved to main
private val t = new Thread {
override def run() = {
if (system != null) {
system.terminate
Logger.info(system + " shutdown !")
}
}
}
Runtime.getRuntime.addShutdownHook(t)
def main(args: Array[String]): Unit = { ... }
}
当 JVM 收到关闭信号时调用关闭挂钩(但请注意它不是 guaranteed,具体取决于信号)