org.xsocket.stream
Class NonBlockingConnectionPool

Package class diagram package NonBlockingConnectionPool
java.lang.Object
  extended by org.xsocket.stream.NonBlockingConnectionPool
All Implemented Interfaces:
IConnectionPool

public final class NonBlockingConnectionPool
extends java.lang.Object

A connection pool implementation. A connection pool will be used on the client-side, if connections for the same server (address) will be created in a serial manner in a sort period of time. By pooling such connections the overhead of establishing a connection will be avoided For pool management reasons, timeouts can be defined. The IdleTimeout defines the max idle time in the pool. After this time the free connection will be closed. In the same way, the max living time defines the timout of the connection. If a free connection exceeds this time, the connection will be closed.
Additional the max size of the active connections can be defined. If a connection is requested and the max limit of the active connection is reached, the request will be blocked until a connection becomes free or the maxWaitTime will be reached.

  // create  a connection pool with idle timeout 60 sec
  NonBlockingConnectionPool pool = new NonBlockingConnectionPool(60L * 1000L);

  INonBlockingCinnection con = null;

  try {
     // retrieve a connection (if no connection is in pool, a new one will be created)
     con = pool.getNonBlockingConnection(host, port);
     con.write("Hello");
     ...

     // always close the connection! (the connection will be returned into the connection pool)
     con.close();

        } catch (IOException) {
     if (con != null) {
        try {
          // if the connection is invalid -> destroy it (it will not return into the pool)
          pool.destroyConnection(con);
        } catch (Exception ignore) { }
     }
  }
 


Field Summary
static long UNLIMITED_TIMEOUT
           
 
Constructor Summary
NonBlockingConnectionPool(long timeToIdleMillis)
          constructor
NonBlockingConnectionPool(long timeToIdleMillis, int maxActive, long maxWaitTimeMillis)
           
 
Method Summary
 void addListener(ILifeCycle listener)
          adds a listener
 void close()
          closes the connection pool.
 void destroyConnection(IConnection connection)
          destroy the given connection.
 long getIdleTimeoutMillis()
          get the idle timeout for pooled connections
 long getLifeTimeoutMillis()
          get the life timeout
 int getMaxActive()
          return the number of max active connections
 int getMaxIdle()
          get the number of max idle connections
 long getMaxWaitMillis()
          get the max wait time to get a free connection by calling the getXXXConnection().
 INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address, int port)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address, int port, IHandler appHandler)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address, int port, IHandler appHandler, java.util.concurrent.Executor workerPool)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(java.lang.String host, int port)
          get a pool connection for the given address.
 int getNumActive()
          get the number of the active (borrowed) connects
 int getNumIdle()
          get the number of idling connections connections
 boolean removeListener(ILifeCycle listener)
          removes a listener
 void setIdleTimeoutMillis(long idleTimeoutMillis)
          set the idle timeout for pooled connections
 void setLifeTimeoutMillis(long lifeTimeoutMillis)
          set the life timeout
 void setMaxActive(int maxActive)
          set the number of max active connections
 void setMaxIdle(int maxIdle)
          set the number of max idle connections
 void setMaxWaitMillis(long maxWaitMillis)
          set the max wait time to get a free connection by calling the getXXXConnection().
 java.lang.String toString()
          
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNLIMITED_TIMEOUT

public static final long UNLIMITED_TIMEOUT
See Also:
Constant Field Values
Constructor Detail

NonBlockingConnectionPool

public NonBlockingConnectionPool(long timeToIdleMillis)
constructor

Parameters:
timeToIdleMillis - the max idle time in the pool. After this time the free connection will be closed

NonBlockingConnectionPool

public NonBlockingConnectionPool(long timeToIdleMillis,
                                 int maxActive,
                                 long maxWaitTimeMillis)
Method Detail

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(java.lang.String host,
                                                       int port)
                                                throws java.io.IOException,
                                                       WaitTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

This method is thread safe

Parameters:
host - the server address
port - the sever port
Returns:
the connection
Throws:
WaitTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
java.io.IOException - if an exception occurs

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address,
                                                       int port)
                                                throws java.io.IOException,
                                                       WaitTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

This method is thread safe

