我正在使用带有固定线程池的executor服务,它提交将来创建xml文件的任务。每次从url获取xml时,它都会生成xml并与现有xml进行比较,以确定是否存在任何不匹配。我已经将fetchapidata方法提供为synchronized,以使其具有线程安全性,但是它大大降低了性能。为了避免线程中断异常并使其具有线程安全性,我在构建httpclient时设置了connectionpoolmanager。我面临的问题是,当服务器点击503时,它会重新连接会话并重试连接。一旦503被命中,它会重试并发送响应,但程序似乎不会在最后退出。它似乎在无休止地等待某个线程响应的响应。下面是我的代码。我已经尝试过了
场效应晶体管chapidata:-
public String fetchAPIData(URI uri) throws ClarityAPIValidatorException {
int retryCount = 0;
CloseableHttpResponse httpResponse = buildHttpResponse(uri,
getClientContext(), getHttpClient());
int statusCode = httpResponse.getStatusLine().getStatusCode();
try {
switch (statusCode) {
/*StatusCode - 200*/
case HttpStatus.SC_OK:
return outputBasedOnStatusCode(httpResponse);
/*StatusCode - 401*/
case HttpStatus.SC_UNAUTHORIZED:
logger.info(ClarityAPIValidatorConstants.RESPONSE_CODE + httpResponse.getStatusLine().getStatusCode() + " " +
ClarityAPIValidatorConstants.URL + uri);
createSessionWithCookies(new URI(ClarityAPIProperties.getApiBaseURL()));
httpResponse = buildHttpResponse(uri, getClientContext(),
getHttpClient());
return outputBasedOnStatusCode(httpResponse);
/*StatusCode - 503*/
case HttpStatus.SC_SERVICE_UNAVAILABLE:
do {
logger.info(ClarityAPIValidatorConstants.RESPONSE_CODE + httpResponse.getStatusLine().getStatusCode() + " " +
ClarityAPIValidatorConstants.URL + uri);
retryCount++;
this.pause(httpResponse, retryCount);
logger.info(ClarityAPIValidatorConstants.REQ_EXECUTING + uri);
createSessionWithCookies(uri);
httpResponse = buildHttpResponse(uri,
getClientContext(), getHttpClient());
logger.info(ClarityAPIValidatorConstants.RETRY + ClarityAPIValidatorConstants.RESPONSE_CODE + httpResponse.getStatusLine().getStatusCode());
}
while (isRetryRequired(httpResponse, retryCount));
return outputBasedOnStatusCode(httpResponse);
/*StatusCode other than 200, 401 and 503*/
default:
throw new ClarityAPIValidatorException(UN_HANDLED_RES_TYPE +
statusCode + PATH + uri.getPath());
}
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
return null;
}
创建会话okie
public void createSessionWithCookies(URI uri) throws ClarityAPIValidatorException {
CredentialsProvider provider =
buildCredentialsProvider(ClarityAPIProperties.getApiUsername(),
ClarityAPIProperties.getApiPassword());
CloseableHttpClient httpClient = buildHttpClient(provider);
HttpClientContext clientContext = buildHttpClientContext(uri, provider);
if (httpClient == null || clientContext == null) {
throw new ClarityAPIValidatorException(CREATE_SESSION_ERROR);
} else {
this.setHttpClient(httpClient);
this.setClientContext(clientContext);
}
}
buildhttpclient:-
public CloseableHttpClient buildHttpClient(CredentialsProvider provider) {
RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD)
.build();
PoolingHttpClientConnectionManager connManager =
new PoolingHttpClientConnectionManager();
return HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connManager)
.build();
}
建筑presponse:-
public CloseableHttpResponse buildHttpResponse(URI uri,
HttpClientContext clientContext,
CloseableHttpClient httpClient)
throws ClarityAPIValidatorException {
HttpGet httpGet = buildHttpRequest(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet, clientContext);
} catch (IOException e) {
e.printStackTrace();
throw new ClarityAPIValidatorException(GET_REQ_FAIL + uri.getPath());
}
if (isNull(response))
throw new ClarityAPIValidatorException(RES_NULL + uri.getPath());
return response;
}
伊斯雷特yrequired:- max 重试时间为5秒,5秒,10秒…最多25秒
public boolean isRetryRequired(CloseableHttpResponse httpResponse,
int retryCount) {
return httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE &&
retryCount < ClarityAPIValidatorConstants.RETRY_MAX_COUNT;
}
暂无答案!
目前还没有任何答案,快来回答吧!