The timer defined in this file is very similar to java.util.Timer, except it integrates with Storm's time simulation capabilities. This lets us test code that does asynchronous work on the timer thread.
| 26 | */ |
| 27 | |
| 28 | public class StormTimer implements AutoCloseable { |
| 29 | |
| 30 | //task to run |
| 31 | private final StormTimerTask task = new StormTimerTask(); |
| 32 | |
| 33 | /** |
| 34 | * Makes a Timer in the form of a StormTimerTask Object. |
| 35 | * |
| 36 | * @param name name of the timer |
| 37 | * @param onKill function to call when timer is killed unexpectedly |
| 38 | */ |
| 39 | public StormTimer(String name, Thread.UncaughtExceptionHandler onKill) { |
| 40 | if (onKill == null) { |
| 41 | throw new RuntimeException("onKill func is null!"); |
| 42 | } |
| 43 | if (name == null) { |
| 44 | this.task.setName("timer"); |
| 45 | } else { |
| 46 | this.task.setName(name); |
| 47 | } |
| 48 | this.task.setOnKillFunc(onKill); |
| 49 | this.task.setActive(true); |
| 50 | |
| 51 | this.task.setDaemon(true); |
| 52 | this.task.setPriority(Thread.MAX_PRIORITY); |
| 53 | this.task.start(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Schedule a function to be executed in the timer. |
| 58 | * |
| 59 | * @param delaySecs the number of seconds to delay before running the function |
| 60 | * @param func the function to run |
| 61 | * @param checkActive whether to check is the timer is active |
| 62 | * @param jitterMs add jitter to the run |
| 63 | */ |
| 64 | public void schedule(int delaySecs, Runnable func, boolean checkActive, int jitterMs) { |
| 65 | scheduleMs(Time.secsToMillisLong(delaySecs), func, checkActive, jitterMs); |
| 66 | } |
| 67 | |
| 68 | public void schedule(int delaySecs, Runnable func) { |
| 69 | schedule(delaySecs, func, true, 0); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Same as schedule with millisecond resolution. |
| 74 | * |
| 75 | * @param delayMs the number of milliseconds to delay before running the function |
| 76 | * @param func the function to run |
| 77 | * @param checkActive whether to check is the timer is active |
| 78 | * @param jitterMs add jitter to the run |
| 79 | */ |
| 80 | public void scheduleMs(long delayMs, Runnable func, boolean checkActive, int jitterMs) { |
| 81 | if (func == null) { |
| 82 | throw new RuntimeException("function to schedule is null!"); |
| 83 | } |
| 84 | if (checkActive) { |
| 85 | checkActive(); |
nothing calls this directly
no outgoing calls
no test coverage detected