Skip to content

Commit

Permalink
Scheduled Thread Executor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsz committed Jul 29, 2020
1 parent a031e9b commit 5847c71
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.org
@@ -1,9 +1,11 @@

* Changes
** 0.4.6-SNAPSHOT
- Benjamin: ability to wrap component for both persistence-fn and logbook-fn independently.
- Mariadb: database connection component
- Next.jdbc: database connection component
- Defensive measure in durable queue component
- Scheduled Executor Service: added one-off method for non-repeating tasks
** 0.4.5
- LDAP component (UnboundID LDAP SDK for Java)
- New Redis queue component
Expand Down
3 changes: 2 additions & 1 deletion src/system/components/scheduled_executor_service.clj
Expand Up @@ -10,7 +10,8 @@
(doseq [x xs]
(case (:method x)
:fixed-delay (.scheduleWithFixedDelay ^ScheduledThreadPoolExecutor s ((:f x) component) (:initial-delay x) (:period x) (:unit x))
:fixed-rate (.scheduleAtFixedRate ^ScheduledThreadPoolExecutor s ((:f x) component) (:initial-delay x) (:period x) (:unit x))))
:fixed-rate (.scheduleAtFixedRate ^ScheduledThreadPoolExecutor s ((:f x) component) (:initial-delay x) (:period x) (:unit x))
:one-off (.schedule ^ScheduledThreadPoolExecutor s ((:f x) component) (:initial-delay x) (:unit x))))
(assoc component :scheduler s)))
(stop [component]
(when-let [scheduler (:scheduler component)]
Expand Down
12 changes: 7 additions & 5 deletions test/system/components/scheduled_executor_service_test.clj
Expand Up @@ -5,22 +5,24 @@
(system.monitoring scheduled-executor-service
[core :refer [started? stopped?]])
[clojure.test :refer [deftest is]])
(:import [java.util.concurrent TimeUnit]))
(:import [java.util.concurrent TimeUnit ScheduledThreadPoolExecutor]))

(def workers [{:f (fn [component] #(println (str "fixed-rate " (:n-threads component)))) :initial-delay 0 :period 1 :unit TimeUnit/MINUTES :method :fixed-rate}
{:f (fn [component] #(println (str "fixed-delay " (:n-threads component)))) :initial-delay 0 :period 1 :unit TimeUnit/MINUTES :method :fixed-delay}
{:f (fn [component] #(println (str "one-off " (:n-threads component)))) :initial-delay 0 :unit TimeUnit/MINUTES :method :one-off}])

(def workers [{:f (fn [component] #(println "Hello, World. " (:n-threads component))) :initial-delay 0 :period 1 :unit TimeUnit/MINUTES :method :fixed-rate}
{:f (fn [component] #(println "Hello, Worlds. " (:n-threads component))) :initial-delay 0 :period 1 :unit TimeUnit/MINUTES :method :fixed-delay}])
(def scheduler (new-scheduler :xs workers))
(def scheduler-with-n-threads (new-scheduler :xs workers :n-threads (+ 2 (.availableProcessors (Runtime/getRuntime)))))

(deftest scheduled-executor-service-test
(alter-var-root #'scheduler component/start)
(is (= java.util.concurrent.ScheduledThreadPoolExecutor (type (:scheduler scheduler))) "the scheduler is running")
(is (= ScheduledThreadPoolExecutor (type (:scheduler scheduler))) "the scheduler is running")
(alter-var-root #'scheduler component/stop)
(is (.isShutdown (:scheduler scheduler)) "the scheduler is stopped"))

(deftest scheduled-executor-service-with-threads-test
(alter-var-root #'scheduler-with-n-threads component/start)
(is (= java.util.concurrent.ScheduledThreadPoolExecutor (type (:scheduler scheduler-with-n-threads))) "the scheduler is running")
(is (= ScheduledThreadPoolExecutor (type (:scheduler scheduler-with-n-threads))) "the scheduler is running")
(alter-var-root #'scheduler-with-n-threads component/stop)
(is (.isShutdown (:scheduler scheduler-with-n-threads)) "the scheduler is stopped"))

Expand Down

0 comments on commit 5847c71

Please sign in to comment.