commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From marcs...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpConnectionManager.java
Date Fri, 12 Apr 2002 21:11:34 GMT
marcsaeg    02/04/12 14:11:34

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpConnectionManager.java
  Log:
  Added getConnection() method that accepts a timeout value.  If a connection
  does not become available before the timeout expires an HttpException
  is thrown.
  
  Revision  Changes    Path
  1.4       +87 -7     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java
  
  Index: HttpConnectionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HttpConnectionManager.java	1 Apr 2002 02:27:17 -0000	1.3
  +++ HttpConnectionManager.java	12 Apr 2002 21:11:34 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java,v
1.3 2002/04/01 02:27:17 marcsaeg Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/04/01 02:27:17 $
  + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java,v
1.4 2002/04/12 21:11:34 marcsaeg Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/04/12 21:11:34 $
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -160,13 +160,33 @@
        * Get an HttpConnection for a given URL.  The URL must be fully
        * specified (i.e. contain a protocol and a host (and optional port number).
        * If the maximum number of connections for the host has been reached, this
  -     * method will block until a connection becomes available.
  +     * method will block forever until a connection becomes available.
        *
        * @param sURL - a fully specified URL.
        * @return an HttpConnection for the given host:port
        * @exception java.net.MalformedURLException
  +     * @exception org.apache.commons.HttpException - If no connection becomes availble
before the timeout expires
        */
  -    public HttpConnection getConnection(String sURL) throws MalformedURLException
  +    public HttpConnection getConnection(String sURL) throws HttpException, MalformedURLException
  +    {
  +        return getConnection(sURL, 0);
  +    }
  +
  +    /**
  +     * Get an HttpConnection for a given URL.  The URL must be fully
  +     * specified (i.e. contain a protocol and a host (and optional port number).
  +     * If the maximum number of connections for the host has been reached, this
  +     * method will block for <code>timeout</code> milliseconds or until a connection

  +     * becomes available.  If no connection becomes available before the timeout expires
  +     * an HttpException exception will be thrown.
  +     *
  +     * @param sURL - a fully specified URL.
  +     * @param timeout - the time (in milliseconds) to wait for a connection to become available
  +     * @return an HttpConnection for the given host:port
  +     * @exception java.net.MalformedURLException
  +     * @exception org.apache.commons.HttpException - If no connection becomes availble
before the timeout expires
  +     */
  +    public HttpConnection getConnection(String sURL, long timeout) throws HttpException,
MalformedURLException
       {
           HttpConnection conn = null;
   
  @@ -233,16 +253,25 @@
                       numConnections = new Integer(numConnections.intValue()+1);
                       mapNumConnections.put(key, numConnections);
                   }else{
  -                    // Wait for a connection to be available
  +                    // No connections available, so wait
  +
  +                    // Start the timeout thread
  +                    TimeoutThread threadTimeout = new TimeoutThread();
  +                    threadTimeout.setTimeout(timeout);
  +                    threadTimeout.setWakeupThread(Thread.currentThread());
  +                    threadTimeout.start();
  +
  +                    // wait for the connection to be available
                       while(conn == null){    // spin lock
                           try{
                               log.debug("HttpConnectionManager.getConnection:  waiting for
connection for " + host + ":" + port);
                               listConnections.wait();
                           }catch(InterruptedException e){
  -                            log.debug("HttpConnectionManager.getConnection:  Interrupted.");
  +                            throw new HttpException("Timeout waiting for connection.");
                           }
                           if(listConnections.size() > 0){
                               conn = (HttpConnection)listConnections.removeFirst();
  +                            threadTimeout.interrupt();
                           }
                       }
                   }
  @@ -285,6 +314,57 @@
               // Put the connect back in the available list and notify a waiter
               listConnections.addFirst(conn);
               listConnections.notify();
  +        }
  +    }
  +
  +    /**
  +     * In getConnection, if the maximum number of connections has already
  +     * been reached the call will block.  This class is used to help provide
  +     * a timeout facility for this wait.  Because does not provide a way to
  +     * determine if wait() returned due to a notify() or a timeout we need
  +     * an outside mechanism to interrupt the waiting thread after the specified
  +     * timeout interval.
  +     */
  +    private class TimeoutThread extends Thread
  +    {
  +        private long timeout = 0;
  +        private Thread thrdWakeup = null;
  +
  +        public void setTimeout(long timeout)
  +        {
  +            this.timeout = timeout;
  +        }
  +
  +        public long getTimeout()
  +        {
  +            return timeout;
  +        }
  +
  +        public void setWakeupThread(Thread thrdWakeup)
  +        {
  +            this.thrdWakeup = thrdWakeup;
  +        }
  +
  +        public Thread getWakeupThread()
  +        {
  +            return thrdWakeup;
  +        }
  +
  +        public void run()
  +        {
  +            if(timeout == 0){
  +                return;
  +            }
  +            if(thrdWakeup == null){
  +                return;
  +            }
  +
  +            try{
  +                sleep(timeout);
  +                thrdWakeup.interrupt();
  +            }catch(InterruptedException e){
  +                // This is expected
  +            }
           }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message