Skip to content

Commit

Permalink
added id-timer abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Marz committed Dec 8, 2011
1 parent 1f39064 commit 720901f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/clj/backtype/storm/util.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:import [java.util Map List Collection]) (:import [java.util Map List Collection])
(:import [java.io FileReader]) (:import [java.io FileReader])
(:import [backtype.storm Config]) (:import [backtype.storm Config])
(:import [backtype.storm.utils Time Container]) (:import [backtype.storm.utils Time Container ClojureTimerTask])
(:import [java.util UUID]) (:import [java.util UUID])
(:import [java.util.concurrent.locks ReentrantReadWriteLock]) (:import [java.util.concurrent.locks ReentrantReadWriteLock])
(:import [java.io File RandomAccessFile StringWriter PrintWriter]) (:import [java.io File RandomAccessFile StringWriter PrintWriter])
Expand All @@ -12,6 +12,7 @@
(:import [org.apache.commons.io FileUtils]) (:import [org.apache.commons.io FileUtils])
(:import [org.apache.commons.exec ExecuteException]) (:import [org.apache.commons.exec ExecuteException])
(:import [org.json.simple JSONValue]) (:import [org.json.simple JSONValue])
(:import [java.util Timer])
(:require [clojure.contrib [str-utils2 :as str]]) (:require [clojure.contrib [str-utils2 :as str]])
(:require [clojure [set :as set]]) (:require [clojure [set :as set]])
(:use [clojure walk]) (:use [clojure walk])
Expand Down Expand Up @@ -531,3 +532,28 @@


(defn container-get [^Container container] (defn container-get [^Container container]
(. container object)) (. container object))

(defn mk-timer []
{:timer (Timer.) :scheduled (atom #{})})

(defn scheduled? [timer id]
(contains? @(:scheduled timer) id))

(defn schedule [timer id delay afn]
(when (scheduled? timer id)
(throw (IllegalArgumentException. "Cannot schedule an already scheduled id")))
(let [wrapped (fn [] (afn) (swap! (:scheduled timer) disj id))]
(.schedule ^Timer (:timer timer)
delay
(ClojureTimerTask. wrapped))
(swap! (:scheduled timer) conj id)
))

(defn schedule-if-free [timer id delay afn]
(if-not (scheduled? timer id)
(schedule timer id delay afn)
))

;; need a map from "states" to the corresponding event transition (transition can include a delay)
;; {:rebalance {:delay 10 :action (fn [] ... :active)}
;; {(fn [] ...)}}
18 changes: 18 additions & 0 deletions src/jvm/backtype/storm/utils/ClojureTimerTask.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
package backtype.storm.utils;

import clojure.lang.IFn;
import java.util.TimerTask;

public class ClojureTimerTask extends TimerTask {
IFn _afn;

public ClojureTimerTask(IFn afn) {
super();
_afn = afn;
}

@Override
public void run() {
_afn.run();
}
}

0 comments on commit 720901f

Please sign in to comment.