?? httpconnectionmanager.java
字號:
package com.maverick.http;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Vector;
/**
*
* Manages a pool of {@link HttpConnection} objects for an {@link HttpClient}.
*
* @author Lee David Painter <a href="mailto: lee@3sp.com"><lee@3sp.com></a>
* @author Brett Smith <a href="mailto: brett@3sp.com"><brett@3sp.com></a>
* @version $Revision: 1.13 $
*/
public class HttpConnectionManager {
private Vector connections = new Vector();
HttpClient client;
static int newIdx = 0;
static int reuseIdx = 0;
private int maxPoolSize = Integer.MAX_VALUE;
/* DEBUG */org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpConnectionManager.class);
public HttpConnectionManager(HttpClient client) {
this.client = client;
}
/**
* Get a connection. If there is a valid pooled available, then that will be
* returned otherwise a new connection will be created.
*
* @return HttpConnection
* @throws IOException
* @throws UnknownHostException
*/
public synchronized HttpConnection getConnection() throws IOException, UnknownHostException, HttpException,
UnsupportedAuthenticationException, AuthenticationCancelledException {
HttpConnection con;
if (connections.size() == 0) {
/* DEBUG */log.info("Creating new connection");
con = new HttpConnection(client);
} else {
con = (HttpConnection) connections.elementAt(0);
connections.removeElementAt(0);
/* DEBUG */log.info("Reusing connection [remaining=" + connections.size() + "] " + con.toString());
con.verify();
}
/* DEBUG */log.info("Returning pooled connection");
return con;
}
/**
* Set the maximum size of the connection pool. Set to zero to place no
* restriction.
*
* @param maxPoolSize maximum pool size
*/
public synchronized void setMaxPoolSize(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
/**
* Release a connection back to the pool.
*
* @param connection to release
*/
public synchronized void releaseConnection(HttpConnection con) {
// Can we reuse this connection?
if (con.canReuse() && !con.isClosed()) {
if(maxPoolSize == 0 || connections.size() < maxPoolSize)
connections.addElement(con);
else
con.close();
/* DEBUG */log.info("Released connection added back to pool [remaining=" + connections.size() + "] " + con.toString());
// Set the last time this was accessed.
con.updateState();
} else {
/* DEBUG */log.info("Release connection will not be reused!");
}
}
/**
* Release all connections back to the pool.
*/
public synchronized void releaseAllConnections() {
for (Enumeration e1 = connections.elements(); e1.hasMoreElements();) {
HttpConnection con = (HttpConnection) e1.nextElement();
releaseConnection(con);
}
}
/**
* Check if a connection is closed or un-reusable and reconnect it
*
* @param con connection
* @throws UnknownHostException
* @throws IOException
* @throws HttpException
* @throws UnsupportedAuthenticationException
* @throws AuthenticationCancelledException
*/
public void checkConnection(HttpConnection con) throws UnknownHostException, IOException, HttpException,
UnsupportedAuthenticationException, AuthenticationCancelledException {
if (con.isClosed || !con.canReuse) {
con.reconnect();
}
}
/**
* Close all connections
*/
public synchronized void closeConnections() {
for (Enumeration e1 = connections.elements(); e1.hasMoreElements();) {
HttpConnection con = (HttpConnection) e1.nextElement();
con.close();
}
}
/**
* Get the number of connections in the pool
*
* @return number of pooled connections
*/
public synchronized int getConnectionCount() {
return connections.size();
}
/**
* Get the number of connections currently open.
*
* @return number of open connections
*/
public synchronized int getOpenConnectionCount() {
int open = 0;
for (Enumeration e1 = connections.elements(); e1.hasMoreElements();) {
HttpConnection con = (HttpConnection) e1.nextElement();
if (!con.isClosed()) {
open++;
}
}
return open;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -