Hello everybody,
I would like to implement a timer into a URLConnection object to avoid some server useless conections. My application can connect to the server, but the server doesn't send back any releavant information (a file) but it mantains the connection alive.
I would like to make a Timer in order to stop the connection if there is no response from the server in a given amount of time. I have this kind of code:
//Connect to the server
con.connect();
final boolean isGettingData = false;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("Time's up! "+isGettingData);
//Kill the URLConnection
con = null;
}
}, 2000);
//Get the length of the image
//Normally the application stops here if the server is giving useless information
//that mantains the connection opened
int length = con.getContentLength();
//If the application passes here, the server is sending the information that I need
//That's why this variable changes.
isGettingData = true;
However, this doesn't work fine for many threads that acces this method.
Do you have an idea for implementing better this code?
thanks for your help.
Marcelo
zero - 29 Nov 2005 22:23 GMT
> I would like to make a Timer in order to stop the connection if there
> is no response from the server in a given amount of time. I have this
> kind of code:
how about URLConnection:setConnectTimeout(int timeout)

Signature
Beware the False Authority Syndrome
zero - 29 Nov 2005 22:26 GMT
zero <zero@this.hi> wrote in news:Xns971DEE06159BDzerothishi@
195.130.132.70:
>> I would like to make a Timer in order to stop the connection if there
>> is no response from the server in a given amount of time. I have this
>> kind of code:
>
> how about URLConnection:setConnectTimeout(int timeout)
Hmm nevermind, I think I misunderstood the question. Sorry.

Signature
Beware the False Authority Syndrome
Marcelo - 29 Nov 2005 22:42 GMT
I have tried with the URLConnection.setConnectionTimeout, but it doesn't work because somehow the server sends useless information (or just un error page),
I am trying to implement another class to manage the timer stuff, but I am having some trouble because of references... If I found a solution, I will put it here...
Marcelo
Marcelo - 30 Nov 2005 00:11 GMT
Hi everybody,
Now i have something like a solution, but I need some help here. The only thing that doesn't work very well is the setDaemon() method. I don't want to use it because my program can still running if there are other active threads (which is possible) so it will take part of my Internet connection.
I don't know how to avoid this step, because the line that is driving me crazy is
length = connection.getContentLength();
somehow, the connection is still open even if I kill the thread t=null
Do you have an idea in order to avoid this problem?
thanks for your help,
Marcelo
public class TimerTemp{
public Timer timer;
public int length = -1;
public boolean isFinished = false;
URLConnection connection;
Thread t;
public TimerTemp(final URL urlObject){
t = new Thread(){
public void run(){
try{
connection = urlObject.openConnection();
connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0;H010818)" );
//Connect to the server
connection.connect();
length = connection.getContentLength();
}catch(IOException e){
e.printStackTrace();
}
}
};
//TODO: Not a good implementation
t.setDaemon(true);
t.start();
timer = new Timer(true);
timer.schedule(new temp(), 2000);
}
private class temp extends TimerTask{
public void run(){
if(length == -1)
System.out.println("Time's up");
else
System.out.println("length "+length);
timer.cancel();
connection = null;
System.out.println("alive:"+t.isAlive()+t.getState());
t = null;
isFinished= true;
}
}
}
Stefan Schulz - 30 Nov 2005 13:55 GMT
> somehow, the connection is still open even if I kill the thread t=null
> Do you have an idea in order to avoid this problem?
You don't kill the thread by nulling the reference. You need to look at
Thread.interrupt(), which might be helpful if used correctly.
Threads do not actually need any references to themselves to continue
running. The following is perfectly legal (if questionable) java code:
new Thread(){
public void run(){
// do something
}
}.start();
It will create and start a new thread which will execute its complete
run() method, even if its reference is never saved anywhere.

Signature
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"