java - How to safeguard json call for socketTimeout and httpTimeout -


i have java web module number of json on http calls server. i've been told our java admin team need safeguard against sockettimeouts , connection timeouts when making json on http calls because when response not received in time leaving hung threads on server.

as result have been asked set timeout 45 seconds.

could have suggestions of best way set both socket , http connection timeout on code below...

  public static string readjsonfromurl(string url) throws exception{       string jsontext = null;       try {           inputstream = null;           try {               = new url(url).openstream();               bufferedreader rd = new bufferedreader(new inputstreamreader(is, charset.forname(jsonconstants.default_charset)));               jsontext = readall(rd);                } {               if (is!=null){                   is.close();                 }                          }       } catch (malformedurlexception e) {           e.printstacktrace();           throw new exception(errorconstants.error_service_msg);       } catch (ioexception e) {           e.printstacktrace();           throw new exception(errorconstants.error_service_msg);                 }        if (jsontext != null && jsontext.indexof(jsonconstants.error_message) != -1){           throw new exception(errorconstants.error_service_msg);            }       return jsontext;   } 

thanks in advance


edit

i should have mentioned above i'm using java 1.4.2 , settimeout , setreadtimeout not there on httpurlconnection in version of java. i've been digging around have not been able find nice snippet on how yet. appreciate if point me example or add snippet of code might help.

don't use openstream(). use openconnection(), cast result httpurlconnection, , call appropriate methods set required timeouts. call getinputstream() data.

however motivation incorrect. if there connect timeout, there no connection, no thread @ server @ all.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -