Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util.net
Class ConnectionPool  view ConnectionPool download ConnectionPool.java

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

Field Summary
private  int capacity
           
private  java.util.HashSet connections
           
(package private) static int DEFAULT_CAPACITY
           
(package private) static long DEFAULT_EXPIRATION_TIMEOUT
           
(package private)  long expirationTimeout
           
private  java.lang.String hostName
           
private  int port
           
private  java.rmi.server.RMIClientSocketFactory socketFactory
           
 
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
private  void checkConnectPermission()
           
private  Connection findConnection()
           
 Connection getConnection()
          Requests a connection from the pool.
(package private)  void notifyConnectionStateChanged()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_EXPIRATION_TIMEOUT

static final long DEFAULT_EXPIRATION_TIMEOUT
See Also:
Constant Field Values

DEFAULT_CAPACITY

static final int DEFAULT_CAPACITY
See Also:
Constant Field Values

connections

private final java.util.HashSet connections

hostName

private final java.lang.String hostName

port

private final int port

socketFactory

private final java.rmi.server.RMIClientSocketFactory socketFactory

expirationTimeout

final long expirationTimeout

capacity

private final int capacity
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.


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.


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.


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.

Method Detail

findConnection

private Connection findConnection()

notifyConnectionStateChanged

void notifyConnectionStateChanged()

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.


checkConnectPermission

private void checkConnectPermission()