Module lettuce.core

Class 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
  • 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 supplied ChannelGroup and establishes a new Channel when disconnected, while reconnect is true. The socketAddressSupplier can supply the reconnect address.
      Parameters:
      reconnectDelay - reconnect delay, must not be null
      clientOptions - client options for the current connection, must not be null
      bootstrap - Configuration for new channels, must not be null
      timer - Timer used for delayed reconnect, must not be null
      reconnectWorkers - executor group for reconnect tasks, must not be null
      socketAddressSupplier - the socket address supplier to obtain an address for reconnection, may be null
      reconnectionListener - the reconnection listener, must not be null
      connectionFacade - the connection facade, must not be null
      eventBus - Event bus to emit reconnect events.
      endpoint - must not be null
  • Method Details

    • channelActive

      public void channelActive​(ChannelHandlerContext ctx) throws Exception
      Specified by:
      channelActive in interface ChannelInboundHandler
      Overrides:
      channelActive in class ChannelInboundHandlerAdapter
      Throws:
      Exception
    • channelInactive

      public void channelInactive​(ChannelHandlerContext ctx) throws Exception
      Specified by:
      channelInactive in interface ChannelInboundHandler
      Overrides:
      channelInactive in class ChannelInboundHandlerAdapter
      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 new ChannelPipeline 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()