首页 > 代码库 > commons-httpclient中的超时设置

commons-httpclient中的超时设置

connectionTimeout与soTimeout的差异,前者指创建一个有效的客户端到服务端链接的最大允许时间,后者指socket接收data的时间。

connectionManager.getParams().setConnectionTimeout(50);
connectionManager.getParams().setSoTimeout(100);

调用connectionTimeout属性的代码:

//org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory
public static Socket createSocket(final SocketTask task, int timeout)
     throws IOException, UnknownHostException, ConnectTimeoutException{
            try {
                TimeoutController.execute(task, timeout);
            } catch (TimeoutController.TimeoutException e) {
                throw new ConnectTimeoutException(
                    "The host did not accept the connection within timeout of " 
                    + timeout + " ms");
            }
            Socket socket = task.getSocket();
            if (task.exception != null) {
                throw task.exception;
            }
            return socket;
    }
//org.apache.commons.httpclient.util.TimeoutController public static void execute(Thread task, long timeout) throws TimeoutException { task.start(); try { task.join(timeout); } catch (InterruptedException e) { /* if somebody interrupts us he knows what he is doing */ } if (task.isAlive()) { task.interrupt(); throw new TimeoutException(); } }

调用soTimeout属性的代码:

socket.setSoTimeout(this.params.getSoTimeout());

 

* HttpClient.executeMethod(method) 主要代码逻辑:

  1  public int executeMethod(HostConfiguration hostconfig, 
  2         final HttpMethod method, final HttpState state)
  3         throws IOException, HttpException  {
  4             
  5         LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)");
  6 
  7         if (method == null) {
  8             throw new IllegalArgumentException("HttpMethod parameter may not be null");
  9         }
 10         HostConfiguration defaulthostconfig = getHostConfiguration();
 11         if (hostconfig == null) {
 12             hostconfig = defaulthostconfig;
 13         }
 14         URI uri = method.getURI(); 
 15         if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
 16             // make a deep copy of the host defaults

 17             hostconfig = (HostConfiguration) hostconfig.clone();
 18             if (uri.isAbsoluteURI()) {
 19                 hostconfig.setHost(uri);
 20             }
 21         }
 22         
 23         HttpMethodDirector methodDirector = new HttpMethodDirector(
 24                 getHttpConnectionManager(),
 25                 hostconfig,
 26                 this.params,
 27                 (state == null ? getState() : state));
 28         methodDirector.executeMethod(method);
 29         return method.getStatusCode();
 30     }
 31 
 32 //------------------------------
 33 
 34 /**
 35      * Executes the method associated with this method director.
 36      * 
 37      * @throws IOException
 38      * @throws HttpException
 39      */
 40     public void executeMethod(final HttpMethod method) throws IOException, HttpException {
 41         //...        
 42         try {
 43             int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100);
 44 
 45             for (int redirectCount = 0;;) {
 46 
 47                 // make sure the connection we have is appropriate
 48                 if (this.conn != null && !hostConfiguration.hostEquals(this.conn)) {
 49                     this.conn.setLocked(false);
 50                     this.conn.releaseConnection();
 51                     this.conn = null;
 52                 }
 53         
 54                 // get a connection, if we need one
 55                 if (this.conn == null) {
 56                     this.conn = connectionManager.getConnectionWithTimeout(
 57                         hostConfiguration,
 58                         this.params.getConnectionManagerTimeout() 
 59                     );
 60                     this.conn.setLocked(true);
 61                     if (this.params.isAuthenticationPreemptive()
 62                      || this.state.isAuthenticationPreemptive()) 
 63                     {
 64                         LOG.debug("Preemptively sending default basic credentials");
 65                         method.getHostAuthState().setPreemptive();
 66                         method.getHostAuthState().setAuthAttempted(true);
 67                         if (this.conn.isProxied() && !this.conn.isSecure()) {
 68                             method.getProxyAuthState().setPreemptive();
 69                             method.getProxyAuthState().setAuthAttempted(true);
 70                         }
 71                     }
 72                 }
 73         } finally {
 74             if (this.conn != null) {
 75                 this.conn.setLocked(false);
 76             }
 77             // If the response has been fully processed, return the connection
 78             // to the pool.  Use this flag, rather than other tests (like
 79             // responseStream == null), as subclasses, might reset the stream,
 80             // for example, reading the entire response into a file and then
 81             // setting the file as the stream.
 82             if (
 83                 (releaseConnection || method.getResponseBodyAsStream() == null) 
 84                 && this.conn != null
 85             ) {
 86                 this.conn.releaseConnection();
 87             }
 88         }
 89 
 90     }
 91 
 92 //-------------------------- this.conn = connectionManager.getConnectionWithTimeout
 93 
 94  public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration, 
 95         long timeout) throws ConnectionPoolTimeoutException {
 96         //...
 97         final HttpConnection conn = doGetConnection(hostConfiguration, timeout);
 98 
 99         // wrap the connection in an adapter so we can ensure it is used 
100         // only once
101         return new HttpConnectionAdapter(conn);
102     }