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 (将#修改为@)