Package io.netty.channel.pool
Class SimpleChannelPool
- java.lang.Object
-
- io.netty.channel.pool.SimpleChannelPool
-
- All Implemented Interfaces:
ChannelPool
,java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
FixedChannelPool
public class SimpleChannelPool extends java.lang.Object implements ChannelPool
SimpleChannelPool
implementation which will create newChannel
s if someone tries to acquire aChannel
but none is in the pool atm. No limit on the maximal concurrentChannel
s is enforced. This implementation uses LIFO order forChannel
s in theChannelPool
.
-
-
Field Summary
Fields Modifier and Type Field Description private Bootstrap
bootstrap
private java.util.Deque<Channel>
deque
private ChannelPoolHandler
handler
private ChannelHealthChecker
healthCheck
private boolean
lastRecentUsed
private static AttributeKey<SimpleChannelPool>
POOL_KEY
private boolean
releaseHealthCheck
-
Constructor Summary
Constructors Constructor Description SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler)
Creates a new instance using theChannelHealthChecker.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.SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck, boolean lastRecentUsed)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Future<Channel>
acquire()
Acquire aChannel
from thisChannelPool
.Future<Channel>
acquire(Promise<Channel> promise)
Acquire aChannel
from thisChannelPool
.private Future<Channel>
acquireHealthyFromPoolOrNew(Promise<Channel> promise)
Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.protected Bootstrap
bootstrap()
Returns theBootstrap
this pool will use to open new connections.void
close()
private void
closeAndFail(Channel channel, java.lang.Throwable cause, Promise<?> promise)
Future<java.lang.Void>
closeAsync()
Closes the pool in an async manner.private void
closeChannel(Channel channel)
protected ChannelFuture
connectChannel(Bootstrap bs)
Bootstrap a newChannel
.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)
protected ChannelPoolHandler
handler()
Returns theChannelPoolHandler
that will be notified for the different pool actions.protected ChannelHealthChecker
healthChecker()
Returns theChannelHealthChecker
that will be used to check if aChannel
is healthy.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 aChannel
back to the internal storage.protected Channel
pollChannel()
Poll aChannel
out of the internal storage to reuse it.Future<java.lang.Void>
release(Channel channel)
Release aChannel
back to thisChannelPool
.Future<java.lang.Void>
release(Channel channel, Promise<java.lang.Void> promise)
Release aChannel
back to thisChannelPool
.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 healthy.protected boolean
releaseHealthCheck()
Indicates whether this pool will check the health of channels before offering them back into the pool.
-
-
-
Field Detail
-
POOL_KEY
private static final AttributeKey<SimpleChannelPool> POOL_KEY
-
deque
private final java.util.Deque<Channel> deque
-
handler
private final ChannelPoolHandler handler
-
healthCheck
private final ChannelHealthChecker healthCheck
-
bootstrap
private final Bootstrap bootstrap
-
releaseHealthCheck
private final boolean releaseHealthCheck
-
lastRecentUsed
private final boolean lastRecentUsed
-
-
Constructor Detail
-
SimpleChannelPool
public SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler)
Creates a new instance using theChannelHealthChecker.ACTIVE
.- Parameters:
bootstrap
- theBootstrap
that is used for connectionshandler
- theChannelPoolHandler
that will be notified for the different pool actions
-
SimpleChannelPool
public SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck)
Creates a new instance.- Parameters:
bootstrap
- theBootstrap
that is used for connectionshandler
- theChannelPoolHandler
that will be notified for the different pool actionshealthCheck
- theChannelHealthChecker
that will be used to check if aChannel
is still healthy when obtain from theChannelPool
-
SimpleChannelPool
public SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck)
Creates a new instance.- Parameters:
bootstrap
- theBootstrap
that is used for connectionshandler
- theChannelPoolHandler
that will be notified for the different pool actionshealthCheck
- theChannelHealthChecker
that will be used to check if aChannel
is still healthy when obtain from theChannelPool
releaseHealthCheck
- will check channel health before offering back if this parameter set totrue
; otherwise, channel health is only checked at acquisition time
-
SimpleChannelPool
public SimpleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck, boolean lastRecentUsed)
Creates a new instance.- Parameters:
bootstrap
- theBootstrap
that is used for connectionshandler
- theChannelPoolHandler
that will be notified for the different pool actionshealthCheck
- theChannelHealthChecker
that will be used to check if aChannel
is still healthy when obtain from theChannelPool
releaseHealthCheck
- will check channel health before offering back if this parameter set totrue
; otherwise, channel health is only checked at acquisition timelastRecentUsed
-true
Channel
selection will be LIFO, iffalse
FIFO.
-
-
Method Detail
-
bootstrap
protected Bootstrap bootstrap()
Returns theBootstrap
this pool will use to open new connections.- Returns:
- the
Bootstrap
this pool will use to open new connections
-
handler
protected ChannelPoolHandler handler()
Returns theChannelPoolHandler
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 theChannelHealthChecker
that will be used to check if aChannel
is healthy.- Returns:
- the
ChannelHealthChecker
that will be used to check if aChannel
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, orfalse
if channel health is only checked at acquisition time
-
acquire
public final Future<Channel> acquire()
Description copied from interface:ChannelPool
Acquire aChannel
from thisChannelPool
. The returnedFuture
is notified once the acquire is successful and failed otherwise. Its important that an acquired is always released to the pool again, even if theChannel
is explicitly closed..- Specified by:
acquire
in interfaceChannelPool
-
acquire
public Future<Channel> acquire(Promise<Channel> promise)
Description copied from interface:ChannelPool
Acquire aChannel
from thisChannelPool
. The givenPromise
is notified once the acquire is successful and failed otherwise. Its important that an acquired is always released to the pool again, even if theChannel
is explicitly closed..- Specified by:
acquire
in interfaceChannelPool
-
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) throws java.lang.Exception
- Throws:
java.lang.Exception
-
notifyHealthCheck
private void notifyHealthCheck(Future<java.lang.Boolean> future, Channel ch, Promise<Channel> promise)
-
connectChannel
protected ChannelFuture connectChannel(Bootstrap bs)
Bootstrap a newChannel
. The default implementation usesBootstrap.connect()
, sub-classes may override this.The
Bootstrap
that is passed in here is cloned viaBootstrap.clone()
, so it is safe to modify.
-
release
public final Future<java.lang.Void> release(Channel channel)
Description copied from interface:ChannelPool
Release aChannel
back to thisChannelPool
. The returnedFuture
is notified once the release is successful and failed otherwise. When failed theChannel
will automatically closed.- Specified by:
release
in interfaceChannelPool
-
release
public Future<java.lang.Void> release(Channel channel, Promise<java.lang.Void> promise)
Description copied from interface:ChannelPool
Release aChannel
back to thisChannelPool
. The givenPromise
is notified once the release is successful and failed otherwise. When failed theChannel
will automatically closed.- Specified by:
release
in interfaceChannelPool
-
doHealthCheckOnRelease
private void doHealthCheckOnRelease(Channel channel, Promise<java.lang.Void> promise) throws java.lang.Exception
- Throws:
java.lang.Exception
-
releaseAndOfferIfHealthy
private void releaseAndOfferIfHealthy(Channel channel, Promise<java.lang.Void> promise, Future<java.lang.Boolean> future) throws java.lang.Exception
Adds the channel back to the pool only if the channel is healthy.- Parameters:
channel
- the channel to put back to the poolpromise
- offer operation promise.future
- the future that contains information fif channel is healthy or not.- Throws:
java.lang.Exception
- in case when failed to notify handler about release operation.
-
releaseAndOffer
private void releaseAndOffer(Channel channel, Promise<java.lang.Void> promise) throws java.lang.Exception
- Throws:
java.lang.Exception
-
closeChannel
private void closeChannel(Channel channel)
-
closeAndFail
private void closeAndFail(Channel channel, java.lang.Throwable cause, Promise<?> promise)
-
pollChannel
protected Channel pollChannel()
Poll aChannel
out of the internal storage to reuse it. This will returnnull
if noChannel
is ready to be reused. Sub-classes may overridepollChannel()
andofferChannel(Channel)
. Be aware that implementations of these methods needs to be thread-safe!
-
offerChannel
protected boolean offerChannel(Channel channel)
Offer aChannel
back to the internal storage. This will returntrue
if theChannel
could be added,false
otherwise. Sub-classes may overridepollChannel()
andofferChannel(Channel)
. Be aware that implementations of these methods needs to be thread-safe!
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceChannelPool
- Specified by:
close
in interfacejava.io.Closeable
-
closeAsync
public Future<java.lang.Void> closeAsync()
Closes the pool in an async manner.- Returns:
- Future which represents completion of the close task
-
-