http 响应未返回 503 状态代码 - java 中的多线程问题

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?