org.xsocket.connection
Class BlockingConnectionPool

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

public final class BlockingConnectionPool
extends Object
implements IConnectionPool

A blocking connection pool implementation.

This class is thread safe

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


  IBlockingConnection con = null;

  try {
     // retrieve a connection (if no connection is in pool, a new one will be created)
     con = pool.getBlockingConnection(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
 
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_ACTIVE_PER_SERVER, DEFAULT_MAX_IDLE, DEFAULT_MAX_WAIT_MILLIS, DEFAULT_MAX_WAITING
 
Constructor Summary
BlockingConnectionPool()
          constructor
BlockingConnectionPool(SSLContext sslContext)
          constructor
 
Method Summary
 void addListener(ILifeCycle listener)
          adds a listener
 void close()
           
 void destroy(IBlockingConnection resource)
           
 List<String> getActiveConnectionInfos()
          get a info list about the active connections
 IBlockingConnection getBlockingConnection(InetAddress address, int port)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(InetAddress address, int port, boolean isSSL)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(InetAddress address, int port, int connectTimeoutMillis)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(InetAddress address, int port, int connectTimeoutMillis, boolean isSSL)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(String host, int port)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(String host, int port, boolean isSSL)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(String host, int port, int connectTimeoutMillis)
          get a pool connection for the given address.
 IBlockingConnection getBlockingConnection(String host, int port, int connectTimeoutMillis, boolean isSSL)
          get a pool connection for the given address.
 List<String> getIdleConnectionInfos()
          get a info list about the idle connections
 int getMaxActive()
          return the number of max active resources
 int getMaxActivePerServer()
          return the number of max active resources per server
 int getMaxIdle()
          get the number of max idling resources
 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 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
 boolean isOpen()
          returns true, is pool is open
 boolean removeListener(ILifeCycle listener)
          removes a listener
 void setMaxActive(int maxActive)
          set the number of max active resources
 void setMaxActivePerServer(int maxActivePerServer)
          set the number of max active resources per server
 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockingConnectionPool

public BlockingConnectionPool()
constructor


BlockingConnectionPool

public BlockingConnectionPool(SSLContext sslContext)
constructor

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

isOpen

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

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

getBlockingConnection

public IBlockingConnection getBlockingConnection(String host,
                                                 int port)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(String host,
                                                 int port,
                                                 boolean isSSL)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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 occur
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(String host,
                                                 int port,
                                                 int connectTimeoutMillis)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(String host,
                                                 int port,
                                                 int connectTimeoutMillis,
                                                 boolean isSSL)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(InetAddress address,
                                                 int port)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(InetAddress address,
                                                 int port,
                                                 boolean isSSL)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(InetAddress address,
                                                 int port,
                                                 int connectTimeoutMillis)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

getBlockingConnection

public IBlockingConnection getBlockingConnection(InetAddress address,
                                                 int port,
                                                 int connectTimeoutMillis,
                                                 boolean isSSL)
                                          throws IOException,
                                                 SocketTimeoutException,
                                                 MaxConnectionsExceededException
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
MaxConnectionsExceededException - if the max number of active connections (per server) is reached

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

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

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

setMaxActivePerServer

public void setMaxActivePerServer(int maxActivePerServer)
set the number of max active resources per server

Specified by:
setMaxActivePerServer in interface IConnectionPool

getMaxActivePerServer

public int getMaxActivePerServer()
return the number of max active resources per server

Specified by:
getMaxActivePerServer in interface IConnectionPool
Returns:
number of max active resources per server

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

getNumIdle

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

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

getActiveConnectionInfos

public List<String> getActiveConnectionInfos()
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()
get a info list about the idle connections

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

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

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

getNumPendingGet

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

Returns:
the current number of pending get operations

close

public void close()
Specified by:
close in interface Closeable

destroy

public void destroy(IBlockingConnection resource)
             throws IOException
Throws:
IOException


Copyright 2010 xSocket.org