Top Description Inners Fields Constructors Methods
io.netty.channel.pool

public Class SimpleChannelPool

extends Object
implements ChannelPool
Class Inheritance
All Implemented Interfaces
io.netty.channel.pool.ChannelPool, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
io.netty.channel.pool.FixedChannelPool
Imports
io.netty.bootstrap.Bootstrap, io.netty.channel.Channel, .ChannelFuture, .ChannelFutureListener, .ChannelInitializer, .EventLoop, io.netty.util.AttributeKey, io.netty.util.concurrent.Future, .FutureListener, .GlobalEventExecutor, .Promise, io.netty.util.internal.PlatformDependent, java.util.Deque, java.util.concurrent.Callable

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final Bootstrap
private final Deque<Channel>
private final ChannelPoolHandler
private final ChannelHealthChecker
private final boolean
private static final AttributeKey<SimpleChannelPool>
private final boolean

Constructor Summary

AccessConstructor and Description
public
SimpleChannelPool(Bootstrap
the Bootstrap that is used for connections
bootstrap
,
final ChannelPoolHandler
the ChannelPoolHandler that will be notified for the different pool actions
handler
)

Creates a new instance using the ChannelHealthChecker#ACTIVE.

public
SimpleChannelPool(Bootstrap
the Bootstrap that is used for connections
bootstrap
,
final ChannelPoolHandler
the ChannelPoolHandler that will be notified for the different pool actions
handler
,
ChannelHealthChecker
the ChannelHealthChecker that will be used to check if a Channel is still healthy when obtain from the ChannelPool
healthCheck
)

Creates a new instance.

public
SimpleChannelPool(Bootstrap
the Bootstrap that is used for connections
bootstrap
,
final ChannelPoolHandler
the ChannelPoolHandler that will be notified for the different pool actions
handler
,
ChannelHealthChecker
the ChannelHealthChecker that will be used to check if a Channel is still healthy when obtain from the ChannelPool
healthCheck
,
boolean
will check channel health before offering back if this parameter set to true; otherwise, channel health is only checked at acquisition time
releaseHealthCheck
)

Creates a new instance.

public
SimpleChannelPool(Bootstrap
the Bootstrap that is used for connections
bootstrap
,
final ChannelPoolHandler
the ChannelPoolHandler that will be notified for the different pool actions
handler
,
ChannelHealthChecker
the ChannelHealthChecker that will be used to check if a Channel is still healthy when obtain from the ChannelPool
healthCheck
,
boolean
will check channel health before offering back if this parameter set to true; otherwise, channel health is only checked at acquisition time
releaseHealthCheck
,
boolean
true Channel selection will be LIFO, if false FIFO.
lastRecentUsed
)

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public final Future<Channel>
public Future<Channel>
acquire(final Promise<Channel> promise)

Implements io.netty.channel.pool.ChannelPool.acquire.

Acquire a Channel from this ChannelPool.
private Future<Channel>

Returns:

future for acquiring a channel.
acquireHealthyFromPoolOrNew
(final Promise<Channel>
the promise to provide acquire result.
promise
)

Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.

protected Bootstrap

Returns:

the Bootstrap this pool will use to open new connections
bootstrap
()

Returns the Bootstrap this pool will use to open new connections.

public void
close()

Implements io.netty.channel.pool.ChannelPool.close.

Closes this stream and releases any system resources associated with it.
private void
closeAndFail(Channel channel, Throwable cause, Promise<?> promise)

public Future<Void>

Returns:

Future which represents completion of the close task
closeAsync
()

Closes the pool in an async manner.

private void
protected ChannelFuture
connectChannel(Bootstrap bs)

Bootstrap a new Channel.

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

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

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

protected ChannelPoolHandler

Returns:

the ChannelPoolHandler that will be notified for the different pool actions
handler
()

Returns the ChannelPoolHandler that will be notified for the different pool actions.

protected ChannelHealthChecker

Returns:

the ChannelHealthChecker that will be used to check if a Channel is healthy
healthChecker
()

