public 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.| Modifier and Type | Class and Description |
|---|---|
static class |
SingleEndpointPool.Builder<E>
The Builder class responsible for constructing
SingleEndpointPool. |
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 |
pendingConnections
Number of connections we're currently trying to establish and waiting for the result
|
| Modifier | Constructor and Description |
|---|---|
protected |
SingleEndpointPool(ConnectorHandler<E> connectorHandler,
E endpointAddress,
E localEndpointAddress,
int corePoolSize,
int maxPoolSize,
DelayedExecutor.DelayQueue<SingleEndpointPool.ConnectTimeoutTask> connectTimeoutQueue,
DelayedExecutor.DelayQueue<SingleEndpointPool.ReconnectTask> reconnectQueue,
DelayedExecutor.DelayQueue<SingleEndpointPool.KeepAliveCleanerTask> keepAliveCleanerQueue,
long connectTimeoutMillis,
long keepAliveTimeoutMillis,
long keepAliveCheckIntervalMillis,
long reconnectDelayMillis,
int maxReconnectAttempts)
Constructs SingleEndpointPool instance.
|
protected |
SingleEndpointPool(ConnectorHandler<E> connectorHandler,
E endpointAddress,
E localEndpointAddress,
int corePoolSize,
int maxPoolSize,
DelayedExecutor delayedExecutor,
long connectTimeoutMillis,
long keepAliveTimeoutMillis,
long keepAliveCheckIntervalMillis,
long reconnectDelayMillis,
int maxReconnectAttempts)
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. |
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. |
int |
getOpenConnectionsCount()
Returns the number of connected
Connections in the pool. |
int |
getReadyConnectionsCount()
Returns the number of
Connections ready to be retrieved and used. |
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 |
isMaxCapacityReached()
Returns true is maximum number of
Connections the pool
can keep is reached and no new Connection can established, or
false otherwise. |
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. |
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 int pendingConnections
protected SingleEndpointPool(ConnectorHandler<E> connectorHandler, E endpointAddress, E localEndpointAddress, int corePoolSize, int maxPoolSize, DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts)
connectorHandler - ConnectorHandler to be used to establish new ConnectionsendpointAddress - endpoint addresslocalEndpointAddress - local bind address. May be null.corePoolSize - 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.protected SingleEndpointPool(ConnectorHandler<E> connectorHandler, E endpointAddress, E localEndpointAddress, int corePoolSize, int maxPoolSize, DelayedExecutor.DelayQueue<SingleEndpointPool.ConnectTimeoutTask> connectTimeoutQueue, DelayedExecutor.DelayQueue<SingleEndpointPool.ReconnectTask> reconnectQueue, DelayedExecutor.DelayQueue<SingleEndpointPool.KeepAliveCleanerTask> keepAliveCleanerQueue, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts)
connectorHandler - ConnectorHandler to be used to establish new ConnectionsendpointAddress - endpoint addresslocalEndpointAddress - local bind address. May be null.corePoolSize - 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 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.public static <T> SingleEndpointPool.Builder<T> builder(Class<T> endpointType)
SingleEndpointPool.Builder.endpointType - endpoint address type, for example
SocketAddress for TCP and UDP transportsSingleEndpointPool.Builderpublic 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 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.public 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.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 detachpublic 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()
protected boolean createConnectionIfPossible()
Connection by calling
checkBeforeOpeningConnection() and if it is possible - establish
new connection.Copyright © 2013 Oracle Corporation. All Rights Reserved.