如何解决http 响应未返回 503 状态代码 - java 中的多线程问题
我正在使用带有固定线程池的执行程序服务,该线程池提交用于创建 xml 文件的未来任务。 每次从url中获取xml,生成xml并与已有的xml进行比较,看是否有不匹配的地方。我提供了 fetchAPIData 方法作为同步以使其线程安全 - 但它大大降低了性能。为了避免线程中断异常并使其线程安全,我在构建 httpclient 时设置了 connectionpoolmanager。 我面临的问题是,当服务器遇到 503 时,它会重新连接会话并重试连接。一旦命中 503,它会重试并发送响应,但程序似乎并没有在最后退出。似乎在无休止地等待某个线程响应的响应。以下是我的代码。我已经尝试过
fetchAPIData:-
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,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,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;
}
创建会话cookie
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();
}
buildHttpResponse:-
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;
}
isRetryrequired:- 最大重试次数为 5 次,其中 5 秒、10 秒....最多 25 秒
public boolean isRetryrequired(CloseableHttpResponse httpResponse,int retryCount) {
return httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE &&
retryCount < ClarityAPIValidatorConstants.RETRY_MAX_COUNT;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。