Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与Jersey客户端的连接重置_Java_Tcp_Jersey_Jersey Client_Connection Leaks - Fatal编程技术网

Java 与Jersey客户端的连接重置

Java 与Jersey客户端的连接重置,java,tcp,jersey,jersey-client,connection-leaks,Java,Tcp,Jersey,Jersey Client,Connection Leaks,我在生产中看到很多连接重置。可能有多种原因,但我想确保代码中没有连接泄漏。我在代码中使用Jersey Client Client this.client = ApacheHttpClient.create(); client.resource("/stores/"+storeId).type(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class,indexableStore); 最初,我是以以下方式实例化客户机 Client th

我在生产中看到很多连接重置。可能有多种原因,但我想确保代码中没有连接泄漏。我在代码中使用Jersey Client

Client this.client = ApacheHttpClient.create();

client.resource("/stores/"+storeId).type(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class,indexableStore);
最初,我是以以下方式实例化客户机
Client this.Client=Client.create(),我们将其更改为ApacheHttpClient.create()。我没有对响应调用close(),但我假设ApacheHttpClient会在调用HttpClient executeMethod时在内部执行此操作,HttpClient executeMethod会为我们处理所有锅炉板的内容。在编写代码的过程中是否存在潜在的连接泄漏?

正如您所说的
连接重置
可能是由许多可能的原因造成的。其中一种可能是服务器在处理请求时超时,这就是客户端接收到连接重置的原因。回答问题的评论部分详细讨论了连接重置的可能原因。我能想到的一个可能的解决方案是将
HttpClient
配置为在出现故障时重试请求。您可以设置
HttpMethodRetryHandler
,如下所示()。您可能需要根据收到的异常修改代码

HttpMethodRetryHandler retryHandler = new HttpMethodRetryHandler()
      {
         public boolean retryMethod(
                 final HttpMethod method,
                 final IOException exception,
                 int executionCount)
         {
            if (executionCount >= 5)
            {
               // Do not retry if over max retry count
               return false;
            }
            if (exception instanceof NoHttpResponseException)
            {
               // Retry if the server dropped connection on us
               return true;
            }
            if (!method.isRequestSent())
            {
               // Retry if the request has not been sent fully or
               // if it's OK to retry methods that have been sent
               return true;
            }
            // otherwise do not retry
            return false;
         }
      };

      ApacheHttpClient client = ApacheHttpClient.create();
      HttpClient hc = client.getClientHandler().getHttpClient();
      hc.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);    
       client.resource("/stores/"+storeId).type(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class,indexableStore);