E - the address type, for example for TCP transport it's SocketAddresspublic class SingleEndpointPool<E> extends Object
Connection pool implementation, in other words
this pool manages Connections to one specific endpoint.
The endpoint address has to be represented by an objected understandable by
a ConnectorHandler passed to the constructor. For example the
endpoint address has to be represented by SocketAddress for
TCPNIOConnectorHandler and UDPNIOConnectorHandler.
There are number of configuration options supported by the SingleEndpointPool:
- corePoolSize: the number of Connections to be kept in the pool and never timed out
because of keep-alive setting;
- maxPoolSize: the maximum number of Connections to be kept by the pool;
- keepAliveTimeoutMillis: the maximum number of milliseconds an idle Connection
will be kept in the pool. The idle Connections will be
closed till the pool size is greater than corePoolSize;
- keepAliveCheckIntervalMillis: the interval, which specifies how often the pool will
perform idle Connections check;
- reconnectDelayMillis: the delay to be used before the pool will repeat the attempt to connect to
the endpoint after previous connect had failed.
- asyncPollTimeoutMillis: maximum amount of time, after which
the async connection poll operation will
be failed with a timeout exception
- connectionTTLMillis: the maximum amount of time, a
Connection could be associated with the pool| Modifier and Type | Class and Description |
|---|---|
protected static class |
SingleEndpointPool.AsyncPoll |
static class |
SingleEndpointPool.Builder<E>
The Builder class responsible for constructing
SingleEndpointPool. |
protected static class |
SingleEndpointPool.ConnectionTTLTaskResolver |
protected static class |
SingleEndpointPool.ConnectionTTLWorker
Connect timeout mechanism classes related to DelayedExecutor.
|
protected static class |
SingleEndpointPool.ConnectTimeoutTask |
protected static class |
SingleEndpointPool.ConnectTimeoutTaskResolver |
protected static class |
SingleEndpointPool.ConnectTimeoutWorker
Connect timeout mechanism classes related to DelayedExecutor.
|
protected static class |
SingleEndpointPool.KeepAliveCleaner
Keep-alive mechanism classes related to DelayedExecutor.
|
protected static class |
SingleEndpointPool.KeepAliveCleanerTask<E> |
protected static class |
SingleEndpointPool.KeepAliveCleanerTaskResolver |
protected static class |
SingleEndpointPool.Reconnector
Reconnect mechanism classes related to DelayedExecutor.
|
protected static class |
SingleEndpointPool.ReconnectTask<E> |
protected static class |
SingleEndpointPool.ReconnectTaskResolver |
| Modifier and Type | Field and Description |
|---|---|
protected int |
maxPoolSize
The max number of
Connections kept by this pool |
protected int |
pendingConnections
Number of connections we're currently trying to establish and waiting for the result
|
| Modifier | Constructor and Description |
|---|---|
protected |
SingleEndpointPool(Endpoint<E> endpoint,
int corePoolSize,
int maxPoolSize,
DelayedExecutor.DelayQueue<SingleEndpointPool.ConnectTimeoutTask> connectTimeoutQueue,
DelayedExecutor.DelayQueue<SingleEndpointPool.ReconnectTask> reconnectQueue,
DelayedExecutor.DelayQueue<SingleEndpointPool.KeepAliveCleanerTask> keepAliveCleanerQueue,
DelayedExecutor.DelayQueue<Link<SingleEndpointPool.AsyncPoll>> asyncPollTimeoutQueue,
DelayedExecutor.DelayQueue<ConnectionInfo> connectionTTLQueue,
long connectTimeoutMillis,
long keepAliveTimeoutMillis,
long keepAliveCheckIntervalMillis,
long reconnectDelayMillis,
int maxReconnectAttempts,
long asyncPollTimeoutMillis,
long connectionTTLMillis,
boolean failFastWhenMaxSizeReached)
Constructs SingleEndpointPool instance.
|
protected |
SingleEndpointPool(Endpoint<E> endpoint,
int corePoolSize,
int maxPoolSize,
DelayedExecutor delayedExecutor,
long connectTimeoutMillis,
long keepAliveTimeoutMillis,
long keepAliveCheckIntervalMillis,
long reconnectDelayMillis,
int maxReconnectAttempts,
long asyncPollTimeoutMillis,
long connectionTTLMillis,
boolean failFastWhenMaxSizeReached)
Constructs SingleEndpointPool instance.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
attach(Connection connection)
Attaches "foreign"
Connection to the pool. |
static <T> SingleEndpointPool.Builder<T> |
builder(Class<T> endpointType)
Returns single endpoint pool
SingleEndpointPool.Builder. |
protected boolean |
checkBeforeOpeningConnection()
The method is called before the pool will try to establish new client
connection.
|
void |
close()
Closes the pool and release associated resources.
|
protected boolean |
createConnectionIfPossible()
Checks if it's possible to create a new
Connection by calling
checkBeforeOpeningConnection() and if it is possible - establish
new connection. |
boolean |
detach(Connection connection)
Detaches a
Connection from the pool. |
long |
getAsyncPollTimeout(TimeUnit timeUnit) |
ConnectionInfo<E> |
getConnectionInfo(Connection connection)
Returns pooled
ConnectionInfo, that might be used for monitoring
reasons, or null if the Connection does not belong to
this pool. |
long |
getConnectionTTL(TimeUnit timeUnit)
Return the maximum amount of time, a
Connection could be associated with the pool. |
long |
getConnectTimeout(TimeUnit timeUnit) |
int |
getCorePoolSize() |
Endpoint<E> |
getEndpoint() |
long |
getKeepAliveCheckInterval(TimeUnit timeUnit) |
long |
getKeepAliveTimeout(TimeUnit timeUnit)
Returns the maximum amount of time an idle
Connection will be kept
in the pool. |
int |
getMaxPoolSize() |
int |
getMaxReconnectAttempts() |
int |
getOpenConnectionsCount() |
int |
getReadyConnectionsCount() |
long |
getReconnectDelay(TimeUnit timeUnit) |
protected int |
getWaitingListSize() |
boolean |
isBusy(Connection connection)
Returns true only if the
Connection is registered in
the pool and is currently in busy state (used by a user), otherwise
returns false. |
boolean |
isFailFastWhenMaxSizeReached() |
boolean |
isMaxCapacityReached() |
boolean |
isRegistered(Connection connection)
Returns true if the
Connection is registered in the pool
no matter if it's currently in busy or ready state, or false if
the Connection is not registered in the pool. |
Connection |
poll() |
boolean |
release(Connection connection)
Returns the
Connection to the pool. |
int |
size()
Returns the current pool size.
|
GrizzlyFuture<Connection> |
take()
Obtains a
Connection from the pool in non-blocking/asynchronous fashion. |
void |
take(CompletionHandler<Connection> completionHandler)
Obtains a
Connection from the pool in non-blocking/asynchronous fashion. |
String |
toString() |
protected final int maxPoolSize
Connections kept by this poolprotected int pendingConnections
protected SingleEndpointPool(Endpoint<E> endpoint, int corePoolSize, int maxPoolSize, DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached)
endpoint - Endpoint to be used to establish new ConnectionscorePoolSize - the number of Connections, kept in the pool, that are immune to keep-alive mechanismmaxPoolSize - the max number of Connections kept by this pooldelayedExecutor - custom DelayedExecutor to be used by keep-alive and reconnect mechanismsconnectTimeoutMillis - timeout, after which, if a connection is not established, it is considered failedkeepAliveTimeoutMillis - the maximum number of milliseconds an idle Connection will be kept in the poolkeepAliveCheckIntervalMillis - the interval, which specifies how often the pool will perform idle Connections checkreconnectDelayMillis - the delay to be used before the pool will repeat the attempt to connect to the endpoint after previous connect had failedmaxReconnectAttempts - the maximum number of reconnect attempts that may be made before failure notification.asyncPollTimeoutMillis - the maximum time, the async poll operation could wait for a connection to become availableconnectionTTLMillis - the maximum time, a connection could stay registered with the poolfailFastWhenMaxSizeReached - true if the "take" method should fail fast if there is no free connection in the pool and max pool size is reachedprotected SingleEndpointPool(Endpoint<E> endpoint, int corePoolSize, int maxPoolSize, DelayedExecutor.DelayQueue<SingleEndpointPool.ConnectTimeoutTask> connectTimeoutQueue, DelayedExecutor.DelayQueue<SingleEndpointPool.ReconnectTask> reconnectQueue, DelayedExecutor.DelayQueue<SingleEndpointPool.KeepAliveCleanerTask> keepAliveCleanerQueue, DelayedExecutor.DelayQueue<Link<SingleEndpointPool.AsyncPoll>> asyncPollTimeoutQueue, DelayedExecutor.DelayQueue<ConnectionInfo> connectionTTLQueue, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached)
endpoint - Endpoint to be used to establish new ConnectionscorePoolSize - the number of Connections, kept in the pool, that are immune to keep-alive mechanismmaxPoolSize - the max number of Connections kept by this poolconnectTimeoutQueue - the DelayedExecutor.DelayQueue used by connect timeout mechanismreconnectQueue - the DelayedExecutor.DelayQueue used by reconnect mechanismkeepAliveCleanerQueue - the DelayedExecutor.DelayQueue used by keep-alive mechanismasyncPollTimeoutQueue - the DelayedExecutor.DelayQueue used by async connection poll mechanismconnectionTTLQueue - the DelayedExecutor.DelayQueue used by connection TTL mechanismconnectTimeoutMillis - timeout, after which, if a connection is not established, it is considered failedkeepAliveTimeoutMillis - the maximum number of milliseconds an idle Connection will be kept in the poolkeepAliveCheckIntervalMillis - the interval, which specifies how often the pool will perform idle Connections checkreconnectDelayMillis - the delay to be used before the pool will repeat the attempt to connect to the endpoint after previous connect had failedmaxReconnectAttempts - the maximum number of reconnect attempts that may be made before failure notification.asyncPollTimeoutMillis - the maximum time, the async poll operation could wait for a connection to become availableconnectionTTLMillis - the maximum time, a connection could stay registered with the poolfailFastWhenMaxSizeReached - true if the "take" method should fail fast if there is no free connection in the pool and max pool size is reachedpublic static <T> SingleEndpointPool.Builder<T> builder(Class<T> endpointType)
SingleEndpointPool.Builder.T - endpoint typeendpointType - endpoint address type, for example
SocketAddress for TCP and UDP transportsSingleEndpointPool.Builderpublic int getCorePoolSize()
Connections, kept in the pool,
that are immune to keep-alive mechanismpublic int getMaxPoolSize()
Connections kept by this poolpublic long getConnectTimeout(TimeUnit timeUnit)
timeUnit - TimeUnitpublic long getReconnectDelay(TimeUnit timeUnit)
timeUnit - TimeUnitpublic int getMaxReconnectAttempts()
public long getKeepAliveTimeout(TimeUnit timeUnit)
Connection will be kept
in the pool. The idle Connections will be closed till the pool
size is greater than corePoolSize.timeUnit - TimeUnitConnection will be kept
in the poolpublic long getKeepAliveCheckInterval(TimeUnit timeUnit)
timeUnit - TimeUnitConnections checkpublic long getAsyncPollTimeout(TimeUnit timeUnit)
timeUnit - TimeUnitpublic long getConnectionTTL(TimeUnit timeUnit)
Connection could be associated with the pool.
Once timeout is hit - the connection will be either closed, if it's idle,
or detached from the pool, if it's being used.timeUnit - TimeUnitConnection could be associated with the poolpublic boolean isFailFastWhenMaxSizeReached()
public int size()
Connections.public int getOpenConnectionsCount()
Connections in the pool.
Unlike size() the value doesn't include connecting
(connect in progress) Connections.public int getReadyConnectionsCount()
Connections ready to be retrieved and used.public boolean isMaxCapacityReached()
Connections the pool
can keep is reached and no new Connection can be established, or
false otherwise.public boolean isRegistered(Connection connection)
Connection is registered in the pool
no matter if it's currently in busy or ready state, or false if
the Connection is not registered in the pool.connection - ConnectionConnection is registered in the pool
no matter if it's currently in busy or ready state, or false if
the Connection is not registered in the poolpublic boolean isBusy(Connection connection)
Connection is registered in
the pool and is currently in busy state (used by a user), otherwise
returns false.connection - ConnectionConnection is registered in
the pool and is currently in busy state (used by a user), otherwise
returns falsepublic ConnectionInfo<E> getConnectionInfo(Connection connection)
ConnectionInfo, that might be used for monitoring
reasons, or null if the Connection does not belong to
this pool.connection - ConnectionConnectionInfo, that might be used for monitoring
reasons, or null if the Connection does not belong to
this poolpublic GrizzlyFuture<Connection> take()
Connection from the pool in non-blocking/asynchronous fashion.
Returns a GrizzlyFuture representing the pending result of the
non-blocking/asynchronous obtain task.
Future's get method will return the Connection once it
becomes available in the pool.
If you would like to immediately block waiting
for a Connection, you can use constructions of the form
connection = pool.take().get();
Note: returned GrizzlyFuture must be checked and released
properly. It must not be forgotten, because a Connection, that
might be assigned as a result of GrizzlyFuture has to be returned
to the pool. If you gave up on waiting for a Connection or you
are not interested in the Connection anymore, the proper release
code has to look like:
if (!future.cancel(false)) {
// means Connection is ready
pool.release(future.get());
}
GrizzlyFuturepublic void take(CompletionHandler<Connection> completionHandler)
Connection from the pool in non-blocking/asynchronous fashion.
The passed CompletionHandler will be notified about the result of the
non-blocking/asynchronous obtain task.completionHandler - to be notified once Connection is available or
an error occurredpublic Connection poll() throws IOException
Connection from the pool, if there is one available at the moment,
or null otherwiseIOException - if the pool is closedpublic boolean release(Connection connection)
Connection to the pool.
The Connection will be returned to the pool only in case it
was created by this pool, or it was attached to it using attach(org.glassfish.grizzly.Connection)
method.
If the Connection is not registered in the pool - it will be closed.
If the Connection is registered in the pool and already marked as ready - this method call will not have any effect.
If the Connection was returned - it is illegal to use it until
it is retrieved from the pool again.connection - the Connection to returntrue if the connection was successfully released.
If the connection cannot be released, the connection will be closed
and false will be returned.public boolean attach(Connection connection) throws IOException
Connection to the pool.
This method might be used to add to the pool a Connection, that
either has not been created by this pool or has been detached.
After calling this method, the Connection could be still used by
the caller and release(org.glassfish.grizzly.Connection) should
be called to return the Connection to the pool so it could be
reused.connection - ConnectionConnection has been successfully attached,
or false otherwise. If the Connection had
been already registered in the pool - the method call doesn't
have any effect and true will be returned.IOException - thrown if this pool has been already closedpublic boolean detach(Connection connection)
Connection from the pool.
De-registers the Connection from the pool and decreases the pool
size by 1. It is possible to re-attach the detached Connection
later by calling attach(org.glassfish.grizzly.Connection).
If the Connection was not registered in the pool - the
method call doesn't have any effect.connection - the Connection to detachtrue if the connection was successfully detached
from this pool, otherwise returns falsepublic void close()
Connections will be closed, the busy Connection,
that are still in use - will be kept open and will be automatically
closed when returned to the pool by release(org.glassfish.grizzly.Connection).protected boolean checkBeforeOpeningConnection()
pendingConnections counter, so don't forget to decrease it, if needed.protected int getWaitingListSize()
protected boolean createConnectionIfPossible()
Connection by calling
checkBeforeOpeningConnection() and if it is possible - establish
new connection.Connection could be open, or false otherwiseCopyright © 2015 Oracle Corporation. All Rights Reserved.