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 = -1; private ServerResponse latestServerResponse; private int errorServerResponseCount = 0; private boolean isIncrErrorServerResponseCount = false; 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; } }