01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.stats.counter.sampled;
05:
06: import com.tc.util.TCTimerImpl;
07:
08: import java.util.Timer;
09:
10: public class SampledCounterManagerImpl implements SampledCounterManager {
11: private final Timer timer = new TCTimerImpl(
12: "SampledCounterManager Timer", true);
13: private boolean shutdown = false;
14:
15: public SampledCounterManagerImpl() {
16: super ();
17: }
18:
19: public synchronized void shutdown() {
20: if (shutdown) {
21: return;
22: }
23: try {
24: timer.cancel();
25: } finally {
26: shutdown = true;
27: }
28: }
29:
30: public synchronized SampledCounter createCounter(
31: SampledCounterConfig config) {
32: if (shutdown) {
33: throw new IllegalStateException(
34: "counter manager is shutdown");
35: }
36: if (config == null) {
37: throw new NullPointerException("config cannot be null");
38: }
39:
40: final SampledCounterImpl counter = new SampledCounterImpl(
41: config);
42:
43: final long period = config.getIntervalSecs() * 1000;
44: timer.schedule(counter.getTimerTask(), period, period);
45:
46: return counter;
47: }
48:
49: }
|