org.xsocket.connection
Class NonBlockingConnectionPool

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

public final class NonBlockingConnectionPool
extends Object
implements IConnectionPool

A connection pool implementation.

This class is thread safe

  // create a unbound connection pool
  NonBlockingConnectionPool pool = new NonBlockingConnectionPool();

  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 e) {
     if (con != null) {
        try {
          // if the connection is invalid -> destroy it (it will not return into the pool)
          pool.destroy(con);
        } catch (Exception ignore) { }
     }
  }
 

Author:
grro@xsocket.org

Field Summary
protected static int MAX_SIZE
           
protected static int MAX_TIMEOUT
           
protected static long MIN_CHECKPERIOD_MILLIS
           
 
Fields inherited from interface org.xsocket.connection.IConnectionPool
DEFAULT_CREATION_TIMEOUT_MILLIS, DEFAULT_IDLE_TIMEOUT_MILLIS, DEFAULT_LIFE_TIMEOUT_MILLIS, DEFAULT_MAX_ACTIVE, DEFAULT_MAX_IDLE, DEFAULT_MAX_WAIT_MILLIS
 
Constructor Summary
NonBlockingConnectionPool()
          constructor
NonBlockingConnectionPool(SSLContext sslContext)
          constructor
 
Method Summary
 void addListener(ILifeCycle listener)
          adds a listener
 void close()
          
