1 | ;; goals with my evaluation of Quartz are: |
2 | ;; |
3 | ;; * create a simple example of creating and scheduling |
4 | ;; an in-process job (print the current time?) |
5 | ;; * make a 'clojure' job? |
6 | ;; * examples of all of the scheduling approaches |
7 | ;; * 'in 10 minutes' |
8 | ;; * repeat / recurrance |
9 | ;; * 'at 8am every monday' (Cron scheduler) |
10 | ;; * exampls of using persistence for registered jobs |
11 | ;; * demonstration of what happens if the scheudler is |
12 | ;; down during the schedule time - how does it deal |
13 | ;; with this, does it re-execute pending jobs that |
14 | ;; missed their invocation points? Or does that need |
15 | ;; to be implemented at an application level? |
16 | ;; * create a job that sends an AMQP message to a broker |
17 | ;; allowing the scheduler to be an abstracted service |
18 | ;; invoker |
19 | ;; * make a registry for these JMS OR AMQP jobs supporting |
20 | ;; the above behaviors for robustness |
21 | ;; * how do you do HA clustering for quartz? |
22 | ;; |
23 |
|
24 | (ns com.github.kyleburton.sandbox.quartz |
25 | (:import (org.quartz SchedulerFactory Scheduler TriggerUtils JobDetail) |
26 | (org.quartz.impl StdSchedulerFactory) |
27 | (com.github.kyleburton.sandbox.quartz ClojureJob))) |
28 |
|
29 | (def *schedule-factory* (StdSchedulerFactory.)) |
30 |
|
31 | (def *scheduler* (atom nil)) |
32 |
|
33 | (defn ensure-scheduler-started [] |
34 | (if (or (not @*scheduler*) |
35 | (.isShutdown @*scheduler*) |
36 | (not (.isStarted @*scheduler*))) |
37 | (do |
38 | (reset! *scheduler* (.getScheduler *schedule-factory*)) |
39 | (.start @*scheduler*) |
40 | true) |
41 | nil)) |
42 |
|
43 | (defn stop-scheduler [] |
44 | (if (and @*scheduler* |
45 | (.isStarted @*scheduler*)) |
46 | (.shutdown @*scheduler*))) |
47 |
|
48 | (defn schedule-job [job-detail trigger] |
49 | (ensure-scheduler-started) |
50 | (.scheduleJob @*scheduler* job-detail trigger)) |
51 |
|
52 | (defn delete-job [job-detail] |
53 | (.deleteJob @*scheduler* |
54 | (.getName job-detail) |
55 | (.getGroup job-detail))) |
56 |
|
57 | (defn job-exists? [job-detail] |
58 | (not (nil? (.getJobDetail @*scheduler* |
59 | (.getName job-detail) |
60 | (.getGroup job-detail))))) |
61 |
|
62 | (defn testfn [context] |
63 | (prn (format "testfn: context=%s time=%s" |
64 | context |
65 | (java.util.Date.)))) |
66 |
|
67 | (defn quartz-test [] |
68 | (let [job-detail (JobDetail. "myJob" nil ClojureJob) |
69 | trigger (doto (TriggerUtils/makeSecondlyTrigger 10) |
70 | (.setStartTime (TriggerUtils/getEvenSecondDate (java.util.Date.))) |
71 | (.setName "My Second Trigger"))] |
72 | (.put (.getJobDataMap job-detail) ClojureJob/NAMESPACE_PARAMETER "com.github.kyleburton.sandbox.quartz") |
73 | (.put (.getJobDataMap job-detail) ClojureJob/FUNCTION_NAME_PARAMETER "testfn") |
74 | (schedule-job job-detail trigger))) |
75 |
|
76 | (defn quartz-test-fn [fn] |
77 | (let [job-detail (JobDetail. "myJob" nil ClojureJob) |
78 | trigger (doto (TriggerUtils/makeSecondlyTrigger 10) |
79 | (.setStartTime (TriggerUtils/getEvenSecondDate (java.util.Date.))) |
80 | (.setName "My Second Trigger"))] |
81 | (.put (.getJobDataMap job-detail) ClojureJob/NAMESPACE_PARAMETER "com.github.kyleburton.sandbox.quartz") |
82 | (.put (.getJobDataMap job-detail) ClojureJob/FUNCTION_PARAMETER fn) |
83 | (schedule-job job-detail trigger))) |
84 |
|
85 | ;; (quartz-test) |
86 |
|
87 | ;; (def *count* (atom 0)) |
88 |
|
89 | ;; (quartz-test-fn (fn [context] |
90 | ;; (reset! *count* (inc @*count*)) |
91 | ;; (prn (format "anon scheduled function! context=%s called %d times!" context @*count*)))) |
92 |
|
93 | ;; (stop-scheduler) |