How to wait particular up to particular time limit until get response from rest service

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to wait particular up to particular time limit until get response from rest service



In my code i am calling one external web service to get response by using HTTP Client Get request. Here the service may delay up to 5 mins to give the response. So i have to do below things.
1. want to set request timeout(No idea how to do it)
2. call the service and set the time limit up to what time it can wait for response(In my case i want to set time limit for 5 mins)



can any one suggest me how can i achieve this 2 things.



Thanks,



Sudheer





I am able to find how to set request time out i am using PoolingHttpClientConnectionManager ConnManager = new PoolingHttpClientConnectionManager(timeToLive,TimeUnit.MINUTES); . Stil no clue how to wait until get the response.
– user3853393
Jul 25 at 16:12




1 Answer
1



You can set timeout for request using:


RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build();
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();



For setting timeout for response,you can wrap the call in thread and set the timeout for that thread.



PFB the code snippet below:


Future<T> future = null;
future = pool.submit(new Callable<T>() {
public T call() {
return executeImpl(url);
}
});

try {
return future.get(10, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
log.warn("task interrupted", name);
}
catch (ExecutionException e) {
log.error(name + " execution exception", e);
}
catch (TimeoutException e) {
log.debug("future timed out", name);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results