synchronized void rotateToNextDomain() { int domainNameCount = diamondConfigure.getDomainNameList().size(); int index = domainNamePos.incrementAndGet(); if (index < 0) { index = -index; } if (domainNameCount == 0) { log.error("rotateToNextDomain domainNameCount is 0, administrator should resolve this"); return; } domainNamePos.set(index % domainNameCount); if (diamondConfigure.getDomainNameList().size() > 0) log.warn( "Rotate domain name to " + diamondConfigure.getDomainNameList().get(domainNamePos.get())); }
void changeSpacingInterval(HttpMethod httpMethod) { Header[] spacingIntervalHeaders = httpMethod.getResponseHeaders(Constants.SPACING_INTERVAL); if (spacingIntervalHeaders.length >= 1) { try { diamondConfigure.setPollingIntervalTime( Integer.parseInt(spacingIntervalHeaders[0].getValue())); } catch (RuntimeException e) { log.error("�����´μ��ʱ��ʧ��", e); } } }
private void configureHttpMethod( boolean skipContentCache, CacheData cacheData, long onceTimeOut, HttpMethod httpMethod) { if (skipContentCache && null != cacheData) { if (null != cacheData.getLastModifiedHeader() && Constants.NULL != cacheData.getLastModifiedHeader()) { httpMethod.addRequestHeader(Constants.IF_MODIFIED_SINCE, cacheData.getLastModifiedHeader()); } if (null != cacheData.getMd5() && Constants.NULL != cacheData.getMd5()) { httpMethod.addRequestHeader(Constants.CONTENT_MD5, cacheData.getMd5()); } } httpMethod.addRequestHeader(Constants.ACCEPT_ENCODING, "gzip,deflate"); HttpMethodParams params = new HttpMethodParams(); params.setSoTimeout((int) onceTimeOut); httpMethod.setParams(params); httpClient .getHostConfiguration() .setHost( diamondConfigure.getDomainNameList().get(this.domainNamePos.get()), diamondConfigure.getPort()); }
private void checkDiamondServerConfigInfo() { Set<String> updateDataIdGroupPairs = checkUpdateDataIds(diamondConfigure.getReceiveWaitTime()); if (null == updateDataIdGroupPairs || updateDataIdGroupPairs.size() == 0) { log.debug("DataID not changed."); return; } for (String freshDataIdGroupPair : updateDataIdGroupPairs) { int middleIndex = freshDataIdGroupPair.indexOf(WORD_SEPARATOR); if (middleIndex == -1) continue; String freshDataId = freshDataIdGroupPair.substring(0, middleIndex); String freshGroup = freshDataIdGroupPair.substring(middleIndex + 1); ConcurrentHashMap<String, CacheData> cacheDatas = cache.get(freshDataId); if (null == cacheDatas) { continue; } CacheData cacheData = cacheDatas.get(freshGroup); if (null == cacheData) { continue; } receiveConfigInfo(cacheData); } }
private void rotateCheckConfigInfo() { scheduledExecutor.schedule( new Runnable() { public void run() { if (!isRun) { log.warn("DiamondSubscriber is not running. so quit."); return; } try { checkLocalConfigInfo(); checkDiamondServerConfigInfo(); checkSnapshot(); } catch (Exception e) { e.printStackTrace(); log.error("rotateCheckConfigInfo error:", e); } finally { rotateCheckConfigInfo(); } } }, bFirstCheck ? 60 : diamondConfigure.getPollingIntervalTime(), TimeUnit.SECONDS); bFirstCheck = false; }
protected void initHttpClient() { if (MockServer.isTestMode()) { return; } HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setHost( diamondConfigure.getDomainNameList().get(this.domainNamePos.get()), diamondConfigure.getPort()); MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 10); HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled()); params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections()); params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections()); params.setConnectionTimeout(diamondConfigure.getConnectionTimeout()); params.setSoTimeout(60 * 1000); connectionManager.setParams(params); httpClient = new HttpClient(connectionManager); httpClient.setHostConfiguration(hostConfiguration); }
Set<String> checkUpdateDataIds(long timeout) { if (!isRun) { throw new RuntimeException("DiamondSubscriber is not running. checkUpdateDataIds return."); } if (MockServer.isTestMode()) { return testData(); } long waitTime = 0; String probeUpdateString = getProbeUpdateString(); if (StringUtils.isBlank(probeUpdateString)) { return null; } while (0 == timeout || timeout > waitTime) { long onceTimeOut = getOnceTimeOut(waitTime, timeout); waitTime += onceTimeOut; PostMethod postMethod = new PostMethod(Constants.HTTP_URI_FILE); postMethod.addParameter(Constants.PROBE_MODIFY_REQUEST, probeUpdateString); HttpMethodParams params = new HttpMethodParams(); params.setSoTimeout((int) onceTimeOut); postMethod.setParams(params); try { httpClient .getHostConfiguration() .setHost( diamondConfigure.getDomainNameList().get(this.domainNamePos.get()), this.diamondConfigure.getPort()); int httpStatus = httpClient.executeMethod(postMethod); switch (httpStatus) { case SC_OK: { Set<String> result = getUpdateDataIds(postMethod); return result; } case SC_SERVICE_UNAVAILABLE: { rotateToNextDomain(); } break; default: { log.warn("checkUpdateDataIds HTTP State: " + httpStatus); rotateToNextDomain(); } } } catch (HttpException e) { log.error("checkUpdateDataIds HttpException", e); rotateToNextDomain(); } catch (IOException e) { log.error("checkUpdateDataIds IOException", e); rotateToNextDomain(); } catch (Exception e) { log.error("checkUpdateDataIds Unknown Exception", e); rotateToNextDomain(); } finally { postMethod.releaseConnection(); } } throw new RuntimeException( "checkUpdateDataIds timeout " + diamondConfigure.getDomainNameList().get(this.domainNamePos.get()) + ", timeout=" + timeout); }
@Override public BatchHttpResult getConfigureInformationBatch( List<String> dataIds, String group, int timeout) { if (dataIds == null) { log.error("dataId list cannot be null,group=" + group); return new BatchHttpResult(HttpStatus.SC_BAD_REQUEST); } if (group == null) { group = Constants.DEFAULT_GROUP; } StringBuilder dataIdBuilder = new StringBuilder(); for (String dataId : dataIds) { dataIdBuilder.append(dataId).append(Constants.LINE_SEPARATOR); } String dataIdStr = dataIdBuilder.toString(); PostMethod post = new PostMethod(Constants.HTTP_URI_FILE_BATCH); post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, timeout); BatchHttpResult response = null; try { NameValuePair dataIdValue = new NameValuePair("dataIds", dataIdStr); NameValuePair groupValue = new NameValuePair("group", group); post.setRequestBody(new NameValuePair[] {dataIdValue, groupValue}); httpClient .getHostConfiguration() .setHost( diamondConfigure.getDomainNameList().get(this.domainNamePos.get()), this.diamondConfigure.getPort()); int status = httpClient.executeMethod(post); String responseMsg = post.getResponseBodyAsString(); if (status == HttpStatus.SC_OK) { String json = null; try { json = responseMsg; List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>(); Object resultObj = JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {}); if (!(resultObj instanceof List<?>)) { throw new RuntimeException( "batch query deserialize type error, not list, json=" + json); } List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj; for (ConfigInfoEx configInfoEx : resultList) { configInfoExList.add(configInfoEx); } response = new BatchHttpResult(configInfoExList); log.info( "batch query success,dataIds=" + dataIdStr + ",group=" + group + ",json=" + json); } catch (Exception e) { response = new BatchHttpResult(Constants.BATCH_OP_ERROR); log.error( "batch query deserialize error,dataIdStr=" + dataIdStr + ",group=" + group + ",json=" + json, e); } } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) { response = new BatchHttpResult(HttpStatus.SC_REQUEST_TIMEOUT); log.error( "batch query timeout, socket timeout(ms):" + timeout + ",dataIds=" + dataIdStr + ",group=" + group); } else { response = new BatchHttpResult(status); log.error( "batch query fail, status:" + status + ", response:" + responseMsg + ",dataIds=" + dataIdStr + ",group=" + group); } } catch (HttpException e) { response = new BatchHttpResult(Constants.BATCH_HTTP_EXCEPTION); log.error("batch query http exception,dataIds=" + dataIdStr + ",group=" + group, e); } catch (IOException e) { response = new BatchHttpResult(Constants.BATCH_IO_EXCEPTION); log.error("batch query io exception, dataIds=" + dataIdStr + ",group=" + group, e); } finally { post.releaseConnection(); } return response; }