Returns the ChannelHealthChecker that will be used to check if a Channel is healthy.

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

protected boolean
offerChannel(Channel channel)

Offer a Channel back to the internal storage.

protected Channel
pollChannel()

Poll a Channel out of the internal storage to reuse it.

public final Future<Void>
release(Channel channel)

Implements io.netty.channel.pool.ChannelPool.release.

Release a Channel back to this ChannelPool.
public Future<Void>
release(final Channel channel, final Promise<Void> promise)

Implements io.netty.channel.pool.ChannelPool.release.

Release a Channel back to this ChannelPool.
private void
releaseAndOffer(Channel channel, Promise<Void> promise)

private void
releaseAndOfferIfHealthy(Channel
the channel to put back to the pool
channel
,
Promise<Void>
offer operation promise.
promise
,
Future<Boolean>
the future that contains information fif channel is healthy or not.
future
)

Adds the channel back to the pool only if the channel is healthy.

protected boolean

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
releaseHealthCheck
()

Indicates whether this pool will check the health of channels before offering them back into the pool.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bootstrapback to summary
private final Bootstrap bootstrap
dequeback to summary
private final Deque<Channel> deque
handlerback to summary
private final ChannelPoolHandler handler
healthCheckback to summary
private final ChannelHealthChecker healthCheck
lastRecentUsedback to summary
private final boolean lastRecentUsed
POOL_KEYback to summary
private static final AttributeKey<SimpleChannelPool> POOL_KEY
releaseHealthCheckback to summary
private final boolean releaseHealthCheck

Constructor Detail

SimpleChannelPoolback to summary
public SimpleChannelPool(Bootstrap bootstrap, final ChannelPoolHandler handler)

Creates a new instance using the ChannelHealthChecker#ACTIVE.

Parameters
bootstrap:Bootstrap

the Bootstrap that is used for connections

handler:ChannelPoolHandler

the ChannelPoolHandler that will be notified for the different pool actions

SimpleChannelPoolback to summary
public SimpleChannelPool(Bootstrap bootstrap, final ChannelPoolHandler handler, ChannelHealthChecker healthCheck)

Creates a new instance.

Parameters
bootstrap:Bootstrap

the Bootstrap that is used for connections

handler:ChannelPoolHandler

the ChannelPoolHandler that will be notified for the different pool actions

healthCheck:ChannelHealthChecker

the ChannelHealthChecker that will be used to check if a Channel is still healthy when obtain from the ChannelPool

SimpleChannelPoolback to summary
public SimpleChannelPool(Bootstrap bootstrap, final ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck)

Creates a new instance.

Parameters
bootstrap:Bootstrap

the Bootstrap that is used for connections

handler:ChannelPoolHandler

the ChannelPoolHandler that will be notified for the different pool actions

healthCheck:ChannelHealthChecker

the ChannelHealthChecker that will be used to check if a Channel is still healthy when obtain from the ChannelPool

releaseHealthCheck:boolean

will check channel health before offering back if this parameter set to true; otherwise, channel health is only checked at acquisition time

SimpleChannelPoolback to summary
public SimpleChannelPool(Bootstrap bootstrap, final ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck, boolean lastRecentUsed)

Creates a new instance.

Parameters
bootstrap:Bootstrap

the Bootstrap that is used for connections

handler:ChannelPoolHandler

the ChannelPoolHandler that will be notified for the different pool actions

healthCheck:ChannelHealthChecker

the ChannelHealthChecker that will be used to check if a Channel is still healthy when obtain from the ChannelPool

releaseHealthCheck:boolean

will check channel health before offering back if this parameter set to true; otherwise, channel health is only checked at acquisition time

lastRecentUsed:boolean

true Channel selection will be LIFO, if false FIFO.

Method Detail

acquireback to summary
public final Future<Channel> acquire()

Implements io.netty.channel.pool.ChannelPool.acquire.

Doc from io.netty.channel.pool.ChannelPool.acquire.

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..

Annotations
@Override
acquireback to summary
public Future<Channel> acquire(final Promise<Channel> promise)

