Move ShutdownHook to node-api. (#770)

This commit is contained in:
Andrzej Cichocki
2017-06-02 15:32:20 +01:00
committed by GitHub
parent 6104b107c0
commit c510e67ed5
6 changed files with 9 additions and 8 deletions

View File

@ -0,0 +1,26 @@
package net.corda.nodeapi.internal
interface ShutdownHook {
/**
* Safe to call from the block passed into [addShutdownHook].
*/
fun cancel()
}
/**
* The given block will run on most kinds of termination including SIGTERM, but not on SIGKILL.
* @return An object via which you can cancel the hook.
*/
fun addShutdownHook(block: () -> Unit): ShutdownHook {
val hook = Thread { block() }
val runtime = Runtime.getRuntime()
runtime.addShutdownHook(hook)
return object : ShutdownHook {
override fun cancel() {
// Allow the block to call cancel without causing IllegalStateException in the shutdown case:
if (Thread.currentThread() != hook) {
runtime.removeShutdownHook(hook)
}
}
}
}