ServerInstance.java
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.taover.heartbeat.bean;
import java.net.SocketTimeoutException;
import com.taover.util.UtilHttpByOkHttp;
public class ServerInstance implements Instance {
public int DEFAULT_REFORM_MIN_ERROR_COUNT = 1;
public int DEFAULT_REFORM_MAX_ERROR_COUNT = 3;
private String code;
private String url;
private int fixRateSec;
private int maxWaitSec;
private long latestRequestUnixtime;
private ServerResponse latestServerResponse;
private int errorServerResponseCount;
private boolean isIncrErrorServerResponseCount;
public ServerInstance(String code, String url, int fixRateSec, int maxWaitSec) throws Exception {
super();
if(fixRateSec < 60) {
throw new Exception("固定时间间隔不允许小于60s");
}
this.code = code;
this.url = url;
this.fixRateSec = fixRateSec;
this.maxWaitSec = maxWaitSec;
}
@Override
public void flush() {
this.isIncrErrorServerResponseCount = false;
//如果小于设置的发送时间间隔,则不发送请求
if(!this.isLatestRequestGreateEqualFixRateSec()) {
return;
}
this.latestRequestUnixtime = System.currentTimeMillis()/1000;
try {
this.latestServerResponse = ServerResponse.createByJSONString(UtilHttpByOkHttp.sendGet(this.url + this.getUrlParams(), null, maxWaitSec));
if(this.latestServerResponse.isCodeOk()) {
this.errorServerResponseCount = 0;
}
} catch(SocketTimeoutException timeError) {
this.latestServerResponse = ServerResponse.createOverdue();
} catch (Exception e) {
this.latestServerResponse = ServerResponse.createException(e);
}
if(this.latestServerResponse.isOverdue() || !this.latestServerResponse.isCodeOk()) {
++this.errorServerResponseCount;
this.isIncrErrorServerResponseCount = true;
}
}
private String getUrlParams() {
return "?code="+this.code+"&fixRateSec="+this.fixRateSec+"&maxWaitSec="+this.maxWaitSec+"&unixtime="+(System.currentTimeMillis()/1000);
}
private boolean isLatestRequestGreateEqualFixRateSec() {
return (System.currentTimeMillis()/1000 - this.latestRequestUnixtime) >= this.fixRateSec;
}
@Override
public String getIdentity() {
return this.code + "@" + this.url;
}
@Override
public boolean needReform() {
return this.isIncrErrorServerResponseCount
&& this.errorServerResponseCount>=DEFAULT_REFORM_MIN_ERROR_COUNT
&& this.errorServerResponseCount<=DEFAULT_REFORM_MAX_ERROR_COUNT;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getFixRateSec() {
return fixRateSec;
}
public void setFixRateSec(int fixRateSec) {
this.fixRateSec = fixRateSec;
}
public int getMaxWaitSec() {
return maxWaitSec;
}
public void setMaxWaitSec(int maxWaitSec) {
this.maxWaitSec = maxWaitSec;
}
public long getLatestRequestUnixtime() {
return latestRequestUnixtime;
}
public void setLatestRequestUnixtime(long latestRequestUnixtime) {
this.latestRequestUnixtime = latestRequestUnixtime;
}
public ServerResponse getLatestServerResponse() {
return latestServerResponse;
}
public void setLatestServerResponse(ServerResponse latestServerResponse) {
this.latestServerResponse = latestServerResponse;
}
public int getLostServerResponseCount() {
return errorServerResponseCount;
}
public void setLostServerResponseCount(int lostServerResponseCount) {
this.errorServerResponseCount = lostServerResponseCount;
}
public int getErrorServerResponseCount() {
return errorServerResponseCount;
}
public void setErrorServerResponseCount(int errorServerResponseCount) {
this.errorServerResponseCount = errorServerResponseCount;
}
public boolean isIncrErrorServerResponseCount() {
return isIncrErrorServerResponseCount;
}
public void setIncrErrorServerResponseCount(boolean isIncrErrorServerResponseCount) {
this.isIncrErrorServerResponseCount = isIncrErrorServerResponseCount;
}
}