Created
August 21, 2013 02:32
-
-
Save manicolosi/6289768 to your computer and use it in GitHub Desktop.
Watch for filesystem events in Clojure using `java.nio.file`
This file contains 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
(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