RestTemplate exchange delay 6-180 seconds











up vote
-1
down vote

favorite












I'm using RestTemplate to send HTTP 1.1 requests to web service endpoints via the exchange() method.
most of the time this works fine, but in sometime cases this results in a long (6-30 seconds) delay between when restTemplate.exchange() is called and when called external api server.
But I'm using httpclient excute called not occur delay and called springboot api server not occur delay.
only occur spring3 tomcat server.
I am suspicious of the header part.
give some any advice plz..



ps: using below config reference [this]: https://tech.asimio.net/2016/12/27/Troubleshooting-Spring-RestTemplate-Requests-Timeout.html#adjusting-poolinghttpclientconnectionmanager-settings



@Slf4j
@Configuration
@EnableScheduling
public class HttpClientConfig {

private static final int CONNECT_TIMEOUT = 10000;
private static final int REQUEST_TIMEOUT = 10000;
private static final int SOCKET_TIMEOUT = 60000;
private static final int MAX_TOTAL_CONNECTIONS = 50;
private static final int DEFAULT_MAX_PER_ROUTE = 50;
private static final int CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS = 30;

@Bean
public PoolingHttpClientConnectionManager poolingConnectionManager() {
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
poolingConnectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
poolingConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
return poolingConnectionManager;
}


@Bean
public CloseableHttpClient httpClient() {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(REQUEST_TIMEOUT)
.setConnectTimeout(CONNECT_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT).build();

return HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(poolingConnectionManager())
.build();
}

@Bean
public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
return new Runnable() {
@Override
@Scheduled(fixedDelay = 10000)
public void run() {
try {
if (connectionManager != null) {
connectionManager.closeExpiredConnections();
connectionManager.closeIdleConnections(CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
} else {
log.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
}
} catch (Exception e) {
log.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
}
}
};
}


}



public class apiCall {
//rest call example
public HttpEntity<?> createHeader(String appType, String jsonParam) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", appType);
headers.set("Content-Type", appType);
return new HttpEntity<String>(jsonParam, headers);

}

//jsonParam : jsonString
public String call() {
HttpEntity<?> requestEntity = this.createHeader(MediaType.APPLICATION_JSON_UTF8_VALUE,jsonParam);
response = restTemplate.exchange(targetUrl, HttpMethod.POST,
requestEntity, String.class);
}
}









