Class MultiEndpointPool<E>
- java.lang.Object
-
- org.glassfish.grizzly.connectionpool.MultiEndpointPool<E>
-
- Type Parameters:
E- the address type, for example for TCP transport it'sSocketAddress
public class MultiEndpointPool<E> extends Object
The multi endpointConnectionpool implementation where each endpoint sub-pool is represented bySingleEndpointPooland referenced by anEndpoint. There are number of configuration options supported by the MultiEndpointPool: - maxConnectionsPerEndpoint: the maximum number ofConnections eachSingleEndpointPoolsub-pool is allowed to have; - maxConnectionsTotal: the total maximum number ofConnections to be kept by the pool; - keepAliveTimeoutMillis: the maximum number of milliseconds an idleConnectionwill be kept in the pool. The idleConnections will be closed until the pool size is greater than the corePoolSize; - keepAliveCheckIntervalMillis: the interval, which specifies how often the pool will perform idleConnections 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, aConnectioncould be associated with the pool- Author:
- Alexey Stashok
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMultiEndpointPool.Builder<E>The Builder class responsible for constructingSingleEndpointPool.static classMultiEndpointPool.EndpointPoolBuilder<E>static interfaceMultiEndpointPool.EndpointPoolCustomizer<E>The customizer, which could be used to modify an endpoint pool setting before it will be created.
-
Field Summary
Fields Modifier and Type Field Description protected Map<Endpoint<E>,SingleEndpointPool<E>>endpointToPoolMapMaps endpoint -to- SingleEndpointPoolprotected ObjectpoolSyncSync for endpointToPoolMap updates
-
Constructor Summary
Constructors Modifier Constructor Description protectedMultiEndpointPool(int maxConnectionsPerEndpoint, int maxConnectionsTotal, org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached, MultiEndpointPool.EndpointPoolCustomizer<E> endpointPoolCustomizer)Constructs MultiEndpointPool instance.protectedMultiEndpointPool(org.glassfish.grizzly.ConnectorHandler<E> defaultConnectorHandler, int maxConnectionsPerEndpoint, int maxConnectionsTotal, org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached, MultiEndpointPool.EndpointPoolCustomizer<E> endpointPoolCustomizer)Deprecated.defaultConnectorHandler is deprecated
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanattach(Endpoint<E> endpoint, org.glassfish.grizzly.Connection connection)Attaches "foreign"Connectionto the pool.static <T> MultiEndpointPool.Builder<T>builder(Class<T> endpointType)Returns single endpoint poolMultiEndpointPool.Builder.protected voidcheckNotClosed()Method throwsIOExceptionif the pool has been closed.voidclose()Closes the pool and releases associated resources.voidclose(Endpoint<E> endpoint)Closes specific endpoint associated pool and releases its resources.protected SingleEndpointPool<E>createSingleEndpointPool(Endpoint<E> endpoint)CreatesSingleEndpointPoolinstance.booleandetach(org.glassfish.grizzly.Connection connection)Detaches aConnectionfrom the pool.longgetAsyncPollTimeout(TimeUnit timeUnit)ConnectionInfo<E>getConnectionInfo(org.glassfish.grizzly.Connection connection)Returns pooledConnectionInfo, that might be used for monitoring reasons, or null if theConnectiondoes not belong to this pool.longgetConnectionTTL(TimeUnit timeUnit)Return the maximum amount of time, aConnectioncould be associated with the pool.longgetConnectTimeout(TimeUnit timeUnit)longgetKeepAliveCheckInterval(TimeUnit timeUnit)longgetKeepAliveTimeout(TimeUnit timeUnit)Returns the maximum amount of time an idleConnectionwill be kept in the pool.intgetMaxConnectionsPerEndpoint()intgetMaxConnectionsTotal()intgetMaxReconnectAttempts()intgetOpenConnectionsCount()longgetReconnectDelay(TimeUnit timeUnit)booleanisBusy(org.glassfish.grizzly.Connection connection)Returns true only if theConnectionis registered in the pool and is currently in busy state (used by a user), otherwise returns false.booleanisFailFastWhenMaxSizeReached()booleanisMaxCapacityReached()booleanisRegistered(org.glassfish.grizzly.Connection connection)Returns true if theConnectionis registered in the pool no matter if it's currently in busy or ready state, or false if theConnectionis not registered in the pool.protected SingleEndpointPool<E>obtainSingleEndpointPool(Endpoint<E> endpoint)ObtainsSingleEndpointPoolassociated with the specific endpoint represented byEndpoint.org.glassfish.grizzly.Connectionpoll(Endpoint<E> endpoint)booleanrelease(org.glassfish.grizzly.Connection connection)Returns theConnectionto the pool.intsize()org.glassfish.grizzly.GrizzlyFuture<org.glassfish.grizzly.Connection>take(Endpoint<E> endpoint)Obtains aConnectionto the specified endpoint from the pool in non-blocking/asynchronous fashion.voidtake(Endpoint<E> endpoint, org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.Connection> completionHandler)Obtains aConnectionto the specified endpoint from the pool in non-blocking/asynchronous fashion.StringtoString()
-
-
-
Constructor Detail
-
MultiEndpointPool
@Deprecated protected MultiEndpointPool(org.glassfish.grizzly.ConnectorHandler<E> defaultConnectorHandler, int maxConnectionsPerEndpoint, int maxConnectionsTotal, org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached, MultiEndpointPool.EndpointPoolCustomizer<E> endpointPoolCustomizer)
Deprecated.defaultConnectorHandler is deprecatedConstructs MultiEndpointPool instance.- Parameters:
defaultConnectorHandler- the defaultConnectorHandlerto be used to establish newConnectionsmaxConnectionsPerEndpoint- the maximum number ofConnections single endpoint sub-pool is allowed to havemaxConnectionsTotal- the total maximum number ofConnections the pool is allowed to havedelayedExecutor- customDelayedExecutorto 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 idleConnectionwill be kept in the poolkeepAliveCheckIntervalMillis- the interval, which specifies how often the pool will perform idleConnections 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 reachedendpointPoolCustomizer- the customizer, which will be used to modify a specific endpoint pool settings and overwrite the default settings assigned by this MultiEndpointPool
-
MultiEndpointPool
protected MultiEndpointPool(int maxConnectionsPerEndpoint, int maxConnectionsTotal, org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached, MultiEndpointPool.EndpointPoolCustomizer<E> endpointPoolCustomizer)Constructs MultiEndpointPool instance.- Parameters:
maxConnectionsPerEndpoint- the maximum number ofConnections single endpoint sub-pool is allowed to havemaxConnectionsTotal- the total maximum number ofConnections the pool is allowed to havedelayedExecutor- customDelayedExecutorto 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 idleConnectionwill be kept in the poolkeepAliveCheckIntervalMillis- the interval, which specifies how often the pool will perform idleConnections 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 reachedendpointPoolCustomizer- the customizer, which will be used to modify a specific endpoint pool settings and overwrite the default settings assigned by this MultiEndpointPool
-
-
Method Detail
-
builder
public static <T> MultiEndpointPool.Builder<T> builder(Class<T> endpointType)
Returns single endpoint poolMultiEndpointPool.Builder.- Type Parameters:
T- endpoint type- Parameters:
endpointType- endpoint address type, for exampleSocketAddressfor TCP and UDP transports- Returns:
MultiEndpointPool.Builder
-
getMaxConnectionsTotal
public int getMaxConnectionsTotal()
- Returns:
- the total maximum number of
Connections to be kept by the pool
-
getMaxConnectionsPerEndpoint
public int getMaxConnectionsPerEndpoint()
- Returns:
- the maximum number of
Connections eachSingleEndpointPoolsub-pool is allowed to have
-
getConnectTimeout
public long getConnectTimeout(TimeUnit timeUnit)
- Parameters:
timeUnit-TimeUnit- Returns:
- the connection timeout, after which, if a connection is not established, it is considered failed
-
getReconnectDelay
public long getReconnectDelay(TimeUnit timeUnit)
- Parameters:
timeUnit-TimeUnit- Returns:
- the delay to be used before the pool will repeat the attempt to connect to the endpoint after previous attempt fail
-
getMaxReconnectAttempts
public int getMaxReconnectAttempts()
- Returns:
- the maximum number of reconnect attempts
-
getKeepAliveTimeout
public long getKeepAliveTimeout(TimeUnit timeUnit)
Returns the maximum amount of time an idleConnectionwill be kept in the pool.- Parameters:
timeUnit-TimeUnit- Returns:
- the maximum amount of time an idle
Connectionwill be kept in the pool
-
getKeepAliveCheckInterval
public long getKeepAliveCheckInterval(TimeUnit timeUnit)
- Parameters:
timeUnit-TimeUnit- Returns:
- the interval, which specifies how often the pool will perform idle
Connections check
-
getAsyncPollTimeout
public long getAsyncPollTimeout(TimeUnit timeUnit)
- Parameters:
timeUnit-TimeUnit- Returns:
- the timeout, after which, the async connection poll operation will fail with a timeout exception
-
getConnectionTTL
public long getConnectionTTL(TimeUnit timeUnit)
Return the maximum amount of time, aConnectioncould 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.- Parameters:
timeUnit-TimeUnit- Returns:
- the maximum amount of time, a
Connectioncould be associated with the pool
-
isFailFastWhenMaxSizeReached
public boolean isFailFastWhenMaxSizeReached()
- Returns:
- true, if the "take" method will fail fast if there is no free connection in the pool and max pool size is reached
-
size
public int size()
- Returns:
- the current pool size. This value includes connected and connecting (connect in progress)
Connections
-
getOpenConnectionsCount
public int getOpenConnectionsCount()
- Returns:
- the number of connected
Connections in the pool. Unlikesize()the value doesn't include connecting (connect in progress)Connections
-
isMaxCapacityReached
public boolean isMaxCapacityReached()
- Returns:
- true is maximum number of
Connections the pool can keep is reached and no newConnectioncan be established, or false otherwise.
-
isRegistered
public boolean isRegistered(org.glassfish.grizzly.Connection connection)
Returns true if theConnectionis registered in the pool no matter if it's currently in busy or ready state, or false if theConnectionis not registered in the pool.- Parameters:
connection-Connection- Returns:
- true if the
Connectionis registered in the pool no matter if it's currently in busy or ready state, or false if theConnectionis not registered in the pool
-
isBusy
public boolean isBusy(org.glassfish.grizzly.Connection connection)
Returns true only if theConnectionis registered in the pool and is currently in busy state (used by a user), otherwise returns false.- Parameters:
connection-Connection- Returns:
- true only if the
Connectionis registered in the pool and is currently in busy state (used by a user), otherwise returns false
-
getConnectionInfo
public ConnectionInfo<E> getConnectionInfo(org.glassfish.grizzly.Connection connection)
Returns pooledConnectionInfo, that might be used for monitoring reasons, or null if theConnectiondoes not belong to this pool.- Parameters:
connection-Connection- Returns:
- pooled
ConnectionInfo, that might be used for monitoring reasons, or null if theConnectiondoes not belong to this pool
-
take
public org.glassfish.grizzly.GrizzlyFuture<org.glassfish.grizzly.Connection> take(Endpoint<E> endpoint)
Obtains aConnectionto the specified endpoint from the pool in non-blocking/asynchronous fashion. Returns aGrizzlyFuturerepresenting the pending result of the non-blocking/asynchronous obtain task. Future's get method will return theConnectiononce 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(endpoint).get();Note: returned
GrizzlyFuturemust be checked and released properly. It must not be forgotten, because aConnection, that might be assigned as a result ofGrizzlyFuturehas to be returned to the pool. If you gave up on waiting for aConnectionor you are not interested in theConnectionanymore, the proper release code has to look like:if (!future.cancel(false)) { // means Connection is ready pool.release(future.get()); }- Parameters:
endpoint-Endpoint, that represents an endpoint- Returns:
GrizzlyFuture
-
take
public void take(Endpoint<E> endpoint, org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.Connection> completionHandler)
Obtains aConnectionto the specified endpoint from the pool in non-blocking/asynchronous fashion. The passedCompletionHandlerwill be notified about the result of the non-blocking/asynchronous obtain task.- Parameters:
endpoint-Endpoint, that represents an endpointcompletionHandler-
-
poll
public org.glassfish.grizzly.Connection poll(Endpoint<E> endpoint) throws IOException
- Parameters:
endpoint-Endpoint, that represents an endpoint- Returns:
- a
Connectionfrom the pool, if there is one available at the moment, or null otherwise - Throws:
IOException- if the pool is closed
-
release
public boolean release(org.glassfish.grizzly.Connection connection)
Returns theConnectionto the pool. TheConnectionwill be returned to the pool only in case it was created by this pool, or it was attached to it usingattach(Endpoint, org.glassfish.grizzly.Connection)method. If theConnectionis not registered in the pool - it will be closed. If theConnectionis registered in the pool and already marked as ready - this method call will not have any effect. If theConnectionwas returned - it is illegal to use it until it is retrieved from the pool again.- Parameters:
connection- theConnectionto return- Returns:
trueif the connection was successfully released. If the connection cannot be released, the connection will be closed andfalsewill be returned.
-
attach
public boolean attach(Endpoint<E> endpoint, org.glassfish.grizzly.Connection connection) throws IOException
Attaches "foreign"Connectionto the pool. This method might be used to add to the pool aConnection, that either has not been created by this pool or has been detached.- Parameters:
endpoint-Endpoint, that represents an endpoint to which the theConnectionwill be attachedconnection-Connection- Returns:
- true if the
Connectionhas been successfully attached, or false otherwise. If theConnectionhad been already registered in the pool - the method call doesn't have any effect and true will be returned. - Throws:
IOException- thrown if this pool has been already closed
-
detach
public boolean detach(org.glassfish.grizzly.Connection connection)
Detaches aConnectionfrom the pool. De-registers theConnectionfrom the pool and decreases the pool size by 1. It is possible to re-attach the detachedConnectionlater by callingattach(org.glassfish.grizzly.connectionpool.Endpoint, org.glassfish.grizzly.Connection). If theConnectionwas not registered in the pool - the method call doesn't have any effect.- Parameters:
connection- theConnectionto detach- Returns:
trueif theConnectionwas detached, otherwise returnsfalse
-
close
public void close(Endpoint<E> endpoint)
Closes specific endpoint associated pool and releases its resources. The readyConnections associated with the endpoint pool will be closed, the busyConnection, that are still in use - will be kept open and will be automatically closed when returned to the pool byrelease(org.glassfish.grizzly.Connection).- Parameters:
endpoint-Endpoint, that represents an endpoint
-
close
public void close()
Closes the pool and releases associated resources. The readyConnections will be closed, the busyConnection, that are still in use - will be kept open and will be automatically closed when returned to the pool byrelease(org.glassfish.grizzly.Connection).
-
obtainSingleEndpointPool
protected SingleEndpointPool<E> obtainSingleEndpointPool(Endpoint<E> endpoint) throws IOException
ObtainsSingleEndpointPoolassociated with the specific endpoint represented byEndpoint. If there is noSingleEndpointPoolassociated with the endpoint - the one will be created.- Parameters:
endpoint-Endpoint, that represents an endpoint- Returns:
SingleEndpointPool- Throws:
IOException- if the pool is already closed
-
createSingleEndpointPool
protected SingleEndpointPool<E> createSingleEndpointPool(Endpoint<E> endpoint)
CreatesSingleEndpointPoolinstance.- Parameters:
endpoint- the endpoint key- Returns:
SingleEndpointPool
-
checkNotClosed
protected void checkNotClosed() throws IOExceptionMethod throwsIOExceptionif the pool has been closed.- Throws:
IOException- if the pool has been closed
-
-