a Timer in a URLConnection



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

.



Relevant Pages

  • Protocolli di connessione win 2000 server e mac G4/G5
    ... abbiamo un server Win 2000 Server con delle cartelle ... dati a cui noi utenti G4/G5 accediamo, ... gli utenti G5 prendono questi files eo cartelle con nomi lunghi e li ...
    (it.comp.macintosh)
  • Re: Sito su .mac con dominio personale LUNGO
    ... Se te ne intendi di DNS puoi usare granitecanyon.com ... Vediamo ora come usare il nostro .Mac con un vero e proprio nome di dominio. ... Generalmente si tende a pensare al sito e al nome di dominio come a due cose unite, mentre in realtà sono concetti completamente disgiunti: semplificando, possiamo dire che al mondo esistono i cosiddetti server DNS che tengono in memoria tutti gli accoppiamenti tra un nome di dominio e il server dov'è ospitato il sito. ... se inseriamo noi stessi il nostro dominio in questi DNS e poi decidiamo noi con quale server abbinarlo ecco che il gioco è ...
    (it.comp.macintosh)
  • Re: whats the referer of an applet ?
    ... URLConnection, ... to set request headers. ...     urlc.connect; ... whether or not you read the response, at that point the server starts ...
    (comp.lang.java.programmer)
  • Re: Buffalo
    ... LINKSTATION LIVE II 500GB SHARED NETWORK STORAGE CON MEDIA SERVER ... si collega al router e permette di scaricare i ... torrent anche da computer spento??? ...
    (it.comp.macintosh)
  • VirtualBox come demone?
    ... Windows Server 2003 o 2008 con un software fiscale in Terminal Server da ... che vorrei non dover loggare il Server Mac per ...
    (it.comp.macintosh)

Quantcast