Cannot use setReadTimeout(), any other alternative ?

Hi,

i have a very typical problem. I am trying to get an inputstream by using conn.getinputstream(), and of course as everybody knows, the program hangs if the inputstream doesn't come or is delayed.

I cannot use setReadTimeout() because i have a limitation of working with Java 1.4.2.

I tried creating another timer thread. But was not able to influence the getInputStream in any way. Neither have i been able to throw an exception from my timer thread to my main thread where i can catch and take appropriate action. I created a getInputStreamTimeout() method as a part of the Timer class which encapsulates the getInputStream() call.

code looks smthing like this :-

class TimerThread extends Thread {

void run(); //usual implementation of a timer.

timeout(); //method fired when elapsed time > max length

InputStream getInputStreamTimeout(URL Connection conn, TimerThread tt) {

try{

tt.start(); // starts the timer thread

tempIS = conn.getInputStream();

return tempIS;

} catch { ...}

}

}

i even tried a divide by zero operation in timeout() , it doesn't gets caught in getInputInputStreamTimeout()

The the callin snippet in Main program looks like

try{

TimerThread tt = new TimerThread(10000); //constructor which takes in MAX time

TempInpStream = tt.getInputStreamTimeout(conn, tt);

return TempInpStream;

} catch { ... }

Please help me guys, im new to java :-) !!

[1517 byte] By [679395XeroGa] at [2007-11-26 12:18:13]
# 1

Suggestions:

Don't divide by zero to create an exception. You can throw one with the throw keyword if you need to:

throw new Exception("whatever");

You have the connection creation on a separate thread but you call if from the original thread.

Instead put the inputstream and the processing of it on a separate thread. Use SwingUtitlities.invokeLater() to update the gui or if there is an error etc.

642814zadoka at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 2

Hi zadok,

i tried throwing exception in timeout before resorting to divide by zero. I threw an exception in timeout() and caught it in run(). But this doesn't relay an exception back to my main program to terminate existing loop and get on with next iteration. remember the fact that run() cannot throw an exception and neither can it return anything.

679395XeroGa at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 3
The timeout() method and the getInputStreamTimeout() method execute in different threads. You can't throw exceptions from one thread to another.You could try closing the URLConnection or the input stream in the timeout method, but there's no cross-platform guarantee it will work.
52259ejpa at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 4
I made the InputStream variable as a class variable, and tried iS.close(); in timeout() method.It makes no difference, the program still waits for the conn.getInputStream() to return.Is there NO way out ?
679395XeroGa at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 5
Does the sun.net.client.defaultReadTimeout system property help? See: http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html
362309alan.batemana at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 6

Hey Alan,

thanks for the suggestion, i tried setting the property. But it didnt work. Maybe i am doing it wrong. Here is wat i did.

I actually have a portlet, which is a kind of servlet, where i fetch the content from a URL. Now to simulate a slow server, i have written another servlet with a Thread.sleep() inside it and give its URL to my main Servlet.

I tried putting the System.setProperty() in the init() method as well as the getStream method. It set the properties right but it didnt cause any timeout or exception even when is set the delay to a much higher value than a timeout.

Am i doing something wrong above ?

679395XeroGa at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 7
That property needs to be set before the protocol handler is loaded. Can you provide the property to the command line options used to start the server?
362309alan.batemana at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...
# 8
whats a protocol handler, and when does it get loaded ?i dont think providing these properties on command line while starting the server is possible. I'll still try to do it. How do we set these properties thru command line ?
679395XeroGa at 2007-7-7 14:57:28 > top of Java-index,Archived Forums,Socket Programming...