Class SingleEndpointPool<E>

java.lang.Object
org.glassfish.grizzly.connectionpool.SingleEndpointPool<E>
Type Parameters:
E - the address type, for example for TCP transport it's SocketAddress

public class SingleEndpointPool<E> extends Object
The single endpoint 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
Author:
Alexey Stashok
  • Field Details

    • maxPoolSize

      protected final int maxPoolSize
      The max number of Connections kept by this pool
    • pendingConnections

      protected int pendingConnections
      Number of connections we're currently trying to establish and waiting for the result
  • Constructor Details

    • SingleEndpointPool

      protected SingleEndpointPool(Endpoint<E> endpoint, int corePoolSize, int maxPoolSize, org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached)
      Constructs SingleEndpointPool instance.
      Parameters:
      endpoint - Endpoint to be used to establish new Connections
      corePoolSize - the number of Connections, kept in the pool, that are immune to keep-alive mechanism
      maxPoolSize - the max number of Connections kept by this pool
      delayedExecutor - custom DelayedExecutor to be used by keep-alive and reconnect mechanisms
      connectTimeoutMillis - timeout, after which, if a connection is not established, it is considered failed
      keepAliveTimeoutMillis - the maximum number of milliseconds an idle Connection will be kept in the pool
      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
      maxReconnectAttempts - 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 available
      connectionTTLMillis - the maximum time, a connection could stay registered with the pool
      failFastWhenMaxSizeReached - true if the "take" method should fail fast if there is no free connection in the pool and max pool size is reached
    • SingleEndpointPool

      protected SingleEndpointPool(Endpoint<E> endpoint, int corePoolSize, int maxPoolSize, org.glassfish.grizzly.utils.DelayedExecutor.DelayQueue<SingleEndpointPool.ConnectTimeoutTask> connectTimeoutQueue, org.glassfish.grizzly.utils.DelayedExecutor.DelayQueue<SingleEndpointPool.ReconnectTask> reconnectQueue, org.glassfish.grizzly.utils.DelayedExecutor.DelayQueue<SingleEndpointPool.KeepAliveCleanerTask> keepAliveCleanerQueue, org.glassfish.grizzly.utils.DelayedExecutor.DelayQueue<Link<SingleEndpointPool.AsyncPoll>> asyncPollTimeoutQueue, org.glassfish.grizzly.utils.DelayedExecutor.DelayQueue<ConnectionInfo> connectionTTLQueue, long connectTimeoutMillis, long keepAliveTimeoutMillis, long keepAliveCheckIntervalMillis, long reconnectDelayMillis, int maxReconnectAttempts, long asyncPollTimeoutMillis, long connectionTTLMillis, boolean failFastWhenMaxSizeReached)
      Constructs SingleEndpointPool instance.
      Parameters:
      endpoint - Endpoint to be used to establish new Connections
      corePoolSize - the number of Connections, kept in the pool, that are immune to keep-alive mechanism
      maxPoolSize - the max number of Connections kept by this pool
      connectTimeoutQueue - the DelayedExecutor.DelayQueue used by connect timeout mechanism
      reconnectQueue - the DelayedExecutor.DelayQueue used by reconnect mechanism
      keepAliveCleanerQueue - the DelayedExecutor.DelayQueue used by keep-alive mechanism
      asyncPollTimeoutQueue - the DelayedExecutor.DelayQueue used by async connection poll mechanism
      connectionTTLQueue - the DelayedExecutor.DelayQueue used by connection TTL mechanism
      connectTimeoutMillis - timeout, after which, if a connection is not established, it is considered failed
      keepAliveTimeoutMillis - the maximum number of milliseconds an idle Connection will be kept in the pool
      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
      maxReconnectAttempts - 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 available
      connectionTTLMillis - the maximum time, a connection could stay registered with the pool
      failFastWhenMaxSizeReached - true if the "take" method should fail fast if there is no free connection in the pool and max pool size is reached
  • Method Details

    • builder

      public static <T> SingleEndpointPool.Builder<T> builder(Class<T> endpointType)
      Returns single endpoint pool SingleEndpointPool.Builder.
      Type Parameters:
      T - endpoint type
      Parameters:
      endpointType - endpoint address type, for example SocketAddress for TCP and UDP transports
      Returns:
      SingleEndpointPool.Builder
    • getEndpoint

      public Endpoint<E> getEndpoint()
      Returns:
      the endpoint description
    • getCorePoolSize

      public int getCorePoolSize()
      Returns:
      the number of Connections, kept in the pool, that are immune to keep-alive mechanism
    • getMaxPoolSize

      public int getMaxPoolSize()
      Returns:
      the max number of Connections kept by this pool
    • 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 idle Connection will be kept in the pool. The idle Connections will be closed till the pool size is greater than corePoolSize.
      Parameters:
      timeUnit - TimeUnit
      Returns:
      the maximum amount of time an idle Connection will 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, a 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.
      Parameters:
      timeUnit - TimeUnit
      Returns:
      the maximum amount of time, a Connection could 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.
      Returns:
      the current pool size
    • getOpenConnectionsCount

      public int getOpenConnectionsCount()
      Returns:
      the number of connected Connections in the pool. Unlike size() the value doesn't include connecting (connect in progress) Connections.
    • getReadyConnectionsCount

      public int getReadyConnectionsCount()
      Returns:
      the number of Connections ready to be retrieved and used.
    • isMaxCapacityReached

      public boolean isMaxCapacityReached()
      Returns:
      true is maximum number of Connections the pool can keep is reached and no new Connection can be established, or false otherwise.
    • isRegistered

      public boolean isRegistered(org.glassfish.grizzly.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.
      Parameters:
      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
    • isBusy

      public boolean isBusy(org.glassfish.grizzly.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.
      Parameters:
      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
    • getConnectionInfo

      public ConnectionInfo<E> getConnectionInfo(org.glassfish.grizzly.Connection connection)
      Returns pooled ConnectionInfo, that might be used for monitoring reasons, or null if the Connection does not belong to this pool.
      Parameters:
      connection - Connection
      Returns:
      pooled ConnectionInfo, that might be used for monitoring reasons, or null if the Connection does not belong to this pool
    • take

      public org.glassfish.grizzly.GrizzlyFuture<org.glassfish.grizzly.Connection> take()
      Obtains a 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());
       }
       
      Returns:
      GrizzlyFuture
    • take

      public void take(org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.Connection> completionHandler)
      Obtains a 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.
      Parameters:
      completionHandler - to be notified once Connection is available or an error occurred
    • poll

      public org.glassfish.grizzly.Connection poll() throws IOException
      Returns:
      a Connection from 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 the 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.
      Parameters:
      connection - the Connection to return
      Returns:
      true if the connection was successfully released. If the connection cannot be released, the connection will be closed and false will be returned.
    • attach

      public boolean attach(org.glassfish.grizzly.Connection connection) throws IOException
      Attaches "foreign" 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.
      Parameters:
      connection - Connection
      Returns:
      true if the Connection 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.
      Throws:
      IOException - thrown if this pool has been already closed
    • detach

      public boolean detach(org.glassfish.grizzly.Connection connection)
      Detaches a 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.
      Parameters:
      connection - the Connection to detach
      Returns:
      true if the connection was successfully detached from this pool, otherwise returns false
    • close

      public void close()
      Closes the pool and release associated resources. The ready 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).
    • checkBeforeOpeningConnection

      protected boolean checkBeforeOpeningConnection()
      The method is called before the pool will try to establish new client connection. Please note, if the method returns true it also increases the pendingConnections counter, so don't forget to decrease it, if needed.
      Returns:
      true if new connection could be created, or false otherwise
    • getWaitingListSize

      protected int getWaitingListSize()
      Returns:
      the number of consumers waiting for a connection
    • createConnectionIfPossible

      protected boolean createConnectionIfPossible()
      Checks if it's possible to create a new Connection by calling checkBeforeOpeningConnection() and if it is possible - establish new connection.
      Returns:
      true if a new Connection could be open, or false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object