Skip to content

Instantly share code, notes, and snippets.

@manicolosi
Created August 21, 2013 02:32
Show Gist options
  • Save manicolosi/6289768 to your computer and use it in GitHub Desktop.
Save manicolosi/6289768 to your computer and use it in GitHub Desktop.
Watch for filesystem events in Clojure using `java.nio.file`
(defn watch-home []
(let [path (java.nio.file.Paths/get "/home/mark" (into-array String []))
watch-service (-> path (.getFileSystem) (.newWatchService))]
(.register path watch-service (into-array java.nio.file.WatchEvent$Kind
[java.nio.file.StandardWatchEventKinds/ENTRY_CREATE
java.nio.file.StandardWatchEventKinds/ENTRY_MODIFY
java.nio.file.StandardWatchEventKinds/ENTRY_DELETE]))
(loop [poll 0]
(if-let [watch-key (.poll watch-service)]
(do
(doseq [event (.pollEvents watch-key)]
(println (str (.kind event) " - " (.context event))))
(.reset watch-key))
(Thread/sleep 1000))
(println (str "Poll #: " poll))
(recur (inc poll)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment