Class SimpleChannelPool

java.lang.Object
io.netty.channel.pool.SimpleChannelPool
All Implemented Interfaces:
ChannelPool, Closeable, AutoCloseable
Direct Known Subclasses:
FixedChannelPool

public class SimpleChannelPool extends Object implements ChannelPool
Simple ChannelPool implementation which will create new Channels if someone tries to acquire a Channel but none is in the pool atm. No limit on the maximal concurrent Channels is enforced. This implementation uses LIFO order for Channels in the ChannelPool.
  • Field Details

  • Constructor Details

  • Method Details

    • bootstrap

      protected Bootstrap bootstrap()
      Returns the Bootstrap this pool will use to open new connections.
      Returns:
      the Bootstrap this pool will use to open new connections
    • handler

      protected ChannelPoolHandler handler()
      Returns the ChannelPoolHandler that will be notified for the different pool actions.
      Returns:
      the ChannelPoolHandler that will be notified for the different pool actions
    • healthChecker

      protected ChannelHealthChecker healthChecker()
      Returns the ChannelHealthChecker that will be used to check if a Channel is healthy.
      Returns:
      the ChannelHealthChecker that will be used to check if a Channel is healthy
    • releaseHealthCheck

      protected boolean releaseHealthCheck()
      Indicates whether this pool will check the health of channels before offering them back into the pool.
      Returns:
      true if this pool will check the health of channels before offering them back into the pool, or false if channel health is only checked at acquisition time
    • acquire

      public final Future<Channel> acquire()
      Description copied from interface: ChannelPool
      Acquire a Channel from this ChannelPool. The returned Future is notified once the acquire is successful and failed otherwise. Its important that an acquired is always released to the pool again, even if the Channel is explicitly closed..
      Specified by:
      acquire in interface ChannelPool
    • acquire

      public Future<Channel> acquire(Promise<Channel> promise)
      Description copied from interface: ChannelPool
      Acquire a Channel from this ChannelPool. The given Promise is notified once the acquire is successful and failed otherwise. Its important that an acquired is always released to the pool again, even if the Channel is explicitly closed..
      Specified by:
      acquire in interface ChannelPool
    • acquireHealthyFromPoolOrNew

      private Future<Channel> acquireHealthyFromPoolOrNew(Promise<Channel> promise)
      Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.
      Parameters:
      promise - the promise to provide acquire result.
      Returns:
      future for acquiring a channel.
    • notifyConnect

      private void notifyConnect(ChannelFuture future, Promise<Channel> promise)
    • doHealthCheck

      private void doHealthCheck(Channel channel, Promise<Channel> promise)
    • notifyHealthCheck

      private void notifyHealthCheck(Future<Boolean> future, Channel channel, Promise<Channel> promise)
    • connectChannel

      protected ChannelFuture connectChannel(Bootstrap bs)
      Bootstrap a new Channel. The default implementation uses Bootstrap.connect(), sub-classes may override this.

      The Bootstrap that is passed in here is cloned via Bootstrap.clone(), so it is safe to modify.

    • release

      public final Future<Void> release(Channel channel)
      Description copied from interface: ChannelPool
      Release a Channel back to this ChannelPool. The returned Future is notified once the release is successful and failed otherwise. When failed the Channel will automatically closed.
      Specified by:
      release in interface ChannelPool
    • release

      public Future<Void> release(Channel channel, Promise<Void> promise)
      Description copied from interface: ChannelPool
      Release a Channel back to this ChannelPool. The given Promise is notified once the release is successful and failed otherwise. When failed the Channel will automatically closed.
      Specified by:
      release in interface ChannelPool
    • doReleaseChannel

      private void doReleaseChannel(Channel channel, Promise<Void> promise)
    • doHealthCheckOnRelease

      private void doHealthCheckOnRelease(Channel channel, Promise<Void> promise) throws Exception
      Throws:
      Exception
    • releaseAndOfferIfHealthy

      private void releaseAndOfferIfHealthy(Channel channel, Promise<Void> promise, Future<Boolean> future)
      Adds the channel back to the pool only if the channel is healthy.
      Parameters:
      channel - the channel to put back to the pool
      promise - offer operation promise.
      future - the future that contains information fif channel is healthy or not.
      Throws:
      Exception - in case when failed to notify handler about release operation.
    • releaseAndOffer

      private void releaseAndOffer(Channel channel, Promise<Void> promise) throws Exception
      Throws:
      Exception
    • closeChannel

      private void closeChannel(Channel channel) throws Exception
      Throws:
      Exception
    • closeAndFail

      private void closeAndFail(Channel channel, Throwable cause, Promise<?> promise)
    • pollChannel

      protected Channel pollChannel()
      Poll a Channel out of the internal storage to reuse it. This will return null if no Channel is ready to be reused. Sub-classes may override pollChannel() and offerChannel(Channel). Be aware that implementations of these methods needs to be thread-safe!
    • offerChannel

      protected boolean offerChannel(Channel channel)
      Offer a Channel back to the internal storage. This will return true if the Channel could be added, false otherwise. Sub-classes may override pollChannel() and offerChannel(Channel). Be aware that implementations of these methods needs to be thread-safe!
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface ChannelPool
      Specified by:
      close in interface Closeable
    • closeAsync

      public Future<Void> closeAsync()
      Closes the pool in an async manner.
      Returns:
      Future which represents completion of the close task