MCPcopy
hub / github.com/dianping/cat / TimerSyncTask

Class TimerSyncTask

cat-core/src/main/java/com/dianping/cat/task/TimerSyncTask.java:31–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29import java.util.concurrent.ExecutorService;
30
31public class TimerSyncTask implements Task {
32
33 private static final long DURATION = TimeHelper.ONE_MINUTE;
34
35 private static TimerSyncTask m_instance = new TimerSyncTask();
36
37 private static ExecutorService s_threadPool = Threads.forPool().getFixedThreadPool("Cat-ConfigSyncTask", 3);
38
39 private static boolean m_active = false;
40
41 private List<SyncHandler> m_handlers = new ArrayList<SyncHandler>();
42
43 public static TimerSyncTask getInstance() {
44 if (!m_active) {
45 synchronized (TimerSyncTask.class) {
46 if (!m_active) {
47 Threads.forGroup("cat").start(m_instance);
48
49 m_active = true;
50 }
51 }
52 }
53 return m_instance;
54 }
55
56 @Override
57 public String getName() {
58 return "timer-sync-task";
59 }
60
61 public void register(SyncHandler handler) {
62 synchronized (this) {
63 m_handlers.add(handler);
64 }
65 }
66
67 @Override
68 public void run() {
69 boolean active = TimeHelper.sleepToNextMinute();
70
71 while (active) {
72 long current = System.currentTimeMillis();
73
74 for (final SyncHandler handler : m_handlers) {
75 s_threadPool.submit(new Runnable() {
76
77 @Override
78 public void run() {
79 final Transaction t = Cat.newTransaction("TimerSync", handler.getName());
80
81 try {
82 handler.handle();
83 t.setStatus(Transaction.SUCCESS);
84 } catch (Exception e) {
85 t.setStatus(e);
86 Cat.logError(e);
87 } finally {
88 t.complete();

Callers

nothing calls this directly

Calls 2

forPoolMethod · 0.95
getFixedThreadPoolMethod · 0.45

Tested by

no test coverage detected