| 
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||

java.lang.Objectorg.xsocket.stream.BlockingConnectionPool
public final class BlockingConnectionPool
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 unlimited connection pool with idle timeout 60 sec 
  BlockingConnectionPool pool = new BlockingConnectionPool(60L * 1000L);
 
 
  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) {
     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 | |
|---|---|
BlockingConnectionPool(long timeToIdleMillis)
constructor  | 
|
BlockingConnectionPool(long timeToIdleMillis,
                       int maxActive,
                       long maxWaitTimeMillis)
constructor  | 
|
BlockingConnectionPool(long timeToIdleMillis,
                       int maxActive,
                       long maxWaitTimeMillis,
                       int maxIdle)
constructor  | 
|
| Method Summary | |
|---|---|
 void | 
addListener(ILifeCycle listener)
adds a listener  | 
 void | 
close()
closes the connection pool.  | 
 void | 
destroyConnection(IConnection connection)
destroy the given connection.  | 
 IBlockingConnection | 
getBlockingConnection(java.net.InetAddress address,
                      int port)
get a pool connection for the given address.  | 
 IBlockingConnection | 
getBlockingConnection(java.lang.String host,
                      int port)
get a pool connection for the given address.  | 
 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().  | 
 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 | 
|---|
public static final long UNLIMITED_TIMEOUT
| Constructor Detail | 
|---|
public BlockingConnectionPool(long timeToIdleMillis)
timeToIdleMillis - the max idle time in the pool. After this time the free connection will be closed
public BlockingConnectionPool(long timeToIdleMillis,
                              int maxActive,
                              long maxWaitTimeMillis)
timeToIdleMillis - the max idle time in the pool. After this time the free connection will be closedtimeToLiveMillis - the max living time of the connection. If a free connection exeeded this time, the connection will be closed (if it is in pool)maxWaitMillis - the max wait time by acquiring a connection from the pool
public BlockingConnectionPool(long timeToIdleMillis,
                              int maxActive,
                              long maxWaitTimeMillis,
                              int maxIdle)
timeToIdleMillis - the max idle time in the pool. After this time the free connection will be closedtimeToLiveMillis - the max living time of the connection. If a free connection exeeded this time, the connection will be closed (if it is in pool)maxWaitMillis - the max wait time by acquiring a connection from the poolmaxIdle - the max number of free connection in the pool| Method Detail | 
|---|
public IBlockingConnection getBlockingConnection(java.lang.String host,
                                                 int port)
                                          throws java.io.IOException,
                                                 WaitTimeoutException
host - the server addressport - the sever port
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
public IBlockingConnection getBlockingConnection(java.net.InetAddress address,
                                                 int port)
                                          throws java.io.IOException,
                                                 WaitTimeoutException
address - the server addressport - the sever port
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
public final void destroyConnection(IConnection connection)
                             throws java.io.IOException
destroyConnection in interface IConnectionPoolconnection - the leased connection
java.io.IOException - if an exception occurspublic void addListener(ILifeCycle listener)
listener - gthe listener to addpublic boolean removeListener(ILifeCycle listener)
listener - the listener to remove
public final int getMaxActive()
getMaxActive in interface IConnectionPoolpublic final void setMaxActive(int maxActive)
setMaxActive in interface IConnectionPoolmaxActive - the number of max active connectionspublic final long getMaxWaitMillis()
getMaxWaitMillis in interface IConnectionPoolpublic final void setMaxWaitMillis(long maxWaitMillis)
setMaxWaitMillis in interface IConnectionPoolmaxWaitMillis - the max wait time to get a free connectionpublic final int getMaxIdle()
getMaxIdle in interface IConnectionPoolpublic final void setMaxIdle(int maxIdle)
setMaxIdle in interface IConnectionPoolmaxIdle - the number of max idle connectionspublic final int getNumActive()
getNumActive in interface IConnectionPoolpublic final int getNumIdle()
getNumIdle in interface IConnectionPoolpublic final long getIdleTimeoutMillis()
getIdleTimeoutMillis in interface IConnectionPoolpublic final void setIdleTimeoutMillis(long idleTimeoutMillis)
setIdleTimeoutMillis in interface IConnectionPoolidleTimeoutMillis - the idle timeout for pooled connectionspublic final long getLifeTimeoutMillis()
getLifeTimeoutMillis in interface IConnectionPoolpublic final void setLifeTimeoutMillis(long lifeTimeoutMillis)
setLifeTimeoutMillis in interface IConnectionPoollifeTimeoutMillis - the life timeoutpublic final void close()
close in interface IConnectionPoolpublic java.lang.String toString()
toString in class java.lang.Object
  | 
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||