KeepAliveCache.java revision 28
486N/A * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. 486N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 486N/A * This code is free software; you can redistribute it and/or modify it 486N/A * under the terms of the GNU General Public License version 2 only, as 486N/A * published by the Free Software Foundation. Sun designates this 486N/A * particular file as subject to the "Classpath" exception as provided 486N/A * by Sun in the LICENSE file that accompanied this code. 486N/A * This code is distributed in the hope that it will be useful, but WITHOUT 486N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 486N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 486N/A * version 2 for more details (a copy is included in the LICENSE file that 486N/A * accompanied this code). 486N/A * You should have received a copy of the GNU General Public License version 486N/A * 2 along with this work; if not, write to the Free Software Foundation, 486N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 486N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 486N/A * CA 95054 USA or visit www.sun.com if you need additional information or 2976N/A * A class that implements a cache of idle Http connections for keep-alive 486N/A * @author Stephen R. Pietrowicz (NCSA) /* maximum # keep-alive connections to maintain at once * This should be 2 by the HTTP spec, but because we don't support pipe-lining * a larger value is more appropriate. So we now set a default of 5, and the value * refers to the number of idle connections per destination (in the cache) only. * It can be reset by setting system property "http.maxConnections". * Register this URL and HttpClient (that supports keep-alive) with the cache * @param url The URL contains info about the host and port * @param http The HttpClient to be cached /* Unfortunately, we can't always believe the keep-alive timeout we got * back from the server. If I'm connected through a Netscape proxy * to a server that sent me a keep-alive * time of 15 sec, the proxy unilaterally terminates my connection * The robustness to get around this is in HttpClient.parseHTTP() // We want to create the Keep-Alive-Timer in the /* remove an obsolete HttpClient from its VectorCache */ /* called by a clientVector thread when all its connections have timed out * and that vector of connections should be removed. * Check to see if this URL has a cached HttpClient if (v ==
null) {
// nothing in cache yet /* Sleeps for an alloted timeout, then checks for timed out connections. * Errs on the side of caution (leave connections idle for a relatively /* Remove all unused HttpClients. Starting from the * bottom of the stack (the least-recently used first). * REMIND: It'd be nice to not remove *all* connections * that aren't presently in use. One could have been added * a second ago that's still perfectly valid, and we're * needlessly axing it. But it's not clear how to do this * cleanly, and doing it right may be more trouble than it's for (i =
0; i < v.
size(); i++) {
* Do not serialize this class! /* FILO order for recycling HttpClients, should run in a thread * to time them out. If > maxConns are in use, block. // sleep time in milliseconds, before cache clear // Loop until we find a connection that has not timed out /* return a still valid, unused HttpClient */ h.
closeServer();
// otherwise the connection remains in limbo * Do not serialize this class! private Object obj =
null;
// additional key, such as socketfactory * @param url the URL containing the protocol, host and port information * Determine whether or not two objects of this type are equal * The hashCode() for this object is the string hashCode() of * concatenation of the protocol, host name and port.