share|improve this question


























    up vote
    -1
    down vote

    favorite












    I'm using RestTemplate to send HTTP 1.1 requests to web service endpoints via the exchange() method.
    most of the time this works fine, but in sometime cases this results in a long (6-30 seconds) delay between when restTemplate.exchange() is called and when called external api server.
    But I'm using httpclient excute called not occur delay and called springboot api server not occur delay.
    only occur spring3 tomcat server.
    I am suspicious of the header part.
    give some any advice plz..



    ps: using below config reference [this]: https://tech.asimio.net/2016/12/27/Troubleshooting-Spring-RestTemplate-Requests-Timeout.html#adjusting-poolinghttpclientconnectionmanager-settings



    @Slf4j
    @Configuration
    @EnableScheduling
    public class HttpClientConfig {

    private static final int CONNECT_TIMEOUT = 10000;
    private static final int REQUEST_TIMEOUT = 10000;
    private static final int SOCKET_TIMEOUT = 60000;
    private static final int MAX_TOTAL_CONNECTIONS = 50;
    private static final int DEFAULT_MAX_PER_ROUTE = 50;
    private static final int CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS = 30;

    @Bean
    public PoolingHttpClientConnectionManager poolingConnectionManager() {
    PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
    poolingConnectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
    poolingConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
    return poolingConnectionManager;
    }


    @Bean
    public CloseableHttpClient httpClient() {
    RequestConfig requestConfig = RequestConfig.custom()
    .setConnectionRequestTimeout(REQUEST_TIMEOUT)
    .setConnectTimeout(CONNECT_TIMEOUT)
    .setSocketTimeout(SOCKET_TIMEOUT).build();

    return HttpClients.custom()
    .setDefaultRequestConfig(requestConfig)
    .setConnectionManager(poolingConnectionManager())
    .build();
    }

    @Bean
    public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
    return new Runnable() {
    @Override
    @Scheduled(fixedDelay = 10000)
    public void run() {
    try {
    if (connectionManager != null) {
    connectionManager.closeExpiredConnections();
    connectionManager.closeIdleConnections(CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
    } else {
    log.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
    }
    } catch (Exception e) {
    log.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
    }
    }
    };
    }


    }



    public class apiCall {
    //rest call example
    public HttpEntity<?> createHeader(String appType, String jsonParam) {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", appType);
    headers.set("Content-Type", appType);
    return new HttpEntity<String>(jsonParam, headers);

    }

    //jsonParam : jsonString
    public String call() {
    HttpEntity<?> requestEntity = this.createHeader(MediaType.APPLICATION_JSON_UTF8_VALUE,jsonParam);
    response = restTemplate.exchange(targetUrl, HttpMethod.POST,
    requestEntity, String.class);
    }
    }









    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I'm using RestTemplate to send HTTP 1.1 requests to web service endpoints via the exchange() method.
      most of the time this works fine, but in sometime cases this results in a long (6-30 seconds) delay between when restTemplate.exchange() is called and when called external api server.
      But I'm using httpclient excute called not occur delay and called springboot api server not occur delay.
      only occur spring3 tomcat server.
      I am suspicious of the header part.
      give some any advice plz..



      ps: using below config reference [this]: https://tech.asimio.net/2016/12/27/Troubleshooting-Spring-RestTemplate-Requests-Timeout.html#adjusting-poolinghttpclientconnectionmanager-settings



      @Slf4j
      @Configuration
      @EnableScheduling
      public class HttpClientConfig {

      private static final int CONNECT_TIMEOUT = 10000;
      private static final int REQUEST_TIMEOUT = 10000;
      private static final int SOCKET_TIMEOUT = 60000;
      private static final int MAX_TOTAL_CONNECTIONS = 50;
      private static final int DEFAULT_MAX_PER_ROUTE = 50;
      private static final int CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS = 30;

      @Bean
      public PoolingHttpClientConnectionManager poolingConnectionManager() {
      PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
      poolingConnectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
      poolingConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
      return poolingConnectionManager;
      }


      @Bean
      public CloseableHttpClient httpClient() {
      RequestConfig requestConfig = RequestConfig.custom()
      .setConnectionRequestTimeout(REQUEST_TIMEOUT)
      .setConnectTimeout(CONNECT_TIMEOUT)
      .setSocketTimeout(SOCKET_TIMEOUT).build();

      return HttpClients.custom()
      .setDefaultRequestConfig(requestConfig)
      .setConnectionManager(poolingConnectionManager())
      .build();
      }

      @Bean
      public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
      return new Runnable() {
      @Override
      @Scheduled(fixedDelay = 10000)
      public void run() {
      try {
      if (connectionManager != null) {
      connectionManager.closeExpiredConnections();
      connectionManager.closeIdleConnections(CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
      } else {
      log.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
      }
      } catch (Exception e) {
      log.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
      }
      }
      };
      }


      }



      public class apiCall {
      //rest call example
      public HttpEntity<?> createHeader(String appType, String jsonParam) {
      HttpHeaders headers = new HttpHeaders();
      headers.set("Accept", appType);
      headers.set("Content-Type", appType);
      return new HttpEntity<String>(jsonParam, headers);

      }

      //jsonParam : jsonString
      public String call() {
      HttpEntity<?> requestEntity = this.createHeader(MediaType.APPLICATION_JSON_UTF8_VALUE,jsonParam);
      response = restTemplate.exchange(targetUrl, HttpMethod.POST,
      requestEntity, String.class);
      }
      }









      share|improve this question













      I'm using RestTemplate to send HTTP 1.1 requests to web service endpoints via the exchange() method.
      most of the time this works fine, but in sometime cases this results in a long (6-30 seconds) delay between when restTemplate.exchange() is called and when called external api server.
      But I'm using httpclient excute called not occur delay and called springboot api server not occur delay.
      only occur spring3 tomcat server.
      I am suspicious of the header part.
      give some any advice plz..



      ps: using below config reference [this]: https://tech.asimio.net/2016/12/27/Troubleshooting-Spring-RestTemplate-Requests-Timeout.html#adjusting-poolinghttpclientconnectionmanager-settings



      @Slf4j
      @Configuration
      @EnableScheduling
      public class HttpClientConfig {

      private static final int CONNECT_TIMEOUT = 10000;
      private static final int REQUEST_TIMEOUT = 10000;
      private static final int SOCKET_TIMEOUT = 60000;
      private static final int MAX_TOTAL_CONNECTIONS = 50;
      private static final int DEFAULT_MAX_PER_ROUTE = 50;
      private static final int CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS = 30;

      @Bean
      public PoolingHttpClientConnectionManager poolingConnectionManager() {
      PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
      poolingConnectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
      poolingConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
      return poolingConnectionManager;
      }


      @Bean
      public CloseableHttpClient httpClient() {
      RequestConfig requestConfig = RequestConfig.custom()
      .setConnectionRequestTimeout(REQUEST_TIMEOUT)
      .setConnectTimeout(CONNECT_TIMEOUT)
      .setSocketTimeout(SOCKET_TIMEOUT).build();

      return HttpClients.custom()
      .setDefaultRequestConfig(requestConfig)
      .setConnectionManager(poolingConnectionManager())
      .build();
      }

      @Bean
      public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
      return new Runnable() {
      @Override
      @Scheduled(fixedDelay = 10000)
      public void run() {
      try {
      if (connectionManager != null) {
      connectionManager.closeExpiredConnections();
      connectionManager.closeIdleConnections(CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
      } else {
      log.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
      }
      } catch (Exception e) {
      log.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
      }
      }
      };
      }


      }



      public class apiCall {
      //rest call example
      public HttpEntity<?> createHeader(String appType, String jsonParam) {
      HttpHeaders headers = new HttpHeaders();
      headers.set("Accept", appType);
      headers.set("Content-Type", appType);
      return new HttpEntity<String>(jsonParam, headers);

      }

      //jsonParam : jsonString
      public String call() {
      HttpEntity<?> requestEntity = this.createHeader(MediaType.APPLICATION_JSON_UTF8_VALUE,jsonParam);
      response = restTemplate.exchange(targetUrl, HttpMethod.POST,
      requestEntity, String.class);
      }
      }






      resttemplate






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 5:39









      parkey19

      1




      1





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424514%2fresttemplate-exchange-delay-6-180-seconds%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424514%2fresttemplate-exchange-delay-6-180-seconds%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Catalogne

          Violoncelliste

          Héron pourpré