Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make the api more convenient.

  • Loading branch information...
commit 240e8a662a7ba06902a2ecfc80019fe2ec5b9c8f 1 parent f7c6705
@Ruiyun authored
View
2  project.clj
@@ -2,4 +2,4 @@
:description "An easy to use Java Timer wrapper for clojure."
:dependencies [[org.clojure/clojure "1.4.0"]]
:dev-dependencies [[lein-autodoc "0.9.0"]]
- :autodoc {:name "ruiyun.tools.timer", :page-title "ruiyun.tools.timer API Documentation"})
+ :autodoc {:name "tools.timer", :page-title "ruiyun.tools.timer API Documentation"})
View
112 src/ruiyun/tools/timer/core.clj
@@ -1,72 +1,82 @@
-(ns ^{:doc "place doc string here"
+(ns ^{:doc "ruiyun.tools.timer is an easy to use Java Timer wrapper for clojure.
+ You can simply start a period task with every 5 seconds immediately like this:
+
+ (run-task! #(println \"Say hello every 5 seconds.\") :period 5000)
+
+ If you want delay the first run with 2 seconds:
+
+ (run-task! #(println \"Say hello after 2 seconds.\") :dealy 2000)
+
+ Use this if you want to execute a task at an absolute time:
+
+ (run-task! #(println \"Say hello at 2013-01-01T00:00:00 in beijing.\") :at #inst \"2013-01-01T00:00:00+08:00\")
+
+ And, you can use the same timer in more than one tasks:
+
+ (def greeting-timer (timer \"The timer for greeting\"))
+ (run-task! #(println \"Say hello after 2 seconds.\") :dealy 2000 :by greeting-timer)
+ (run-task! #(println \"Say hello every 5 seconds.\") :period 5000 :by greeting-timer)
+
+ Finally, you can cancel a timer's tasks:
+
+ (cancel! greeting-timer)"
:author "ruiyun"}
ruiyun.tools.timer.core
- (:use cljain.tools.predicate)
(:import [java.util Timer TimerTask Date]))
(defn timer
"Create a new java.util.Timer object."
- {:added "0.2.0"}
+ {:added "1.0.0"}
([] (Timer.))
([name] (Timer. name)))
(defn deamon-timer
"Create a new java.util.Timer object with deamon option."
- {:added "0.2.0"}
- []
- (Timer. true))
-
-(defmacro task
- "Create a java.util.TimerTask object with some code."
- {:arglists '([body*])
- :added "0.2.0"}
- [& body]
- `(proxy [TimerTask] []
- (run []
- ~@body)))
-
-(defmulti run!
- "Execute a timer task, then return the timer user passed or created auto.
- User must set one of the two options:
+ {:added "1.0.0"}
+ ([] (Timer. true))
+ ([name] (Timer. name true)))
+
+(defn run-task!
+ "Execute a timer task, then return the timer user passed or be auto created.
+ Normally, User need set one of the two options:
:at <time>
:delay <milliseconds>
+ If set none of them, the task will launch immediately.
- Optional, user can set
- :period <milliseconds>"
- {:arglists '([timer? task & options])
- :added "0.2.0"}
- (fn [a & more] (class a)))
-
-(defn- run-task!
- "The 'run!' functions internal implement."
- {:added "0.2.0"}
- [timer task & {:keys [at delay period]}]
- {:pre [(or (instance? Date at) (>= delay 0))
- (or (nil? period) (> period 0))]
- :post [(instance? Timer %)]}
- (let [start-time at]
- (cond
- (not (nil? start-time))
- (if (nil? period)
- (.schedule timer task start-time)
- (.schedule timer task start-time period))
-
- (not (nil? delay))
- (if (nil? period)
- (.schedule timer task delay)
- (.schedule timer task delay period)))
- timer))
+ Optional, user can set:
+ :period <milliseconds>
-(defmethod run! Timer
- [timer task & options]
- (apply run-task! timer task options))
+ If user want share a timer between tow or more tasks, he can set an exist timer:
+ :by <a timer>
-(defmethod run! TimerTask
- [task & options]
- (apply run-task! (timer) task options))
+ Sometimes user task may cause exception, it's a good reason to use an exception handler:
+ :on-exception <handle function with an exception argument>"
+ {:added "1.0.0"}
+ [task & {:keys [by at delay period on-exception]}]
+ {:pre [(fn? task)
+ (or (nil? by) (instance? Timer by))
+ (or (nil? at) (instance? Date at))
+ (or (nil? delay) (>= delay 0))
+ (or (nil? period) (> period 0))
+ (or (nil? on-exception) (fn? on-exception))]
+ :post [(instance? Timer %)]}
+ (let [task (proxy [TimerTask] []
+ (run []
+ (if (nil? on-exception)
+ (task)
+ (try
+ (task)
+ (catch Exception e
+ (on-exception e))))))
+ timer (or by (timer))
+ start-time (or at delay 0)]
+ (if (nil? period)
+ (.schedule timer task start-time)
+ (.schedule timer task start-time period))
+ timer))
(defn cancel!
"Terminates a timer, discarding any currently scheduled tasks."
- {:added "0.2.0"}
+ {:added "1.0.0"}
[timer]
(.cancel timer))
View
6 test/ruiyun/tools/timer/test/core.clj
@@ -1,6 +0,0 @@
-(ns ruiyun.tools.timer.test.core
- (:use [ruiyun.tools.timer.core])
- (:use [clojure.test]))
-
-(deftest replace-me ;; FIXME: write
- (is false "No tests have been written."))
Please sign in to comment.
Something went wrong with that request. Please try again.