1 package org.csc.phynixx.watchdog;
2
3 /*
4 * #%L
5 * phynixx-watchdog
6 * %%
7 * Copyright (C) 2014 csc
8 * %%
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * #L%
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 /* (non-Javadoc)
49 * @see de.csc.xaresource.sample.watchdog.ITimeoutCondition#getTimeout()
50 */
51 public long getTimeout() {
52 return timeout.get();
53 }
54
55
56 /* (non-Javadoc)
57 * @see de.csc.xaresource.sample.watchdog.ITimeoutCondition#setActive(boolean)
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 /* (non-Javadoc)
70 * @see de.csc.xaresource.sample.watchdog.ITimeoutCondition#resetCondition()
71 */
72 public void resetCondition() {
73 this.start = new AtomicLong(System.currentTimeMillis());
74 }
75
76
77 /* (non-Javadoc)
78 * @see de.csc.xaresource.sample.watchdog.ITimeoutCondition#resetCondition(long)
79 */
80 public void resetCondition(long timeout) {
81 this.timeout = new AtomicLong(timeout);
82 this.start = new AtomicLong(System.currentTimeMillis());
83 }
84
85 /*
86 * Not synchronized as to be meant for the watch dog exclusively
87 *
88 * Do not call it unsynchronized
89 *
90 * (non-Javadoc)
91 * @see de.csc.xaresource.sample.watchdog.ITimeoutCondition#checkCondition()
92 */
93 public boolean checkCondition() {
94 long timeElapsed = System.currentTimeMillis() - start.get();
95 if (timeElapsed > this.timeout.get()) {
96
97 if (LOG.isDebugEnabled()) {
98 //LOG.info("Current Thread="+Thread.currentThread()+" Condition="+this+" Checkpoint "+ System.currentTimeMillis() +" timeout="+(System.currentTimeMillis() - start));
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 * Not synchronized as to be meant for the watch dog exclusively
116 *
117 * Do not call it unsynchronized
118 *
119 * (non-Javadoc)
120 * @see de.csc.xaresource.sample.watchdog.ITimeoutCondition#conditionViolated()
121 */
122 public abstract void conditionViolated();
123
124 }