static void destroy(INonBlockingConnection connection)
          
 List<String> getActiveConnectionInfos()
          get a info list about the active connections
 long getCreationMaxWaitMillis()
          get the max waiting by retrieving a resource
 List<String> getIdleConnectionInfos()
          get a info list about the idle connections
 int getMaxActive()
          return the number of max active resources
 int getMaxIdle()
          get the number of max idling resources
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, IHandler appHandler)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, IHandler appHandler, Executor workerPool, int connectTimeoutMillis)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, IHandler appHandler, Executor workerPool, int connectTimeoutMillis, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, int connectTimeoutMillis)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(InetAddress address, int port, int connectTimeoutMillis, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(String host, int port)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(String host, int port, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(String host, int port, IHandler appHandler)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(String host, int port, IHandler appHandler, boolean isSSL)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(String host, int port, int connectTimeoutMillis)
          get a pool connection for the given address.
 INonBlockingConnection getNonBlockingConnection(String host, int port, int connectTimeoutMillis, boolean isSSL)
          get a pool connection for the given address.
 int getNumActive()
          get the current number of the active resources
 int getNumCreated()
          get the number of the created resources
 int getNumCreationError()
          get the number of the creation errors
 int getNumDestroyed()
          get the number of the destroyed resources
 int getNumIdle()
          get the current number of idling resources
 int getNumPendingGet()
          get the current number of pending get operations to retrieve a resource
 int getNumTimeoutConnectionIdle()
          
 int getNumTimeoutConnectionLifetime()
          
 int getNumTimeoutPooledMaxIdleTime()
          get the number of timeouts caused by the pool idle timeout
 int getNumTimeoutPooledMaxLifeTime()
          get the number of timeouts caused by the pool life timeout
 int getPooledMaxIdleTimeMillis()
          get the idle time out
 int getPooledMaxLifeTimeMillis()
          get the life timeout of a resource
 Executor getWorkerpool()
          get the worker pool which will be assigned to the connections for call back handling
 boolean isOpen()
          returns true, is pool is open
 boolean removeListener(ILifeCycle listener)
          removes a listener
 void setCreationMaxWaitMillis(long maxWaitMillis)
          set the max waiting by retrieving a resource
 void setMaxActive(int maxActive)
          set the number of max active resources
 void setMaxIdle(int maxIdle)
          set the number of max idling resources
 void setPooledMaxIdleTimeMillis(int idleTimeoutMillis)
          set the idle time out of a resource within the pool
 void setPooledMaxLifeTimeMillis(int lifeTimeoutMillis)
          set the life timeout of a resource
 void setWorkerpool(Executor workerpool)
          set the worker pool which will be assigned to the connections for call back handling
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MIN_CHECKPERIOD_MILLIS

protected static final long MIN_CHECKPERIOD_MILLIS
See Also:
Constant Field Values

MAX_SIZE

protected static final int MAX_SIZE
See Also:
Constant Field Values

MAX_TIMEOUT

protected static final int MAX_TIMEOUT
See Also:
Constant Field Values
Constructor Detail

NonBlockingConnectionPool

public NonBlockingConnectionPool()
constructor


NonBlockingConnectionPool

public NonBlockingConnectionPool(SSLContext sslContext)
constructor

Parameters:
sslContext - the ssl context or null if ssl should not be used
Method Detail

getNonBlockingConnection

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

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

getNonBlockingConnection

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

Parameters:
host - the server address
port - the server port
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

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

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(String host,
                                                       int port,
                                                       int connectTimeoutMillis,
                                                       boolean isSSL)
                                                throws IOException,
                                                       SocketTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

Parameters:
host - the server address
port - the server port
connectTimeoutMillis - the connection timeout
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

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

getNonBlockingConnection

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

Parameters:
address - the server address
port - the sever port
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

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

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(InetAddress address,
                                                       int port,
                                                       int connectTimeoutMillis,
                                                       boolean isSSL)
                                                throws IOException,
                                                       SocketTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

Parameters:
address - the server address
port - the server port
connectTimeoutMillis - the connection timeout
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

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

getNonBlockingConnection

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

Parameters:
address - the server address
port - the server port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

Parameters:
host - the server address
port - the server port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(String host,
                                                       int port,
                                                       IHandler appHandler,
                                                       boolean isSSL)
                                                throws IOException,
                                                       SocketTimeoutException
get a pool connection for the given address. If no free connection is in the pool, a new one will be created

Parameters:
host - the server address
port - the server port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

Parameters:
address - the server address
port - the server port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
connectTimeoutMillis - the connection creation timeout
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

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

Parameters:
address - the server address
port - the server port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
connectTimeoutMillis - the connection creation timeout
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(InetAddress address,
                                                       int port,
                                                       IHandler appHandler,
                                                       Executor workerPool,
                                                       int connectTimeoutMillis)
                                                throws IOException,
                                                       SocketTimeoutException
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, IIdleTimeoutHandler and ConnectionTimeoutHandler)
workerPool - the worker pool to use
connectTimeoutMillis - the connection creation timeout
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

getNonBlockingConnection

public INonBlockingConnection getNonBlockingConnection(InetAddress address,
                                                       int port,
                                                       IHandler appHandler,
                                                       Executor workerPool,
                                                       int connectTimeoutMillis,
                                                       boolean isSSL)
                                                throws IOException,
                                                       SocketTimeoutException
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, IIdleTimeoutHandler and ConnectionTimeoutHandler)
workerPool - the worker pool to use
connectTimeoutMillis - the connection creation timeout
isSSL - true, if ssl connection
Returns:
the connection
Throws:
SocketTimeoutException - if the wait timeout has been reached (this will only been thrown if wait time has been set)
IOException - if an exception occurs

isOpen

public boolean isOpen()
returns true, is pool is open

Specified by:
isOpen in interface IConnectionPool
Returns:
true, is pool is open

addListener

public void addListener(ILifeCycle listener)
adds a listener

Specified by:
addListener in interface IConnectionPool
Parameters:
listener - the listener to add

removeListener

public boolean removeListener(ILifeCycle listener)
removes a listener

Specified by:
removeListener in interface IConnectionPool
Parameters:
listener - the listener to remove
Returns:
true, is the listener has been removed

setWorkerpool

public void setWorkerpool(Executor workerpool)
set the worker pool which will be assigned to the connections for call back handling

Parameters:
workerpool - the worker pool

getWorkerpool

public Executor getWorkerpool()
get the worker pool which will be assigned to the connections for call back handling

Returns:
the worker pool

getMaxActive

public int getMaxActive()
return the number of max active resources

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

setMaxActive

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

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

getCreationMaxWaitMillis

