Package io.lettuce.core.protocol
Class ConnectionWatchdog
- 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 nettyChannelHandler
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 suppliedChannelGroup
and establishes a newChannel
when disconnected, while reconnect is true.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(ChannelHandlerContext ctx)
void
channelInactive(ChannelHandlerContext ctx)
boolean
isListenOnChannelInactive()
boolean
isReconnectSuspended()
void
run(int attempt)
Reconnect to the remote address that the closed channel was connected to.void
scheduleReconnect()
Schedule reconnect if channel is not available/not active.void
setListenOnChannelInactive(boolean listenOnChannelInactive)
Enable event listener for disconnected events.void
setReconnectSuspended(boolean reconnectSuspended)
Suspend reconnection temporarily.-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRead, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
-
-
-
Constructor Detail
-
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 suppliedChannelGroup
and establishes a newChannel
when disconnected, while reconnect is true. The socketAddressSupplier can supply the reconnect address.- Parameters:
reconnectDelay
- reconnect delay, must not benull
clientOptions
- client options for the current connection, must not benull
bootstrap
- Configuration for new channels, must not benull
timer
- Timer used for delayed reconnect, must not benull
reconnectWorkers
- executor group for reconnect tasks, must not benull
socketAddressSupplier
- the socket address supplier to obtain an address for reconnection, may benull
reconnectionListener
- the reconnection listener, must not benull
connectionFacade
- the connection facade, must not benull
eventBus
- Event bus to emit reconnect events.endpoint
- must not benull
-
-
Method Detail
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws Exception
- Specified by:
channelActive
in interfaceChannelInboundHandler
- Overrides:
channelActive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
scheduleReconnect
public void scheduleReconnect()
Schedule reconnect if channel is not available/not active.
-
run
public void run(int attempt) throws Exception
Reconnect to the remote address that the closed channel was connected to. This creates a newChannelPipeline
with 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
-true
to 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
-true
to suspend reconnection
-
isReconnectSuspended
public boolean isReconnectSuspended()
-
-