org.pf.net
Class ConnectionPool

java.lang.Object
  extended by org.pf.util.ExceptionIgnorer
      extended by org.pf.net.ConnectionPool

public class ConnectionPool
extends ExceptionIgnorer

This class provides a mechanism to reuse established connections rather than creating new ones every time. This results in a significant performance increase.

Version:
1.0
Author:
Manfred Duchrow

Constructor Summary
ConnectionPool(java.lang.String hostname, int port)
          Initialize the new instance with the hostname and the port of the server to connect to.
ConnectionPool(java.lang.String hostname, int port, ExceptionHandler exHandler)
          Initialize the new instance with the hostname and the port of the server to connect to.
ConnectionPool(java.lang.String hostname, int port, int poolLimit)
          Initialize the new instance with the hostname and the port of the server to connect to.
ConnectionPool(java.lang.String hostname, int port, int poolLimit, ExceptionHandler exHandler)
          Initialize the new instance with the hostname and the port of the server to connect to.
 
Method Summary
 void createConnections(int numberOfConnections)
          Creates connections ready to be used in the pool.
 TextSocketConnection getConnection()
          Returns a connection based on the underlying socket.
 int numberOfPooledConnections()
          Returns how many connections are in the pool
 void returnConnection(TextSocketConnection connection)
           
 
Methods inherited from class org.pf.util.ExceptionIgnorer
exceptionOccurred, getExceptionHandler, setExceptionHandler
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(java.lang.String hostname,
                      int port)
Initialize the new instance with the hostname and the port of the server to connect to. It uses the default pool limit of 10.

Parameters:
hostname - The name of the server to which the pool should connect to
port - The port on the host on which the server process is listening

ConnectionPool

public ConnectionPool(java.lang.String hostname,
                      int port,
                      int poolLimit)
Initialize the new instance with the hostname and the port of the server to connect to. It also initializes the pool to a maximum of connections that can be pooled.

Parameters:
hostname - The name of the server to which the pool should connect to
port - The port on the host on which the server process is listening
poolLimit - The maximum number of connections to be pooled ( must be > 1 )

ConnectionPool

public ConnectionPool(java.lang.String hostname,
                      int port,
                      ExceptionHandler exHandler)
Initialize the new instance with the hostname and the port of the server to connect to. It uses the default pool limit of 10.

Parameters:
hostname - The name of the server to which the pool should connect to
port - The port on the host on which the server process is listening
exHandler - An optional handler that gets called for all occuring exceptions

ConnectionPool

public ConnectionPool(java.lang.String hostname,
                      int port,
                      int poolLimit,
                      ExceptionHandler exHandler)
Initialize the new instance with the hostname and the port of the server to connect to.

Parameters:
hostname - The name of the server to which the pool should connect to
port - The port on the host on which the server process is listening
poolLimit - The maximum number of connections to be pooled ( must be > 1 )
exHandler - An optional handler that gets called for all occuring exceptions
Method Detail

getConnection

public TextSocketConnection getConnection()
Returns a connection based on the underlying socket. Tries to reuse connections as much as possible. If no connection can be reused, a new connection will be established, if the limit of the connection pool is not yet reached.


returnConnection

public void returnConnection(TextSocketConnection connection)

createConnections

public void createConnections(int numberOfConnections)
Creates connections ready to be used in the pool. This method should only be called right after the instantiation of the connection pool. It must not be called, when the pool is already in use !


numberOfPooledConnections

public int numberOfPooledConnections()
Returns how many connections are in the pool