StreamConnectionPoolpublic class StreamConnectionPool extends Object This class implements the necessary functionality for the HTTP persistent
connection pool. This class contains individual http connection elements
(or containers) including whether or/not a that particular connection is
active. If a connection becomes inactive - its is available for reuse by
another http connection session or the same one. This reduces the connection
time for each supsequent connection. |
Fields Summary |
---|
private long | m_connectionLingerTimeHow long a connection can linger after its last use. | private Vector | m_connectionsinternal connection hash table | private int | m_max_connectionsmaximum connections |
Constructors Summary |
---|
StreamConnectionPool(int number_of_connections, long connectionLingerTime)Create a new instance of this class.
We are initially unconnected this will create the
first connection.
this.m_max_connections = number_of_connections;
this.m_connectionLingerTime = connectionLingerTime;
m_connections = new Vector(m_max_connections);
|
Methods Summary |
---|
synchronized boolean | add(java.lang.String p_protocol, java.lang.String p_host, int p_port, javax.microedition.io.StreamConnection sc, java.io.DataOutputStream dos, java.io.DataInputStream dis)Tries to add a reuseable connection to the connection pool.
Replace any not in use connections to the same host and port.
Will not add to the same host and port in use. Will replace
oldest not in use element (if any) if the pool is full.
StreamConnectionElement oldestNotInUse = null;
// find the last unused element
Enumeration cons = m_connections.elements();
while (cons.hasMoreElements()) {
StreamConnectionElement sce =
(StreamConnectionElement)cons.nextElement();
if (sce.m_in_use) {
if (p_host.equals(sce.m_host) && p_port == sce.m_port) {
return false;
}
continue;
}
// if the connection is a duplicate, delete it
if (p_host.equals(sce.m_host) && p_port == sce.m_port) {
// no protocol duplicates on host and port
sce.close();
m_connections.removeElement(sce);
break;
} else {
if (oldestNotInUse == null ||
sce.m_time < oldestNotInUse.m_time) {
// save the oldest not in use, it may be removed later
oldestNotInUse = sce;
}
}
}
/*
* first check and see if the maximum number of connections
* has been reached - if so delete the first one in the list (FIFO)
* or
* if this port and host are already in the pool.
*/
if (m_connections.size() >= m_max_connections) {
if (oldestNotInUse == null) {
return false;
}
oldestNotInUse.close();
m_connections.removeElement(oldestNotInUse);
}
m_connections.addElement(new StreamConnectionElement(p_protocol,
p_host, p_port, sc, dos, dis));
return true;
| synchronized StreamConnectionElement | get(java.lang.String p_protocol, java.lang.String p_host, int p_port)get an available connection and set the boolean flag to
true (unavailable) in the connection pool.
Also removes any stale connections, since this method gets
called more than add or remove.
StreamConnectionElement result = null;
long c_time = System.currentTimeMillis();
Enumeration cons = m_connections.elements();
while (cons.hasMoreElements()) {
StreamConnectionElement sce =
(StreamConnectionElement)cons.nextElement();
if ((c_time - sce.m_time) > m_connectionLingerTime) {
if (!sce.m_in_use) {
sce.close();
} else {
// signal returnToUse() to close
sce.m_removed = true;
}
m_connections.removeElement(sce);
continue;
}
if (p_host.equals(sce.m_host) && p_port == sce.m_port &&
p_protocol.equals(sce.m_protocol) && !sce.m_in_use) {
result = sce;
// do not break out so old connections can be removed
continue;
}
}
if (result != null) {
result.m_in_use = true;
}
return result;
| synchronized void | remove(StreamConnectionElement sce)Close connection and remove an instance of the stream connection
element from the connection pool.
sce.close();
m_connections.removeElement(sce);
| synchronized void | returnForReuse(StreamConnectionElement returned)Return an instance of the stream connection element to the
connection pool so it can be reused. It is done in the method
so it can be synchronized with the get method.
returned.m_in_use = false;
if (returned.m_removed) {
// the connection was out too long
returned.close();
return;
}
returned.m_time = System.currentTimeMillis();
|
|