|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.xsocket.stream.NonBlockingConnectionPool
public final class NonBlockingConnectionPool
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 |
---|
public static final long UNLIMITED_TIMEOUT
Constructor Detail |
---|
public NonBlockingConnectionPool(long timeToIdleMillis)
timeToIdleMillis
- the max idle time in the pool. After this time the free connection will be closedpublic NonBlockingConnectionPool(long timeToIdleMillis, int maxActive, long maxWaitTimeMillis)
Method Detail |
---|
public INonBlockingConnection getNonBlockingConnection(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 occurspublic INonBlockingConnection getNonBlockingConnection(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 occurspublic INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address, int port, IHandler appHandler) throws java.io.IOException, WaitTimeoutException
address
- the server addressport
- the sever portappHandler
- the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
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 occurspublic INonBlockingConnection getNonBlockingConnection(java.net.InetAddress address, int port, IHandler appHandler, java.util.concurrent.Executor workerPool) throws java.io.IOException, WaitTimeoutException
address
- the server addressport
- the sever portappHandler
- the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)workerPool
- the worker pool to use
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 occurspublic final void destroyConnection(IConnection connection) throws java.io.IOException
destroyConnection
in interface IConnectionPool
connection
- 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 IConnectionPool
public final void setMaxActive(int maxActive)
setMaxActive
in interface IConnectionPool
maxActive
- the number of max active connectionspublic final long getMaxWaitMillis()
getMaxWaitMillis
in interface IConnectionPool
public final void setMaxWaitMillis(long maxWaitMillis)
setMaxWaitMillis
in interface IConnectionPool
maxWaitMillis
- the max wait time to get a free connectionpublic final int getMaxIdle()
getMaxIdle
in interface IConnectionPool
public final void setMaxIdle(int maxIdle)
setMaxIdle
in interface IConnectionPool
maxIdle
- the number of max idle connectionspublic final int getNumActive()
getNumActive
in interface IConnectionPool
public final int getNumIdle()
getNumIdle
in interface IConnectionPool
public final long getIdleTimeoutMillis()
getIdleTimeoutMillis
in interface IConnectionPool
public final void setIdleTimeoutMillis(long idleTimeoutMillis)
setIdleTimeoutMillis
in interface IConnectionPool
idleTimeoutMillis
- the idle timeout for pooled connectionspublic final long getLifeTimeoutMillis()
getLifeTimeoutMillis
in interface IConnectionPool
public final void setLifeTimeoutMillis(long lifeTimeoutMillis)
setLifeTimeoutMillis
in interface IConnectionPool
lifeTimeoutMillis
- the life timeoutpublic final void close()
close
in interface IConnectionPool
public 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 |