0

I want to call REST API by java programming. And I also want to give a time limitation during calling that API. If response time take more than 10 second than I want to disconnect API calling and print a message that response time is more than 10 second.

Please help me by given example code of java.

Given bellow the source code of calling API.

JSONParserPost jsonParserpost = new JSONParserPost();
        String output = jsonParserpost.makeHttpRequest(URL, "POST", request); 
        System.out.println("Row output :"+ output.toString());
        JSONObject jsonObject = new JSONObject(output);
        if(jsonObject != null) 
            responeXML = (String)jsonObject.get("response");

Here in 2nd line I've called a REST API. Now I want to fixed a time limit on duration of response of REST API.

Roman Sarker
  • 31
  • 1
  • 6
  • Please share your implementation here for better suggestion. –  Feb 20 '19 at 06:08
  • Dear Arun Kumar, thanks for your response. I have attached my source code. Please see this and response me again. – Roman Sarker Feb 20 '19 at 06:32
  • you can try this https://www.baeldung.com/java-http-request or https://www.baeldung.com/httpclient-timeout –  Feb 20 '19 at 06:36
  • Please see the link - https://stackoverflow.com/questions/15867930/how-to-set-connection-timeout-for-jsonparser-makehttprequest-in-android – Santosh Feb 20 '19 at 06:49

3 Answers3

0

1

URL url = new URL(this.serviceURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("Accept", "application/xml;");

connection.setDoInput(true);
connection.setDoOutput(true);

/*
 * connection timeout value to 20 seconds
 */
int apiReadTimeOut = 20; // 20 seconds
connection.setConnectTimeout(apiReadTimeOut * 1000);
connection.setReadTimeout(apiReadTimeOut * 1000);

2

HttpClient httpClient = null;

/*
* connection timeout value to 20 seconds
*/
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
httpClient = new DefaultHttpClient(httpParams);
Santosh
  • 782
  • 4
  • 15
  • 38
0

You can use spring restTemplate

   @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        ((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setConnectTimeout(milisecond);
        ((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setReadTimeout(milisecond);

        return restTemplate;
    }

Please find example - https://howtodoinjava.com/spring-boot2/resttemplate-timeout-example/

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
0

If you are using httpClient this following link can help you from my understanding of your problem. Apache HttpClient Timeout.

int CONNECTION_TIMEOUT_MS = timeoutSeconds * 1000; // Timeout in millis.
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
.setConnectTimeout(CONNECTION_TIMEOUT_MS)
.setSocketTimeout(CONNECTION_TIMEOUT_MS)
.build();

HttpPost httpPost = new HttpPost(URL);
httpPost.setConfig(requestConfig);
Rahat
  • 16
  • 2