mirror of
https://github.com/corda/corda.git
synced 2025-06-13 20:58:19 +00:00
Move ShutdownHook to node-api. (#770)
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user