Parameters:
address - the server address
port - the sever port
Returns:
the connection
Throws:
WaitTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
java.io.IOException - if an exception occurs

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address,
                                                       int port,
                                                       IHandler appHandler)
                                                throws java.io.IOException,
                                                       WaitTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

This method is thread safe

Parameters:
address - the server address
port - the sever port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
Returns:
the connection
Throws:
WaitTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
java.io.IOException - if an exception occurs

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address,
                                                       int port,
                                                       IHandler appHandler,
                                                       java.util.concurrent.Executor workerPool)
                                                throws java.io.IOException,
                                                       WaitTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

This method is thread safe

Parameters:
address - the server address
port - the sever port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
workerPool - the worker pool to use
Returns:
the connection
Throws:
WaitTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
java.io.IOException - if an exception occurs

destroyConnection

public final void destroyConnection(IConnection connection)
                             throws java.io.IOException
destroy the given connection. This connection will not return into the pool. It will be really closed instead. A leased connection should be destroyed by this way, if it is clear that the connection has become invalid.

This method is thread safe

Specified by:
destroyConnection in interface IConnectionPool
Parameters:
connection - the leased connection
Throws:
java.io.IOException - if an exception occurs

addListener

public void addListener(ILifeCycle listener)
adds a listener

Parameters:
listener - gthe listener to add

removeListener

public boolean removeListener(ILifeCycle listener)
removes a listener

Parameters:
listener - the listener to remove
Returns:
true, is the listener has been removed

getMaxActive

public final int getMaxActive()
return the number of max active connections

Specified by:
getMaxActive in interface IConnectionPool
Returns:
the number of max active connections

setMaxActive

public final void setMaxActive(int maxActive)
set the number of max active connections

Specified by:
setMaxActive in interface IConnectionPool
Parameters:
maxActive - the number of max active connections

getMaxWaitMillis

public final long getMaxWaitMillis()
get the max wait time to get a free connection by calling the getXXXConnection().

Specified by:
getMaxWaitMillis in interface IConnectionPool
Returns:
the max wait time to get a free connection

setMaxWaitMillis

public final void setMaxWaitMillis(long maxWaitMillis)
set the max wait time to get a free connection by calling the getXXXConnection().

Specified by:
setMaxWaitMillis in interface IConnectionPool
Parameters:
maxWaitMillis - the max wait time to get a free connection

getMaxIdle

public final int getMaxIdle()
get the number of max idle connections

Specified by:
getMaxIdle in interface IConnectionPool
Returns:
the number of max idle connections

setMaxIdle

public final void setMaxIdle(int maxIdle)
set the number of max idle connections

Specified by:
setMaxIdle in interface IConnectionPool
Parameters:
maxIdle - the number of max idle connections

getNumActive

public final int getNumActive()
get the number of the active (borrowed) connects

Specified by:
getNumActive in interface IConnectionPool
Returns:
the number of the active connects

getNumIdle

public final int getNumIdle()
get the number of idling connections connections

Specified by:
getNumIdle in interface IConnectionPool
Returns:
the number of idling connections connections

getIdleTimeoutMillis

public final long getIdleTimeoutMillis()
get the idle timeout for pooled connections

Specified by:
getIdleTimeoutMillis in interface IConnectionPool
Returns:
the idle timeout for pooled connections

setIdleTimeoutMillis

public final void setIdleTimeoutMillis(long idleTimeoutMillis)
set the idle timeout for pooled connections

Specified by:
setIdleTimeoutMillis in interface IConnectionPool
Parameters:
idleTimeoutMillis - the idle timeout for pooled connections

getLifeTimeoutMillis

public final long getLifeTimeoutMillis()
get the life timeout

Specified by:
getLifeTimeoutMillis in interface IConnectionPool
Returns:
the life timeout

setLifeTimeoutMillis

public final void setLifeTimeoutMillis(long lifeTimeoutMillis)
set the life timeout

Specified by:
setLifeTimeoutMillis in interface IConnectionPool
Parameters:
lifeTimeoutMillis - the life timeout

close

public final void close()
closes the connection pool. All free connection of the pool will be closed

This method is thread safe

Specified by:
close in interface IConnectionPool

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object