How to execute custom SIGTERM closure using Akka?

By default, Akka disables the actor system (s) when it receives SIGTERM . How can I override this behavior to execute my custom shutdown logic before shutting down akka system? I already have this logic implemented in actors using special graceful stop messages - I just need to call this logic when SIGTERM received.

Or do I need to use another way to disable the application? This is also an option.

+6
source share
2 answers

You can simply use sys.shutdownHookThread as follows:

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

Will it match: 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.


0
source

All Articles