Permalink
Browse files

Support Joda Time objects in the trigger builder DSL

  • Loading branch information...
michaelklishin committed Apr 24, 2012
1 parent 59f0b0b commit 5e22c4910eb920ac16206115dc0cef64f4c98878
@@ -29,3 +29,27 @@
JobExecutionContext
(from-job-data [^JobExecutionContext input]
(from-job-data (.getMergedJobDataMap input))))
+
+
+(defprotocol DateConversion
+ (to-date [input] "Converts given input to java.util.Date"))
+
+(extend-protocol DateConversion
+ java.util.Date
+ (to-date [input]
+ input)
+
+ ;; common cases
+ org.joda.time.DateTime
+ (to-date [input]
+ (.toDate input))
+ org.joda.time.MutableDateTime
+ (to-date [input]
+ (.toDate input))
+
+ ;; catch-all for Joda Date types convertable to java.util.Date
+ org.joda.time.base.BaseDateTime
+ (to-date [input]
+ (.toDate input)))
+
+
@@ -2,8 +2,8 @@
(:refer-clojure :exclude [key])
(:import [org.quartz Trigger TriggerBuilder TriggerKey ScheduleBuilder]
[org.quartz.utils Key]
- [java.util Date])
- (:use [clojurewerkz.quartzite.conversion :only [to-job-data]]))
+ java.util.Date)
+ (:use [clojurewerkz.quartzite.conversion :only [to-job-data to-date]]))
;;
@@ -58,18 +58,16 @@
(.startNow tb))
-;; multimethods make it possible to support date classes
-;; other than java.util.Date here. Seamless JodaTime integration is one
+;; Seamless JodaTime integration is one
;; of the goals of Quartzite.
-(defmulti start-at (fn [builder date] (type date)))
-(defmethod start-at Date
- [^TriggerBuilder builder ^Date start]
- (.startAt builder start))
-
-(defmulti end-at (fn [builder date] (type date)))
-(defmethod end-at Date
- [^TriggerBuilder builder ^Date end]
- (.endAt builder end))
+(defn start-at
+ [^TriggerBuilder tb date]
+ (.startAt tb (to-date date)))
+
+(defn end-at
+ [^TriggerBuilder tb date]
+ (.endAt tb (to-date date)))
+
(defn for-job
@@ -1,13 +1,14 @@
(ns clojurewerkz.quartzite.test.execution
- (:use [clojure.test]
- [clojurewerkz.quartzite.conversion])
+ (:use clojure.test
+ clojurewerkz.quartzite.conversion
+ [clj-time.core :only [now secs from-now]])
(:require [clojurewerkz.quartzite.scheduler :as sched]
[clojurewerkz.quartzite.jobs :as j]
[clojurewerkz.quartzite.triggers :as t]
[clojurewerkz.quartzite.schedule.simple :as s]
[clojurewerkz.quartzite.schedule.calendar-interval :as calin])
- (:import [java.util.concurrent CountDownLatch]
- [org.quartz.impl.matchers GroupMatcher]))
+ (:import java.util.concurrent.CountDownLatch
+ org.quartz.impl.matchers.GroupMatcher))
(println (str "Using Clojure version " *clojure-version*))
@@ -224,7 +225,7 @@
(j/of-type clojurewerkz.quartzite.test.execution.JobG)
(j/with-identity "clojurewerkz.quartzite.test.execution.job7" "tests"))
trigger (t/build
- (t/start-now)
+ (t/start-at (-> 2 secs from-now))
(t/with-schedule (calin/schedule
(calin/with-interval-in-seconds 2))))]
(is (sched/schedule job trigger))
@@ -234,5 +235,5 @@
(sched/schedule job trigger)))
;; but maybe-schedule will not
(is (not (sched/maybe-schedule job trigger)))
- (Thread/sleep 5000)
+ (Thread/sleep 7000)
(is (= 3 @counter7))))

0 comments on commit 5e22c49

Please sign in to comment.