edu.emory.mathcs.util.net
Class ConnectionPool

java.lang.Object
  extended byedu.emory.mathcs.util.net.ConnectionPool

public class ConnectionPool
extends java.lang.Object

Manages a pool of socket connections to a single network endpoint. Pooling enables reusing connections for multiple, unrelated data transfers, and it can be used to implement certain connection-based protocols like HTTP 1.1. Additionally, pooling can aid in controlling network load - limiting the maximum pool size causes excessive connection requests to be enqueued at the client side.

The endpoint is represented by a host name and a port number, as well as by an optional client socket factory, specified at the construction time. Client requests connections, use them, then return them to the pool. Clients should not close the socket associated with the connection or use the socket after returning connection to the pool. Upon a request for connection, the pool first tries to return a pre-existing idle one, creating a new connection only if none is available. Request may block if pool size limit is reached and all connections are in use. After being returned to the pool, if connection idles for longer than its expiration timeout, it is closed.

Example:

 ConnectionPool pool = new ConnectionPool(host, port);
 ...
 Connection conn = pool.getConnection();
 Socket socket = conn.getSocket();
 try {
     socket.getOutputStream().write(0x00);
     ...
     conn.returnToPool();
 }
 catch (IOException e) {
     conn.close();
 }
 

Version:
1.0
Author:
Dawid Kurzyniec, Tomasz Janiak

Constructor Summary
ConnectionPool(java.lang.String hostName, int port)
          Creates a connection pool for a specified endpoint, using a default TCP/IP socket factory, a default expiration timeout of 15 s, and a default capacity of 10 connections.
ConnectionPool(java.lang.String hostName, int port, long expirationTimeout, int capacity)
          Creates a connection pool for a specified endpoint, using specified expiration timeout and capacity and a default TCP/IP socket factory.
ConnectionPool(java.lang.String hostName, int port, java.rmi.server.RMIClientSocketFactory socketFactory)
          Creates a connection pool for a specified endpoint, using specified socket factory and a default expiration timeout of 15 s and a default capacity of 10 connections.
ConnectionPool(java.lang.String hostName, int port, java.rmi.server.RMIClientSocketFactory socketFactory, long expirationTimeout, int capacity)
          Creates a connection pool for a specified endpoint, using specified socket factory, expiration timeout, and capacity.
 
Method Summary
 Connection getConnection()
          Requests a connection from the pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(java.lang.String hostName,
                      int port)
Creates a connection pool for a specified endpoint, using a default TCP/IP socket factory, a default expiration timeout of 15 s, and a default capacity of 10 connections.

Parameters:
hostName - remote host name
port - remote port

ConnectionPool

public ConnectionPool(java.lang.String hostName,
                      int port,
                      java.rmi.server.RMIClientSocketFactory socketFactory)
Creates a connection pool for a specified endpoint, using specified socket factory and a default expiration timeout of 15 s and a default capacity of 10 connections.

Parameters:
hostName - remote host name
port - remote port
socketFactory - socket factory to use when creating new connections

ConnectionPool

public ConnectionPool(java.lang.String hostName,
                      int port,
                      long expirationTimeout,
                      int capacity)
Creates a connection pool for a specified endpoint, using specified expiration timeout and capacity and a default TCP/IP socket factory.

Parameters:
hostName - remote host name
port - remote port
expirationTimeout - maximum connection idle time
capacity - maximum number of active connections

ConnectionPool

public ConnectionPool(java.lang.String hostName,
                      int port,
                      java.rmi.server.RMIClientSocketFactory socketFactory,
                      long expirationTimeout,
                      int capacity)
Creates a connection pool for a specified endpoint, using specified socket factory, expiration timeout, and capacity.

Parameters:
hostName - remote host name
port - remote port
socketFactory - socket factory to use when creating new connections
expirationTimeout - maximum connection idle time
capacity - maximum number of active connections
Method Detail

getConnection

public Connection getConnection()
                         throws java.io.IOException,
                                java.lang.InterruptedException
Requests a connection from the pool. If an existing idle connection is found, it is returned. Otherwise, if pool capacity has not been reached, new connection is created. Otherwise, the operation blocks until a connection is available.

Returns:
a connection
Throws:
java.io.IOException - if I/O error occurs
java.lang.InterruptedException - if interrupted while waiting for a connection