java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.lettuce.core.protocol.ConnectionWatchdog
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler
@Sharable public class ConnectionWatchdog extends ChannelInboundHandlerAdapter
A netty
ChannelHandler responsible for monitoring the channel and reconnecting when the connection is lost.- Author:
- Will Glozer, Mark Paluch, Koji Lin
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
Constructors Constructor Description ConnectionWatchdog(Delay reconnectDelay, ClientOptions clientOptions, Bootstrap bootstrap, Timer timer, EventExecutorGroup reconnectWorkers, Mono<SocketAddress> socketAddressSupplier, ReconnectionListener reconnectionListener, ConnectionFacade connectionFacade, EventBus eventBus, Endpoint endpoint)Create a new watchdog that adds to new connections to the suppliedChannelGroupand establishes a newChannelwhen disconnected, while reconnect is true. -
Method Summary
Modifier and Type Method Description voidchannelActive(ChannelHandlerContext ctx)voidchannelInactive(ChannelHandlerContext ctx)booleanisListenOnChannelInactive()booleanisReconnectSuspended()voidrun(int attempt)Reconnect to the remote address that the closed channel was connected to.voidscheduleReconnect()Schedule reconnect if channel is not available/not active.voidsetListenOnChannelInactive(boolean listenOnChannelInactive)Enable event listener for disconnected events.voidsetReconnectSuspended(boolean reconnectSuspended)Suspend reconnection temporarily.Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRead, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Constructor Details
-
ConnectionWatchdog
public ConnectionWatchdog(Delay reconnectDelay, ClientOptions clientOptions, Bootstrap bootstrap, Timer timer, EventExecutorGroup reconnectWorkers, Mono<SocketAddress> socketAddressSupplier, ReconnectionListener reconnectionListener, ConnectionFacade connectionFacade, EventBus eventBus, Endpoint endpoint)Create a new watchdog that adds to new connections to the suppliedChannelGroupand establishes a newChannelwhen disconnected, while reconnect is true. The socketAddressSupplier can supply the reconnect address.- Parameters:
reconnectDelay- reconnect delay, must not benullclientOptions- client options for the current connection, must not benullbootstrap- Configuration for new channels, must not benulltimer- Timer used for delayed reconnect, must not benullreconnectWorkers- executor group for reconnect tasks, must not benullsocketAddressSupplier- the socket address supplier to obtain an address for reconnection, may benullreconnectionListener- the reconnection listener, must not benullconnectionFacade- the connection facade, must not benulleventBus- Event bus to emit reconnect events.endpoint- must not benull
-
-
Method Details
-
channelActive
- Specified by:
channelActivein interfaceChannelInboundHandler- Overrides:
channelActivein classChannelInboundHandlerAdapter- Throws:
Exception
-
channelInactive
- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
Exception
-
scheduleReconnect
public void scheduleReconnect()Schedule reconnect if channel is not available/not active. -
run
Reconnect to the remote address that the closed channel was connected to. This creates a newChannelPipelinewith the same handler instances contained in the old channel's pipeline.- Parameters:
attempt- attempt counter- Throws:
Exception- when reconnection fails.
-
setListenOnChannelInactive
public void setListenOnChannelInactive(boolean listenOnChannelInactive)Enable event listener for disconnected events.- Parameters:
listenOnChannelInactive-trueto listen for disconnected events.
-
isListenOnChannelInactive
public boolean isListenOnChannelInactive() -
setReconnectSuspended
public void setReconnectSuspended(boolean reconnectSuspended)Suspend reconnection temporarily. Reconnect suspension will interrupt reconnection attempts.- Parameters:
reconnectSuspended-trueto suspend reconnection
-
isReconnectSuspended
public boolean isReconnectSuspended()
-