Connection Reset with Jersey Client

坚强是说给别人听的谎言 提交于 2019-12-01 13:40:00
nilesh

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