| 1 | package net.digitaltsunami.tmeter.record; |
| 2 | |
| 3 | import net.digitaltsunami.tmeter.Timer; |
| 4 | import net.digitaltsunami.tmeter.TimerShell; |
| 5 | import net.digitaltsunami.tmeter.action.ActionChain; |
| 6 | import net.digitaltsunami.tmeter.action.ActionChainShutdownType; |
| 7 | import net.digitaltsunami.tmeter.action.TimeRecorderAction; |
| 8 | |
| 9 | /** |
| 10 | * Time recorder that persists timers in a separate thread. This recorder does |
| 11 | * not perform the persistence of the timer, but wraps the recorder that will |
| 12 | * persist the timer. |
| 13 | * <p> |
| 14 | * As this implementation persists the timers using a queue, it may have |
| 15 | * unfinished work when the application is shutdown. This default behavior may |
| 16 | * be overridden by providing an {@link ActionChainShutdownType} to the |
| 17 | * constructor. |
| 18 | * |
| 19 | * @author dhagberg |
| 20 | * |
| 21 | */ |
| 22 | public class QueuedTimeRecorder implements TimeRecorder { |
| 23 | |
| 24 | private ActionChain chain; |
| 25 | |
| 26 | /** |
| 27 | * Create a wrapper for the |
| 28 | * |
| 29 | * @param recorder |
| 30 | */ |
| 31 | public QueuedTimeRecorder(TimeRecorder recorder) { |
| 32 | TimeRecorderAction action = new TimeRecorderAction(recorder); |
| 33 | chain = new ActionChain(action); |
| 34 | } |
| 35 | |
| 36 | public QueuedTimeRecorder(TimeRecorder recorder, ActionChainShutdownType shutdownType) { |
| 37 | TimeRecorderAction action = new TimeRecorderAction(recorder); |
| 38 | chain = new ActionChain(action, shutdownType); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void record(Timer timer) { |
| 43 | chain.submitCompletedTimer(timer); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Complete the current queue of timers and stop processing. No timers |
| 48 | * submitted after this action will be processed. |
| 49 | */ |
| 50 | public void shutdown() { |
| 51 | chain.shutdown(); |
| 52 | } |
| 53 | |
| 54 | } |