如何使用Akka在SIGTERM上进行自定义关闭?

时间:2013-02-22 10:48:21

标签: akka

默认情况下,Akka在收到SIGTERM时会关闭actor系统。在关闭akka系统之前,如何覆盖此行为以执行自定义关闭逻辑?我已经在actor中实现了这个逻辑,使用了特殊的优雅停止消息 - 我只需要在收到SIGTERM时调用该逻辑。

或者我是否必须使用其他方式关闭应用程序?这也是一种选择。

2 个答案:

答案 0 :(得分:1)

您可以像这样使用sys.shutdownHookThread

def main(args: Array[String]) {
  ... initialization code ...
  sys.shutdownHookThread {
    println "Shutting down ..."
    shutdownHandler ! DoSomething
  }
}

答案 1 :(得分:0)

这适合:http://doc.akka.io/api/akka/2.1.0/#akka.actor.ActorSystem

abstract def
registerOnTermination(code: Runnable): Unit
Register a block of code (callback) to run after ActorSystem.shutdown has been issued and all actors in this actor system have been stopped. Multiple code blocks may be registered by calling this method multiple times. The callbacks will be run sequentially in reverse order of registration, i.e. last registration is run first.
Exceptions thrown
a
RejectedExecutionException if the System has already shut down or if shutdown has been initiated.


abstract def
registerOnTermination[T](code: ⇒ T): Unit
Register a block of code (callback) to run after ActorSystem.shutdown has been issued and all actors in this actor system have been stopped. Multiple code blocks may be registered by calling this method multiple times. The callbacks will be run sequentially in reverse order of registration, i.e. last registration is run first.
Exceptions thrown
a
RejectedExecutionException if the System has already shut down or if shutdown has been initiated.