Last active
August 4, 2025 17:45
-
-
Save wdhowe/5477a35ceb89cdadd36be673861f0a65 to your computer and use it in GitHub Desktop.
Clojure shutdown hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns myapp.shutdown-hook | |
| (:gen-class)) | |
| (defn add-shutdown-hook! | |
| "Adds a shutdown hook that executes the given function when the JVM is shutting down." | |
| [f] | |
| (.addShutdownHook | |
| (Runtime/getRuntime) | |
| (new Thread ^Runnable f))) | |
| ;; Usage | |
| (defn -main [& _] | |
| (add-shutdown-hook! | |
| (fn [] | |
| (println "Shutting down cleanly..."))) | |
| (println "Application running. Press Ctrl+C to stop.") | |
| (Thread/sleep 99999999)) ; simulate long-running app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment