2

I have one GET Restful service that return JSON. I have to get Response time of that service. My requirement is that when i try to get JSON data from that service and if that service take more than 10 seconds then i have to redirect it some another page. So how can i do this?

My code is given below

import java.net.URL;
import java.net.HttpURLConnection;

URL url = new URL(sURL);
HttpURLConnection req = (HttpURLConnection) url.openConnection();
req.connect();

if (req.getResponseCode() == HttpURLConnection.HTTP_OK) {
    System.out.println("ResponseCode = HTTP_OK");
}

I am using Java with Eclipse Mars 1.

user3441151
  • 1,880
  • 6
  • 35
  • 79
  • Possible duplicate of [HttpURLConnection timeout question](http://stackoverflow.com/questions/2799938/httpurlconnection-timeout-question) – Sneh Nov 10 '16 at 10:39
  • You can check the accepted answer here for the required behaviour. [Connection Timeout](http://stackoverflow.com/questions/2799938/httpurlconnection-timeout-question) – Umais Gillani Nov 10 '16 at 10:39

4 Answers4

3

HttpURLConnection has a setConnectTimeout method.

You can use it and catch the SocketTimeoutException, then you can redirect to the other page you want.

Edit

If you want the response anyway, and the duration also, you can take the current system time juste before the call, then after response compare the time that your request took.

Incepter
  • 2,711
  • 15
  • 33
0

First set timeout and then catch the timeout exception and make a new request:

try {
    req.setConnectTimeout(TimeUnit.SECONDS.toMillis(10)); 
    req.setReadTimeout(TimeUnit.SECONDS.toMillis(10));

    [...] readData(req);

} catch (SocketTimeoutException e) {
    // do request again
}

Edit: readTimeout is optional. I'm using it always bacause I want wait too long for data.

alex
  • 8,904
  • 6
  • 49
  • 75
  • Why you are using setReadTimeout? Is that usefull for me? – user3441151 Nov 10 '16 at 10:25
  • @user3441151 http://stackoverflow.com/questions/3069382/what-is-the-difference-between-connection-and-read-timeout-for-sockets – alex Nov 10 '16 at 10:28
  • I am using "HttpURLConnection req = (HttpURLConnection) url.openConnection();req.setConnectTimeout(10);req.setReadTimeout(10);req.connect();" but it always forward me to exception? – user3441151 Nov 10 '16 at 10:37
  • TIMEOUT is in miliseconds? – user3441151 Nov 10 '16 at 10:39
  • I am using your code and wait for 10 seconds but it will not worked for me. It is not coming on catch block? – user3441151 Nov 10 '16 at 11:28
  • I am using try { URL url = new URL(sURL); HttpURLConnection req = (HttpURLConnection) url.openConnection(); req.setConnectTimeout(10000); req.setReadTimeout(10000); req.connect(); if (req.getResponseCode() == HttpURLConnection.HTTP_OK) { System.out.println("ResponseCode = HTTP_OK"); } } catch (SocketTimeoutException e) { System.out.println("API takes more than 10 second to response"); } – user3441151 Nov 10 '16 at 11:42
  • How can I test it? – user3441151 Nov 10 '16 at 11:53
0
long start = System.currentTimeMillis();   
chain.doFilter(request, response);
long elapsedTime = System.currentTimeMillis() - start;

if(elapsedTime <= 10){
    System.out.println("ResponseCode = HTTP_OK");
}

check elapsedTime less then or equal to 10 or not, here request and respone is from ServletRequest and ServletResponse

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78
0

firstly you want to setReadTimeout or setConnectTimeout in url request and catch java.net.SocketTimeoutException then in catch redirect to your new url as the code below

import java.net.URL;
import java.net.HttpURLConnection;
try {
     URL url = new URL(sURL);
     HttpURLConnection req = (HttpURLConnection) url.openConnection();
     req .setReadTimeout(10000); // 10 seconds
       if (req.getResponseCode() == HttpURLConnection.HTTP_OK) {
            System.out.println("ResponseCode = HTTP_OK");
         }
    } catch (java.net.SocketTimeoutException e) {
        req = (HttpURLConnection) new URL("your new URL").openConnection();
   }
Ahmed Taha
  • 13
  • 4