Class ConnectionPool

java.lang.Object
org.serviceconnector.net.connection.ConnectionPool

public class ConnectionPool extends Object
The Class ConnectionPool. Concrete implementation of connection pooling.

This connection pool takes care of following listed points:
- creating / destroying of connections
- observing the max numbers of connections
- keeping a minimum of connections active
- disconnect connection after Constants.DEFAULT_NR_OF_KEEP_ALIVES_TO_CLOSE
- destroying connection pool, destroys all connections

optional functions:
- closing connection after getting it back
- initializing pool by starting a minimum of connections immediately
- observing connection idle timeout and sending keep alive messages to refresh firewall
- force closing of a specific connection, very useful if connection has a curious state
Author:
JTraber
  • Constructor Details

    • ConnectionPool

      public ConnectionPool(String host, int port, String conType, int keepAliveIntervalSeconds, int keepAliveOTIMillis)
      Instantiates a new connection pool.
      Parameters:
      host - the host
      port - the port
      conType - the connection type
      keepAliveIntervalSeconds - the keep alive interval
  • Method Details

    • getConnection

      public IConnection getConnection() throws Exception
      Gets a connection of the pool.
      Returns:
      the connection
      Throws:
      Exception - the exception
    • freeConnection

      public void freeConnection(IConnection connection)
      Free connection. Gives connection back for other interested parties.
      Parameters:
      connection - the connection
    • setMaxConnections

      public void setMaxConnections(int maxConnections)
      Sets the max connections for the pool.
      Parameters:
      maxConnections - the new max connections
    • destroy

      public void destroy()
      Destroy the pool.
    • forceClosingConnection

      public void forceClosingConnection(IConnection connection, boolean quietClose)
      Force closing specific connection.
      Parameters:
      connection - the connection
      quietClose - the quiet close
    • setCloseOnFree

      public void setCloseOnFree(boolean closeOnFree)
      Sets the close on free. Indicates that connection should be closed at the time they get freed.
      Parameters:
      closeOnFree - the new close on free
    • setCloseAfterKeepAlive

      public void setCloseAfterKeepAlive(boolean closeAfterKeepAlive)
      Sets the close after keep alive. Indicates that a connection gets closed after Constants.DEFAULT_NR_OF_KEEP_ALIVES_TO_CLOSE.
      Parameters:
      closeAfterKeepAlive - the new close after keep alive
    • setMinConnections

      public void setMinConnections(int minConnections)
      Sets the minimum connections for the pool.
      Parameters:
      minConnections - the new minimum connections
    • initMinConnections

      public void initMinConnections()
      Initiates the minimum connections. The minimum of connections gets active immediately.
    • getMaxConnections

      public int getMaxConnections()
      Gets the max connections.
      Returns:
      the max connections
    • getMinConnections

      public int getMinConnections()
      Gets the min connections.
      Returns:
      the min connections
    • getBusyConnections

      public int getBusyConnections()
      Gets the number of busy connections at this time.
      Returns:
      the busy connections
    • hasFreeConnections

      public boolean hasFreeConnections()
      Checks for free connections in the pool.
      Returns:
      true, if successful
    • connectionIdle

      public void connectionIdle(IConnection connection)
      Connection idle. Process idle event of connection. Sending of keep alive does not need to be synchronized.
      Parameters:
      connection - the connection
    • getKeepAliveInterval

      public int getKeepAliveInterval()
      Gets the keep alive interval the pool is observing. 0 means disabled.
      Returns:
      the keep alive interval
    • getHost

      public String getHost()
      Gets the host.
      Returns:
      the host
    • getPort

      public int getPort()
      Gets the port.
      Returns:
      the port
    • getFreeConnections

      public List<IConnection> getFreeConnections()
      Gets the free connections.
      Returns:
      the free connections
    • getUsedConnections

      public List<IConnection> getUsedConnections()
      Gets the used connections.
      Returns:
      the used connections
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • dump

      public void dump(XMLDumpWriter writer) throws Exception
      Dump the connection pool into the xml writer.
      Parameters:
      writer - the writer
      Throws:
      Exception - the exception