title: Spring Boot RestTemplate ResourceAccessException: I/O error on POST request failed to respond
body: <p>I use Spring Boot and faced the following issue while keeping the long running connection to 3rd party REST service:</p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:5000/products/10": localhost:5000 failed to respond; nested exception is org.apache.http.NoHttpResponseException: localhost:5000 failed to respond
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:732)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
      at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:435)
      at com.example.pipeline.domain.service.nlp.NLPService.getDocumentsInfoNew(NLPService.java:42)
      at com.example.pipeline.domain.batch.steps.NLPTasklet.execute(NLPTasklet.java:170)
      at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
      at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
      at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
      at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
      at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
      at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
      at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
      at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
      at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
      at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
      at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
      at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:394)
      at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135)
      at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:308)
      at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:141)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: org.apache.http.NoHttpResponseException: localhost:5000 failed to respond
      at org.apache.http.impl.conn.DefaultHttpRespons
  </code></pre>
  
  <p>This service can keep the connection for one hour or more before it will return the result.</p>
  
  <p>My <code>RestTemplate</code> configuration looks like:</p>
  
  <pre><code>public static RestTemplate createRestTemplate(int connectionTimeoutMs, int readTimeoutMs, ObjectMapper objectMapper) {
  
          HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
          httpRequestFactory.setConnectTimeout(connectionTimeoutMs);
          httpRequestFactory.setReadTimeout(readTimeoutMs);
  
          RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
          List&lt;ClientHttpRequestInterceptor&gt; interceptors = new ArrayList&lt;ClientHttpRequestInterceptor&gt;();
          interceptors.add(new LoggingRequestInterceptor());
          restTemplate.setInterceptors(interceptors);
          restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpRequestFactory));
          MappingJackson2HttpMessageConverter messageConverter = restTemplate.getMessageConverters().stream().filter(MappingJackson2HttpMessageConverter.class::isInstance)
                  .map(MappingJackson2HttpMessageConverter.class::cast).findFirst().orElseThrow(() -&gt; new RuntimeException("MappingJackson2HttpMessageConverter not found"));
          messageConverter.setObjectMapper(objectMapper);
  
          restTemplate.getMessageConverters().stream().filter(StringHttpMessageConverter.class::isInstance).map(StringHttpMessageConverter.class::cast).forEach(a -&gt; {
              a.setWriteAcceptCharset(false);
              a.setDefaultCharset(StandardCharsets.UTF_8);
          });
  
          return restTemplate;
      }
  
  }
  
  RestTemplate restTemplate = HttpUtils.createRestTemplate(60 * 1000, 3 * 60 * 60 * 1000, objectMapper);
  </code></pre>
  
  <p>Is there anything I can do in order to fix it on my side or this is a pure issue on <code>http://localhost:5000</code> service side and should be fixed there?</p>