1 package org.csc.phynixx.watchdog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.csc.phynixx.common.logger.IPhynixxLogger;
25 import org.csc.phynixx.common.logger.PhynixxLogManager;
26 import org.csc.phynixx.watchdog.log.CheckConditionFailedLog;
27
28 import java.util.concurrent.atomic.AtomicLong;
29
30
31 public abstract class TimeoutCondition extends NotificationCondition implements ITimeoutCondition {
32
33
34 private static final IPhynixxLogger LOG = PhynixxLogManager.getLogger(TimeoutCondition.class);
35
36 private AtomicLong timeout = new AtomicLong(-1);
37
38 private AtomicLong start = new AtomicLong(-1);
39
40
41 public TimeoutCondition(long timeout) {
42 super();
43 this.timeout = new AtomicLong(timeout);
44 this.resetCondition();
45 }
46
47
48
49
50
51 public long getTimeout() {
52 return timeout.get();
53 }
54
55
56
57
58
59 public void setActive(boolean active) {
60 super.setActive(active);
61 if (active) {
62 this.start = new AtomicLong(System.currentTimeMillis());
63 } else {
64 this.start =new AtomicLong( -1);
65 }
66 }
67
68
69
70
71
72 public void resetCondition() {
73 this.start = new AtomicLong(System.currentTimeMillis());
74 }
75
76
77
78
79
80 public void resetCondition(long timeout) {
81 this.timeout = new AtomicLong(timeout);
82 this.start = new AtomicLong(System.currentTimeMillis());
83 }
84
85
86
87
88
89
90
91
92
93 public boolean checkCondition() {
94 long timeElapsed = System.currentTimeMillis() - start.get();
95 if (timeElapsed > this.timeout.get()) {
96
97 if (LOG.isDebugEnabled()) {
98
99 String logString = " violated at " + System.currentTimeMillis() +
100 " (elapsed time=" + timeElapsed + ")";
101 LOG.debug(new CheckConditionFailedLog(this, logString).toString());
102 }
103 return false;
104 }
105 return true;
106 }
107
108 public String toString() {
109 return "Timeout Condition " + this.timeout + " msecs (isActive=" + this.isActive() + ")";
110 }
111
112
113
114
115
116
117
118
119
120
121
122 public abstract void conditionViolated();
123
124 }