public class SimpleChannelPool extends java.lang.Object implements ChannelPool
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.| Modifier and Type | Field and Description |
|---|---|
private Bootstrap |
bootstrap |
private java.util.Deque<Channel> |
deque |
private static java.lang.IllegalStateException |
FULL_EXCEPTION |
private ChannelPoolHandler |
handler |
private ChannelHealthChecker |
healthCheck |
private static AttributeKey<SimpleChannelPool> |
POOL_KEY |
private boolean |
releaseHealthCheck |
private static java.lang.IllegalStateException |
UNHEALTHY_NON_OFFERED_TO_POOL |
| Constructor and Description |
|---|
SimpleChannelPool(Bootstrap bootstrap,
ChannelPoolHandler handler)
Creates a new instance using the
ChannelHealthChecker.ACTIVE. |
SimpleChannelPool(Bootstrap bootstrap,
ChannelPoolHandler handler,
ChannelHealthChecker healthCheck)
Creates a new instance.
|
SimpleChannelPool(Bootstrap bootstrap,
ChannelPoolHandler handler,
ChannelHealthChecker healthCheck,
boolean releaseHealthCheck)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
Future<Channel> |
acquire()
Acquire a
Channel from this ChannelPool. |
Future<Channel> |
acquire(Promise<Channel> promise)
Acquire a
Channel from this ChannelPool. |
private Future<Channel> |
acquireHealthyFromPoolOrNew(Promise<Channel> promise)
Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.
|
void |
close() |
private static void |
closeAndFail(Channel channel,
java.lang.Throwable cause,
Promise<?> promise) |
private static void |
closeChannel(Channel channel) |
protected ChannelFuture |
connectChannel(Bootstrap bs)
Bootstrap a new
Channel. |
private void |
doHealthCheck(Channel ch,
Promise<Channel> promise) |
private void |
doHealthCheckOnRelease(Channel channel,
Promise<java.lang.Void> promise) |
private void |
doReleaseChannel(Channel channel,
Promise<java.lang.Void> promise) |
private void |
notifyConnect(ChannelFuture future,
Promise<Channel> promise) |
private void |
notifyHealthCheck(Future<java.lang.Boolean> future,
Channel ch,
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. |
Future<java.lang.Void> |
release(Channel channel)
Release a
Channel back to this ChannelPool. |
Future<java.lang.Void> |
release(Channel channel,
Promise<java.lang.Void> promise)
Release a
Channel back to this ChannelPool. |
private void |
releaseAndOffer(Channel channel,
Promise<java.lang.Void> promise) |
private void |
releaseAndOfferIfHealthy(Channel channel,
Promise<java.lang.Void> promise,
Future<java.lang.Boolean> future)
Adds the channel back to the pool only if the channel is healty.
|
private static final AttributeKey<SimpleChannelPool> POOL_KEY
private static final java.lang.IllegalStateException FULL_EXCEPTION
private static final java.lang.IllegalStateException UNHEALTHY_NON_OFFERED_TO_POOL
private final java.util.Deque<Channel> deque
private final ChannelPoolHandler handler
private final ChannelHealthChecker healthCheck
private final Bootstrap bootstrap
private final boolean releaseHealthCheck
public SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler)
ChannelHealthChecker.ACTIVE.bootstrap - the Bootstrap that is used for connectionshandler - the ChannelPoolHandler that will be notified for the different pool actionspublic SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck)
bootstrap - the Bootstrap that is used for connectionshandler - the ChannelPoolHandler that will be notified for the different pool actionshealthCheck - the ChannelHealthChecker that will be used to check if a Channel is
still healthy when obtain from the ChannelPoolpublic SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck)
bootstrap - the Bootstrap that is used for connectionshandler - the ChannelPoolHandler that will be notified for the different pool actionshealthCheck - the ChannelHealthChecker that will be used to check if a Channel is
still healthy when obtain from the ChannelPoolreleaseHealthCheck - will offercheck channel health before offering back if this parameter set to
true.public final Future<Channel> acquire()
ChannelPoolChannel 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..acquire in interface ChannelPoolpublic Future<Channel> acquire(Promise<Channel> promise)
ChannelPoolChannel 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..acquire in interface ChannelPoolprivate Future<Channel> acquireHealthyFromPoolOrNew(Promise<Channel> promise)
promise - the promise to provide acquire result.private void notifyConnect(ChannelFuture future, Promise<Channel> promise)
private void notifyHealthCheck(Future<java.lang.Boolean> future, Channel ch, Promise<Channel> promise)
protected ChannelFuture connectChannel(Bootstrap bs)
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.
public final Future<java.lang.Void> release(Channel channel)
ChannelPoolChannel back to this ChannelPool. The returned Future is notified once
the release is successful and failed otherwise. When failed the Channel will automatically closed.release in interface ChannelPoolpublic Future<java.lang.Void> release(Channel channel, Promise<java.lang.Void> promise)
ChannelPoolChannel back to this ChannelPool. The given Promise is notified once
the release is successful and failed otherwise. When failed the Channel will automatically closed.release in interface ChannelPoolprivate void doHealthCheckOnRelease(Channel channel, Promise<java.lang.Void> promise) throws java.lang.Exception
java.lang.Exceptionprivate void releaseAndOfferIfHealthy(Channel channel, Promise<java.lang.Void> promise, Future<java.lang.Boolean> future) throws java.lang.Exception
channel - the channel to put back to the poolpromise - offer operation promise.future - the future that contains information fif channel is healthy or not.java.lang.Exception - in case when failed to notify handler about release operation.private void releaseAndOffer(Channel channel, Promise<java.lang.Void> promise) throws java.lang.Exception
java.lang.Exceptionprivate static void closeChannel(Channel channel)
private static void closeAndFail(Channel channel, java.lang.Throwable cause, Promise<?> promise)
protected Channel pollChannel()
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!protected boolean offerChannel(Channel channel)
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!public void close()
close in interface ChannelPoolclose in interface java.io.Closeableclose in interface java.lang.AutoCloseable