I come from a Spring world where XML, Timers, Jobs and Quartz rule the
world, so am hoping for something small and elegant for Closure :-)
S.
(import '(java.util.concurrent Executors TimeUnit))
(let [s (Executors/newSingleThreadScheduledExecutor)]
(.scheduleAtFixedRate s
#(try
(println "I did it again")
(catch Exception e
(.printStackTrace e)))
(long 0)
(long 1)
TimeUnit/SECONDS))
Admittedly very java-ish.
Albert
--
http://albert.rierol.net
VimClojure relies on Nailgun, with a bunch of people on this list
using it with Clojure every day.
Why not just run an agent that does something, then calls sleep for N
seconds, then calls the next thing?
Granted, it will eat up a thread in your agent thread pool, but if
you've only got one of these in the app it shouldn't be a problem.
Or you could go the Java route, and start a daemon thread manually.
John,Is like I am missing something? This thread mentioned making periodic tasks, there was a java example to it. However, what you posted I am yet to figure out how to make a periodic tasks possible.
Maybe I'm confused, but can't you just do a regular java thread for
this?
(defn periodicly [fun time]
"starts a thread that calls function every time ms"
(let [thread (new Thread (fn [] (loop [] (fun) (Thread/sleep time)
(recur))))]
(.start thread)
thread))
(periodicly #(println "foo foo foo") 3000)
I'm not really sure about the error handling thing, but I guess you
could wrap the function in a try-catch where it reinitialized itself.
(although I guess that could get dangerous if you start throwing a lot
of errors).