I want to start a server with clojure boot that must keep on running. So I made a -main
function so that I can run ./build.boot
and that will execute the main function. In the main function I start a non-daemon thread with the idea that the JVM keeps on running as long as there is a non-daemon thread. However the boot script stops and my server stops as well. What am I doing wrong?
#!/usr/bin/env boot
(defn -main [& args]
(let [t (Thread. #(loop []
(println (java.util.Date.))
(Thread/sleep 500)
(recur)))]
(.setDaemon t false)
(.start t)
(Thread/sleep 3000)
(println "I have started a non-daemon thread, but still I exit???")))
Output
#inst "2016-06-23T08:39:23.453-00:00"
#inst "2016-06-23T08:39:24.005-00:00"
#inst "2016-06-23T08:39:24.507-00:00"
#inst "2016-06-23T08:39:25.009-00:00"
#inst "2016-06-23T08:39:25.511-00:00"
#inst "2016-06-23T08:39:26.013-00:00"
I have started a non-daemon thread, but still I exit???
After which the script ends.
I guess boot calls System/exit
once the tasks are done (their function call ends).
You need to modify your task to join
to your spawned thread so the -main
function doesn't complete until your spawned thread hasn't finished:
(.join t)