Connection Reset with Jersey Client

好久不见. 提交于 2019-12-01 11:32:15

问题


I am seeing a lot of Connection Resets in Production.There could be multiple causes to it but I wanted to ensure that there are no Connection leakages coming from in code.I am using Jersey Client in code

Client this.client = ApacheHttpClient.create();

client.resource("/stores/"+storeId).type(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class,indexableStore);

Originally I was instantiating client in the following fashion Client this.client = Client.create() and we changed it to ApacheHttpClient.create(). I am not calling close() on the response but I am assuming ApacheHttpClient would do that internally as HttpClient executeMethod gets invoked which handles all the boiler plate stuff for us. Could there be a potential connection leakage in the way the code is written ?


回答1:


Like you said Connection Reset could be caused by many possible reasons. One such possibility could be that server timed out while processing the request, thats why the client receives connection reset. The comments section of the answered question here discusses possible causes of connection reset in detail. One possible solution I can think of is to configure HttpClient to retry the request in case of a failure. You could set the HttpMethodRetryHandler like below to do so (Reference). You may perhaps need to modify the code based on the exception you receive.

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);


来源:https://stackoverflow.com/questions/23568641/connection-reset-with-jersey-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!