Implements io.netty.channel.pool.ChannelPool.acquire.

Doc from io.netty.channel.pool.ChannelPool.acquire.

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..

Annotations
@Override
acquireHealthyFromPoolOrNewback to summary
private Future<Channel> acquireHealthyFromPoolOrNew(final Promise<Channel> promise)

Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.

Parameters
promise:Promise<Channel>

the promise to provide acquire result.

Returns:Future<Channel>

future for acquiring a channel.

bootstrapback to summary
protected Bootstrap bootstrap()

Returns the Bootstrap this pool will use to open new connections.

Returns:Bootstrap

the Bootstrap this pool will use to open new connections

closeback to summary
public void close()

Implements io.netty.channel.pool.ChannelPool.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
closeAndFailback to summary
private void closeAndFail(Channel channel, Throwable cause, Promise<?> promise)
closeAsyncback to summary
public Future<Void> closeAsync()

Closes the pool in an async manner.

Returns:Future<Void>

Future which represents completion of the close task

closeChannelback to summary
private void closeChannel(Channel channel) throws Exception
connectChannelback to summary
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.

doHealthCheckback to summary
private void doHealthCheck(final Channel channel, final Promise<Channel> promise)
doHealthCheckOnReleaseback to summary
private void doHealthCheckOnRelease(final Channel channel, final Promise<Void> promise) throws Exception
doReleaseChannelback to summary
private void doReleaseChannel(Channel channel, Promise<Void> promise)
handlerback to summary
protected ChannelPoolHandler handler()

Returns the ChannelPoolHandler that will be notified for the different pool actions.

Returns:ChannelPoolHandler

the ChannelPoolHandler that will be notified for the different pool actions

healthCheckerback to summary
protected ChannelHealthChecker healthChecker()

Returns the ChannelHealthChecker that will be used to check if a Channel is healthy.

Returns:ChannelHealthChecker

the ChannelHealthChecker that will be used to check if a Channel is healthy

notifyConnectback to summary
private void notifyConnect(ChannelFuture future, Promise<Channel> promise)
notifyHealthCheckback to summary
private void notifyHealthCheck(Future<Boolean> future, Channel channel, Promise<Channel> promise)
offerChannelback to summary
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!

pollChannelback to summary
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!

releaseback to summary
public final Future<Void> release(Channel channel)

Implements io.netty.channel.pool.ChannelPool.release.

Doc from io.netty.channel.pool.ChannelPool.release.

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.

Annotations
@Override
releaseback to summary
public Future<Void> release(final Channel channel, final Promise<Void> promise)

Implements io.netty.channel.pool.ChannelPool.release.

Doc from io.netty.channel.pool.ChannelPool.release.

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.

Annotations
@Override
releaseAndOfferback to summary
private void releaseAndOffer(Channel channel, Promise<Void> promise) throws Exception
releaseAndOfferIfHealthyback to summary
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:Channel

the channel to put back to the pool

promise:Promise<Void>

offer operation promise.

future:Future<Boolean>

the future that contains information fif channel is healthy or not.

Exceptions
Exception:
in case when failed to notify handler about release operation.
releaseHealthCheckback to summary
protected boolean releaseHealthCheck()

Indicates whether this pool will check the health of channels before offering them back into the pool.

Returns:boolean

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

io.netty.channel.pool back to summary

private final Class SimpleChannelPool.ChannelPoolFullException

extends IllegalStateException
Class Inheritance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Throwable
fillInStackTrace()

Overrides java.lang.Throwable.fillInStackTrace.

Fills in the execution stack trace.

Constructor Detail

ChannelPoolFullExceptionback to summary
private ChannelPoolFullException()

Method Detail

fillInStackTraceback to summary
public Throwable fillInStackTrace()

Overrides java.lang.Throwable.fillInStackTrace.

Doc from java.lang.Throwable.fillInStackTrace.

Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.

If the stack trace of this Throwable is not writable, calling this method has no effect.

Returns:Throwable

a reference to this Throwable instance.

Annotations
@Override