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);
}
}
resttemplate
add a comment |
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);
}
}
resttemplate
add a comment |
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);
}
}
resttemplate
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
resttemplate
asked Nov 22 at 5:39
parkey19
1
1
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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