public long getCreationMaxWaitMillis()
get the max waiting by retrieving a resource

Specified by:
getCreationMaxWaitMillis in interface IConnectionPool
Returns:
the max waiting by retrieving a resource

setCreationMaxWaitMillis

public void setCreationMaxWaitMillis(long maxWaitMillis)
set the max waiting by retrieving a resource

Specified by:
setCreationMaxWaitMillis in interface IConnectionPool
Parameters:
maxWaitMillis - the max waiting by retrieving a resource

getMaxIdle

public int getMaxIdle()
get the number of max idling resources

Specified by:
getMaxIdle in interface IConnectionPool
Returns:
the number of max idling resources

setMaxIdle

public void setMaxIdle(int maxIdle)
set the number of max idling resources

Specified by:
setMaxIdle in interface IConnectionPool

getNumActive

public int getNumActive()
get the current number of the active resources

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

getActiveConnectionInfos

public List<String> getActiveConnectionInfos()
Description copied from interface: IConnectionPool
get a info list about the active connections

Specified by:
getActiveConnectionInfos in interface IConnectionPool
Returns:
a info list about the active connection

getIdleConnectionInfos

public List<String> getIdleConnectionInfos()
Description copied from interface: IConnectionPool
get a info list about the idle connections

Specified by:
getIdleConnectionInfos in interface IConnectionPool
Returns:
a info list about the idle connections

getNumIdle

public int getNumIdle()
get the current number of idling resources

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

getNumCreated

public int getNumCreated()
get the number of the created resources

Specified by:
getNumCreated in interface IConnectionPool
Returns:
the number of the created resources

getNumCreationError

public int getNumCreationError()
get the number of the creation errors

Returns:
the number of creation errors

getNumDestroyed

public int getNumDestroyed()
get the number of the destroyed resources

Specified by:
getNumDestroyed in interface IConnectionPool
Returns:
the number of the destroyed resources

getNumTimeoutConnectionLifetime

public int getNumTimeoutConnectionLifetime()


getNumTimeoutConnectionIdle

public int getNumTimeoutConnectionIdle()


getNumTimeoutPooledMaxIdleTime

public int getNumTimeoutPooledMaxIdleTime()
get the number of timeouts caused by the pool idle timeout

Specified by:
getNumTimeoutPooledMaxIdleTime in interface IConnectionPool
Returns:
the number of timeouts

getNumTimeoutPooledMaxLifeTime

public int getNumTimeoutPooledMaxLifeTime()
get the number of timeouts caused by the pool life timeout

Specified by:
getNumTimeoutPooledMaxLifeTime in interface IConnectionPool
Returns:
the number of timeouts

getPooledMaxIdleTimeMillis

public int getPooledMaxIdleTimeMillis()
get the idle time out

Specified by:
getPooledMaxIdleTimeMillis in interface IConnectionPool
Returns:
the idle time out

setPooledMaxIdleTimeMillis

public void setPooledMaxIdleTimeMillis(int idleTimeoutMillis)
set the idle time out of a resource within the pool

Specified by:
setPooledMaxIdleTimeMillis in interface IConnectionPool
Parameters:
idleTimeoutMillis - the idle time out

getPooledMaxLifeTimeMillis

public int getPooledMaxLifeTimeMillis()
get the life timeout of a resource

Specified by:
getPooledMaxLifeTimeMillis in interface IConnectionPool
Returns:
the life timeout of a resource

setPooledMaxLifeTimeMillis

public void setPooledMaxLifeTimeMillis(int lifeTimeoutMillis)
set the life timeout of a resource

Specified by:
setPooledMaxLifeTimeMillis in interface IConnectionPool

getNumPendingGet

public int getNumPendingGet()
get the current number of pending get operations to retrieve a resource

Returns:
the current number of pending get operations

destroy

public static void destroy(INonBlockingConnection connection)
                    throws IOException

Throws:
IOException

close

public final void close()

Specified by:
close in interface Closeable

toString

public String toString()

Overrides:
toString in class Object


Copyright 2008 xSocket.org