im.yagni.common.OnShutdown.scala Maven / Gradle / Ivy
The newest version!
package im.yagni.common
import collection.mutable.ListBuffer
case class ShutdownFunction(message: String, function: () => Unit)
object OnShutdown {
private val shutdownFunctions = new ListBuffer[ShutdownFunction]
def execute(message: String, function: () => Unit) {
shutdownFunctions.append(new ShutdownFunction(message, function))
}
Runtime.getRuntime addShutdownHook new Thread {
override def run() {
println("### Executing shutdowns: " + shutdownFunctions.map(_.message).mkString(", ") )
shutdownFunctions.foreach(f => {
try { f.function() }
finally { println("### Finished shutdown: " + f.message) }
}
)}
}
}