1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.datacollection.writer;
20
21
22 import org.apache.log4j.Logger;
23
24 public class ClientAck {
25 static Logger log = Logger.getLogger(ClientAck.class);
26
27
28
29 public static final int OK = 100;
30 public static final int KO = -100;
31 public static final int KO_LOCK = -200;
32
33 private long ts = 0;
34
35 private Object lock = new Object();
36 private int status = 0;
37 private Throwable exception = null;
38 private int waitTime = 6 * 1000;
39 private int timeOut = 15 * 1000;
40
41 public ClientAck() {
42 this.ts = System.currentTimeMillis() + timeOut;
43 }
44
45 public int getTimeOut() {
46 return timeOut;
47 }
48
49 public void wait4Ack() {
50 synchronized (lock) {
51
52 while (this.status == 0) {
53
54 try {
55 lock.wait(waitTime);
56 } catch (InterruptedException e) {
57 }
58 long now = System.currentTimeMillis();
59 if (now > ts) {
60 this.status = KO_LOCK;
61 this.exception = new RuntimeException("More than maximum time lock ["
62 + this.toString() + "]");
63 }
64 }
65
66
67
68 }
69 }
70
71 public void releaseLock(int status, Throwable exception) {
72 this.exception = exception;
73 this.status = status;
74
75
76
77
78 synchronized (lock) {
79
80 lock.notifyAll();
81 }
82
83 }
84
85 public int getStatus() {
86 return status;
87 }
88
89 public void setStatus(int status) {
90 this.status = status;
91 }
92
93 public Throwable getException() {
94 return exception;
95 }
96
97 public void setException(Throwable exception) {
98 this.exception = exception;
99 }
100 }