commons-httpclient中的超时设置
2014-05-19 18:41
127 查看
connectionTimeout与soTimeout的差异,前者指创建一个有效的客户端到服务端链接的最大允许时间,后者指socket接收data的时间。
调用connectionTimeout属性的代码:
调用soTimeout属性的代码:
* HttpClient.executeMethod(method) 主要代码逻辑:
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) 主要代码逻辑:
public int executeMethod(HostConfiguration hostconfig, final HttpMethod method, final HttpState state) throws IOException, HttpException { LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)"); if (method == null) { throw new IllegalArgumentException("HttpMethod parameter may not be null"); } HostConfiguration defaulthostconfig = getHostConfiguration(); if (hostconfig == null) { hostconfig = defaulthostconfig; } URI uri = method.getURI(); if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) { // make a deep copy of the host defaults hostconfig = (HostConfiguration) hostconfig.clone(); if (uri.isAbsoluteURI()) { hostconfig.setHost(uri); } } HttpMethodDirector methodDirector = new HttpMethodDirector( getHttpConnectionManager(), hostconfig, this.params, (state == null ? getState() : state)); methodDirector.executeMethod(method); return method.getStatusCode(); } //------------------------------ /** * Executes the method associated with this method director. * * @throws IOException * @throws HttpException */ public void executeMethod(final HttpMethod method) throws IOException, HttpException { //... try { int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100); for (int redirectCount = 0;;) { // make sure the connection we have is appropriate if (this.conn != null && !hostConfiguration.hostEquals(this.conn)) { this.conn.setLocked(false); this.conn.releaseConnection(); this.conn = null; } // get a connection, if we need one if (this.conn == null) { this.conn = connectionManager.getConnectionWithTimeout( hostConfiguration, this.params.getConnectionManagerTimeout() ); this.conn.setLocked(true); if (this.params.isAuthenticationPreemptive() || this.state.isAuthenticationPreemptive()) { LOG.debug("Preemptively sending default basic credentials"); method.getHostAuthState().setPreemptive(); method.getHostAuthState().setAuthAttempted(true); if (this.conn.isProxied() && !this.conn.isSecure()) { method.getProxyAuthState().setPreemptive(); method.getProxyAuthState().setAuthAttempted(true); } } } } finally { if (this.conn != null) { this.conn.setLocked(false); } // If the response has been fully processed, return the connection // to the pool. Use this flag, rather than other tests (like // responseStream == null), as subclasses, might reset the stream, // for example, reading the entire response into a file and then // setting the file as the stream. if ( (releaseConnection || method.getResponseBodyAsStream() == null) && this.conn != null ) { this.conn.releaseConnection(); } } } //-------------------------- this.conn = connectionManager.getConnectionWithTimeout public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration, long timeout) throws ConnectionPoolTimeoutException { //... final HttpConnection conn = doGetConnection(hostConfiguration, timeout); // wrap the connection in an adapter so we can ensure it is used // only once return new HttpConnectionAdapter(conn); }
相关文章推荐
- httpclient4 请一定设置超时时间
- HttpClient两个超时设置的区别
- httpClient中的三种超时时间设置
- Java中httpClient中的三种超时设置小结(转)
- HttpClient 4.5版本设置连接超时时间
- Httpclient设置超时
- 使用Apache HttpClient 4.5设置超时时间
- httpClient中的三种超时设置总结
- httpclient连接超时时间设置
- org.apache.commons.httpclient.HttpClient 设置数据接收超时
- Java中httpClient中的三种超时设置小结
- Java中httpClient中三种超时设置
- Java中httpClient中的设置超时时间
- HttpClient设置联网超时时间
- httpClient中的三种超时设置小结
- Httpclient 设置读取超时及连接重试等配置
- HttpClient设置请求超时和读取超时
- java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout
- HttpClient连接请求超时设置
- Java中httpClient中的三种超时设置小结