Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

about URLConnection


  • From: Pramendra.Dadhwal@xxxxxxxxxxxxxxx (Pramendra Dadhwal)
  • Subject: about URLConnection
  • Date: Mon, 30 Aug 1999 19:06:38 +0900

Hi,

You must have
1. a thread that talks to the server( by sending a http request ) say
'HTTPRequester' 
2. a timer thread that fires a 'timeout' set by the user. 

You start the timer thread.
You start the HTTPRequester thread.

Scenario 1:
 The HTTPRequest succeeds before the 'TimeOut' of the timer thread and all
is fine.
 
Scenario 2:
 The HTTPRequest succeeds , but since your server is slow, the timeout
occurs. notify the client.

Scenarion 3:
  The HTTP request fails (because the server is down,( u get an IOException
) ). notify the client.

so you have, possibly, 3 objects :

1. The timer object
2. the http requester object
3. the client object which will be responding to the above scenarios.


a simple timer is 
public class Timer extends Thread
{
     public long timeOutInterval = 0L;
     TimeOutListener listener = null;
     boolean continueThread = true;

     public Timer(long timeOut,TimeOutListener l)
     {
	super("Time Out Thread);
	timeoutInterval = timeOut;
	timeOutListenr = l;		
     }

   
    public void stop()
   {
       continueThread = false;
   }

     public void run()
    {
	long acutalTimeSlept = 0L;
	while((actualTimeSlept<timeOutInterval) && continueThread)
	{
		long l1 = System.currentTimeMillis();
	           try
		{
			sleep(timeOutInterval);
		}
		catch(Exception e) // InterruptedException
		long l2 = System.currentTimeMillis();
		actualTimeSlept += l1-l2;
	}
	if(continueThread)
		timeOutListener.timedOut(timeOutInterval);
    }
}


public class HTTPRequester extends Thread
{
      HTTPRequester(URL urltoRead, HTTPResponseListener );
      boolean continueThread = true;

     public void stop()
     {   continueThread = false; }

	// u could add a retry count.. 
     public void run()
     {
	DatanputStream dis = null;
	while( continueThread )
	{
		try
		{
	                URLConnection uc = urltoRead.openConnection(); // no
connection yet
            	    uc.setUseCaches(false); // possibly user specified.
	                uc.connect(); // request for connection now!!!
throws IOException here , so url read failed. notify user using
HTTPResponseListener i/f

            	    dis = new DataInputStream(uc.getInputStream()); //
throws IOException here .try again
 
		    // read the data in a loop

                	    dis.close(); // throws an exception
	
  	              // notify the data has been read using
HTTPResponseListener..

		    continueThread = false; // because we are done.
		 }
	            catch(IOException e) // catch errors while connecting
and reading from the input stream.
            	{ 
			// notify the user that the HTTP Reqest failed using
the HTTPResponseListener
			continueThread = false;
			return;
	            }
            }
	if(dis!=null)
   	{
	   dis.close();
	   dis = null;
	}
        }
     }
 }


public class Client  implements TimeOutListener, HTTPResponseListener
{
	static boolean responseAvailable = false;
	public synchronized void timedOut(long timeOutInterval)
	{
		httpRequester.stop();
		showMessage("Connection timedOut);
		responseAvailable = true;
	}

	public synchronized void httpSuccess(Object[] data)
	{
		timer.stop();
		showMessage("Success");
		responseAvailable = true;
	}
	
	// other methods for various HTTP Response Types.. 

	public static void main(String[] args)
	{
		TimeOutThread timer = new TimeOut(1000000);
		HTTPRequesterThread httpRequesterThread = new
HTTPRequesterThread(new URL(http://www.yahoo.com";));
		timer.start();
		httpRequestThread.start();
		
		while(!(Client.responseAvailable))
		{
			try
			{
			 	sleep(100);
			}
			catch(InterruptedException e){}
		 }
	}
}

suggestions welcome..

hope this helps...

pramendra.
> ----------
> From: 	zbei[SMTP:zbei@xxxxxxxxxxx]
> Sent: 	Tuesday, August 31, 1999 2:08 AM
> To: 	advanced-java@xxxxxxxxxxxxxxxx
> Subject: 	about URLConnection
> 
> Hi all:
> 	How can i know a URLConnection  hasn't connected with
> a server in a certain time?I want to know the  timeout of the 
> connection.But,if it connected with the server,it has no time 
> limit to download a file.
> 	if i add a timer ,i want to use the timer in the period 
> of connection not in the period of downloading.How can i achieve
> it?
> 
> thanks a lot
> 
>  
>