SHARE
TWEET

quartz in clojure

a guest Aug 28th, 2013 34 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; file quartz.clj
  2. (ns d.quartz
  3.   (:require [clojure.tools.logging :as log]
  4.             [clj-time.core :refer [now]]
  5.             [clj-time.format :refer [formatters unparse]])
  6.   (:import
  7.    [org.quartz CronScheduleBuilder DateBuilder JobDetail Scheduler Trigger JobBuilder TriggerBuilder Job]
  8.    [org.quartz.impl StdSchedulerFactory]
  9.    [org.quartz.impl.triggers CronTriggerImpl]
  10.    [java.util TimeZone Locale]))
  11.  
  12. (def job-class "d.gen.SomeJob")
  13.  
  14. (gen-class
  15.  :name "d.gen.SomeJob"
  16.  :implements ["org.quartz.Job"]
  17.  :prefix "job-"
  18.  :main false)
  19.  
  20. (defn job-execute [x y]
  21.   (log/info "hello from job"))
  22.  
  23.  
  24. ;; file core.clj
  25. (ns d.core
  26.   (:require [d.quartz :refer [job-class state job-execute]]
  27.             [clojure.tools.logging :as log]
  28.             [clojure.edn :as edn]
  29.             [clojure.java.io :as io]
  30.             [cheshire.core :as json]
  31.             [clojure.string :refer [trim]])
  32.   (:import
  33.    [org.quartz CronScheduleBuilder DateBuilder JobDetail Scheduler Trigger JobBuilder TriggerBuilder Job JobKey]
  34.    [org.quartz.impl StdSchedulerFactory]
  35.    [org.quartz.impl.triggers CronTriggerImpl]
  36.    [java.util TimeZone Locale Date]))
  37.  
  38. (gen-class
  39.  :name "d.main"
  40.  :prefix "main-"
  41.  :main true)
  42.  
  43. (def job-group "group0")
  44.  
  45. (defn -main [& args]
  46.   (let [scheduler (StdSchedulerFactory/getDefaultScheduler)
  47.             job-key (JobKey. "cron-job" job-group)
  48.             job (-> (JobBuilder/newJob
  49.                      (Class/forName job-class))
  50.                     .storeDurably
  51.                     (.withIdentity job-key)
  52.                     .build)]
  53.         (doto scheduler
  54.           (.addJob job false)
  55.           .start)
  56.         (.scheduleJob scheduler
  57.                       (-> (TriggerBuilder/newTrigger)
  58.                           (.forJob job)
  59.                           (.withIdentity "cron-trigger" job-group)
  60.                           (.startAt (Date. (+ (System/currentTimeMillis) (* 1000 30))))
  61.                           (.withSchedule
  62.                            (CronScheduleBuilder/cronSchedule
  63.                             (:cron files)))
  64.                           .build))))
  65.  
  66. ;; project.clj needs these lines:
  67. ;; :aot [d.quartz d.main]
  68. ;; [org.quartz-scheduler/quartz "2.2.0"]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top