0

I have a java servlet requesting some request to some other server, and the other server has a apache server with a default timeout of 2 minutes.

I have two urls to be called, in-case the first url has failed, then i need to call the second url, but what happens is the first url takes a default time of 2 minutes to timeout. But i do not require it to timeout like this, say after 10 seconds if the result has not been obtained then i need to call the second url

URL urlConnect = new URL(url.toString());
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection urlc = (HttpURLConnection) urlConnect.openConnection();
urlc.setConnectTimeout(1000*20);
urlc.connect();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
raghul
  • 1,008
  • 1
  • 15
  • 33

1 Answers1

0

Just catch a SocketTimeoutException:

try{ 
         boolean timeout = false;
         URL urlConnect = new URL(url.toString());
         HttpURLConnection.setFollowRedirects(false);
         HttpURLConnection urlc = (HttpURLConnection) urlConnect.openConnection();
         urlc.setConnectTimeout(10000); // 10 sec
         urlc.setReadTimeout(10000); // 10 sec
         urlc.connect();
       }catch(SocketTimeoutException e){
           timeout = true;
       }finally{
          if(timeout){
             handleSecoundRequestFunction(); //the same principle as by first connection
          }
       }
Oli
  • 94
  • 1
  • 10