a Timer in a URLConnection

M

Marcelo

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
 
Z

zero

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)
 
M

Marcelo

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
 
M

Marcelo

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;
}
}
}
 
S

Stefan Schulz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top