连接池例子

来源:互联网 发布:网校源码第三方 编辑:程序博客网 时间:2024/05/17 07:03

/** * Manages a browser's connection pool * Creates, frees, and disconnects IRBrowserConnection objects so * the associated Browser can use them. Only this class should * set the busy/free/disconnected state of any connection. * * This class supports debug-level logging on com.empirix.lbrowser.http.ConnectionPool * @author rkuzsma */ public class ConnectionPool implements IEmpirixLogger { private RBrowser m_browser = null; private int m_maxConnections = -1; private IRBrowserConnection m_connectionObjects[] = null; private boolean m_stop = false; private Logger m_logger; private Object busyFlag = new Object(); private ArrayList m_alJobsWaitingForConnection = new ArrayList(); public ConnectionPool(RBrowser browser, int maxConnections) { m_browser = browser; resetLogger(); if (m_logger.isDebugEnabled()) m_logger.debug("Connection Pool " + this.hashCode() + " Created with " + maxConnections + " connection slots"); m_maxConnections = maxConnections; if (m_maxConnections < 1) m_maxConnections = 1; m_connectionObjects = new IRBrowserConnection[m_maxConnections]; } public void resetLogger(){ if(m_browser != null){ m_logger = LoggerHelper.getLogger(ConnectionPool.class.getName() + "." + m_browser.getBrowserID()); }else{ m_logger = LoggerHelper.getLogger(ConnectionPool.class.getName()); } if(m_connectionObjects != null){ for(int i = 0; i < m_connectionObjects.length; i++){ if(m_connectionObjects[i] != null){ m_connectionObjects[i].resetLogger(); } } } } /** * Return a new connection that can be used for the given job. * If no connections available, this method blocks until a job becomes available and returns it. * @param job Job for which a connection is sought * @param getAsynchronousConnection Must be set to False. * @return IRBrowserConnection ready to be used for the given job */ public IRBrowserConnection getConnection(HTTPJob job, boolean getAsynchronousConnection) { if (m_logger.isDebugEnabled()){ m_logger.debug(this.hashCode() + " getConnection() called for job " + (job == null?"null":job.getURLString())); } //assert(getAsynchronousConnection == false); // TODO: Support async connections later // Note: If all the connections are working on jobs that redirect (or cause authentication), // the browser will not set them free until all redirects have happened OR // until maxRedirects have happened. // Worst-case wait time if ALL connections in the pool are doing redirections or // authenticating is < maxRedirections*connectionTimeout // Connections won't timeout for a long time, and the browser needs to keep using the same connection w/out freeing it IRBrowserConnection conn; while (!m_stop) { synchronized(busyFlag) { conn = getAvailableConnection(job); if (conn != null) { m_logger.debug(this.hashCode() + " getConnection(" + (job == null?"null":job.hashCode()+"") + ") returned " + conn.hashCode()); return conn; } else { // All connections are busy now. Block until one becomes released try{ busyFlag.wait(); }catch(InterruptedException ignore){ } } } } // someone shut down the connection pool return null; } /** * Request a Connection to run a Job on. * As soon as a connection is available in the connection pool, ConnectionPool * calls the RBrowser's ConnectionAvailable() with an acceptible connection * it can immediately navigate on. * @param job */ public void getConnectionAsync(HTTPJob job) { IRBrowserConnection conn = getAvailableConnection(job); if (conn != null) { m_browser.ConnectionAvailable(conn, job); } else { synchronized(m_alJobsWaitingForConnection) { m_alJobsWaitingForConnection.add(job); } } return; } /** * Request a Connection to run a Job on * @param job * @return A connection suitable to run Job on, or NULL if none available now; a NULL job will return a new connection */ private synchronized IRBrowserConnection getAvailableConnection(HTTPJob job) { // Synchronized to lock access to m_connectionObjects while checking them // getAvailableConnection, closeAllConnections, and freeConnection are mutually exclusive if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " getConnection() called for job " + (job == null ? "null" : job.hashCode() + job.getURLString())); IRBrowserConnection conn, lastPossibleConn = null; for (int i = 0; i < m_maxConnections; i++) { conn = (IRBrowserConnection)m_connectionObjects[i]; if (conn == null) { // No connection created at this slot yet... use this one // Create a connection OBJECT and add it to the slot (avoid unnecessary gc) conn = new RSyncConnection(); conn.IRBrowserConnection_construct(m_browser, m_browser.getThreadPool(), m_browser.getConnectionHandler(), false); if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " Constructed Connection Object " + conn.hashCode() + ", added to slot " + i); m_connectionObjects[i] = conn; // Now CONNECT this connection and return it conn.IRBrowserConnection_setBusy(true); if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " Returning new not-yet-connected connection " + conn.hashCode()); return conn; } else { // Is the connection available for use? if (!conn.IRBrowserConnection_isBusy()) { // Connection may/may not be connected, but we don't care // Find the most ideal connection to use that will hopefully // take advantage of keep-alives if possible lastPossibleConn = conn; if (job != null && conn.IRBrowserConnection_isIdealForJob(job)) { conn.IRBrowserConnection_setBusy(true); if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " Returning ideal existing connection " + conn.hashCode() + " for job " + job.hashCode()); return conn; } } } } if (lastPossibleConn != null) { // No "ideal" or new connection was returned, so we'll choose a less optimal connection, // but at least one that's already been physically constructed in memory and is not being used by anyone else now // This connection should ALWAYS be disconnected since it's not ideal for the job if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " conn " + lastPossibleConn.hashCode() + " disconnecting; needed for new job"); lastPossibleConn.IRBrowserConnection_disconnect(); lastPossibleConn.IRBrowserConnection_setBusy(true); if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " Returned disconnected connection " + lastPossibleConn.hashCode() + " for job " + (job == null ? "null":job.hashCode()+"")); return lastPossibleConn; } // All connections are busy now. One should be released later. // TODO: Implement a connection available timeout scheduler to notify browser in case no connection released within X seconds if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " All connections busy for job " + (job == null ? "null":job.hashCode()+"")); return null; } /** * Give back a connection to the pool. The pool will decide if the connection * should be disconnected. The Browser should not decide this. * @param conn Connection to disconnect * @param immediately TRUE if the connection had a problem and must be closed immediately */ public void releaseConnection(IRBrowserConnection conn, boolean immediately) { if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " releaseConnection(" + ((conn==null)? "null" : new Integer(conn.hashCode()).toString()) + ", immediately=" + immediately + ")"); // Determine if we should disconnect or not if (conn == null) return; if (!immediately && conn.IRBrowserConnection_isConnected()) { if (conn.IRBrowserConnection_isKeepAlive()) { if ((conn.IRBrowserConnection_getKeepAliveMaxRequests() < 0) || // negative means Unlimited KeepAlive Requests (conn.IRBrowserConnection_getNumJobsRequested() < conn.IRBrowserConnection_getKeepAliveMaxRequests())) { // Do not disconnect if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " conn " + conn.hashCode() + " releasing, not disconnecting"); freeConnection(conn); return; } /* else if (conn.IRBrowserConnection_getKeepAliveTimeoutTime() < System.currentTimeMillis()) { // Do not disconnect if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " conn " + conn.hashCode() + " releasing, not disconnecting"); freeConnection(conn); return; } */ } } // Definitely disconnect this connection if (m_logger.isDebugEnabled()) m_logger.debug(this.hashCode() + " conn " + conn.hashCode() + " releasing and disconnecting"); conn.IRBrowserConnection_disconnect(); freeConnection(conn); return; } private void freeConnection(IRBrowserConnection connectionFreed) { synchronized(busyFlag) { // The busyflag is still used for blocking calls to getConnection() synchronized(this) { // Synchronize access to this method // freeConnection, closeAllConnections, and getAvailableConnection are mutually exclusive m_logger.debug(this.hashCode() + " freeConnection()"); connectionFreed.IRBrowserConnection_setBusy(false); // For calls to getConnectionAsync(), we need to notify browser // of any available connections synchronized(m_alJobsWaitingForConnection) { int numJobsWaitingForConnection = m_alJobsWaitingForConnection.size(); if (numJobsWaitingForConnection > 0) { HTTPJob job; IRBrowserConnection conn; for (int i = 0; i < numJobsWaitingForConnection; i++) { job = (HTTPJob)m_alJobsWaitingForConnection.get(i); conn = getAvailableConnection(job); if (conn != null) { m_alJobsWaitingForConnection.remove(i); m_logger.debug("m_browser.ConnectionAvailable(" + conn.hashCode() + "," + job.hashCode() + ")"); m_browser.ConnectionAvailable(conn, job); return; } } } } } // The busyflag is still used for blocking calls to getConnection() m_logger.debug(this.hashCode() + " connectionFreed(): notify"); busyFlag.notify(); // wake up this thread in case it's waiting for a connection } } /** * Use to shut down the connection pool immediately, * possibly disrupting anyone currently using the connections */ public void shutdown() { m_stop = true; synchronized (busyFlag) { busyFlag.notify(); } closeAllConnections(); m_stop = false; } private synchronized void closeAllConnections() { // Disconnect all connections IRBrowserConnection conn; for (int i = 0; i < m_maxConnections; i++) { conn = (IRBrowserConnection)m_connectionObjects[i]; if (conn != null) { conn.IRBrowserConnection_disconnect(); } } m_connectionObjects = new IRBrowserConnection[m_maxConnections]; } }

原创粉丝点击