Class ConnectionReconnectHandler

java.lang.Object
com.lucimber.dbus.connection.AbstractDuplexHandler
com.lucimber.dbus.connection.ConnectionReconnectHandler
All Implemented Interfaces:
Handler, InboundHandler, OutboundHandler

public final class ConnectionReconnectHandler extends AbstractDuplexHandler
A connection handler that implements automatic reconnection with exponential backoff when connection failures are detected.

This handler integrates with the connection pipeline and monitors connection events to automatically attempt reconnection when the connection is lost. It uses exponential backoff to avoid overwhelming the server with reconnection attempts.

The handler listens for connection state changes and failures, triggering reconnection attempts based on the configured backoff strategy.

  • Constructor Details

    • ConnectionReconnectHandler

      public ConnectionReconnectHandler(ConnectionConfig config)
      Creates a new reconnection handler with the specified configuration.
      Parameters:
      config - The connection configuration containing reconnection settings
  • Method Details

    • getAttemptCount

      public int getAttemptCount()
      Gets the current number of reconnection attempts.
      Returns:
      The current attempt count
    • getLastReconnectAttempt

      public Instant getLastReconnectAttempt()
      Gets the timestamp of the last reconnection attempt.
      Returns:
      The timestamp of the last reconnection attempt, or null if none
    • isEnabled

      public boolean isEnabled()
      Gets whether reconnection is currently enabled.
      Returns:
      true if reconnection is active
    • cancelReconnection

      public void cancelReconnection()
      Manually cancels any pending reconnection attempts.
    • reset

      public void reset()
      Resets the reconnection state, clearing attempt count and timers.
    • onHandlerAdded

      public void onHandlerAdded(Context ctx)
      Description copied from interface: Handler
      Invoked when this handler is added to the pipeline.

      This is typically the first lifecycle method called for a handler.

      Specified by:
      onHandlerAdded in interface Handler
      Overrides:
      onHandlerAdded in class AbstractDuplexHandler
      Parameters:
      ctx - the Context this handler is bound to.
    • onHandlerRemoved

      public void onHandlerRemoved(Context ctx)
      Description copied from interface: Handler
      Invoked just before this handler is removed from the pipeline.

      This allows the handler to perform any necessary cleanup.

      Specified by:
      onHandlerRemoved in interface Handler
      Overrides:
      onHandlerRemoved in class AbstractDuplexHandler
      Parameters:
      ctx - the Context this handler is bound to.
    • onConnectionActive

      public void onConnectionActive(Context ctx)
      Description copied from interface: Handler
      Invoked when the connection associated with this context becomes active.
      Specified by:
      onConnectionActive in interface Handler
      Overrides:
      onConnectionActive in class AbstractDuplexHandler
      Parameters:
      ctx - the Context this handler is bound to.
    • onConnectionInactive

      public void onConnectionInactive(Context ctx)
      Description copied from interface: Handler
      Invoked when the connection associated with this context becomes inactive.
      Specified by:
      onConnectionInactive in interface Handler
      Overrides:
      onConnectionInactive in class AbstractDuplexHandler
      Parameters:
      ctx - the Context this handler is bound to.
    • handleInboundFailure

      public void handleInboundFailure(Context ctx, Throwable cause)
      Description copied from interface: InboundHandler
      Invoked when an error occurs during the processing of an inbound message.

      This method can be used to log, transform, or recover from the error as appropriate.

      Specified by:
      handleInboundFailure in interface InboundHandler
      Overrides:
      handleInboundFailure in class AbstractDuplexHandler
      Parameters:
      ctx - the Context this handler is bound to.
      cause - the Throwable describing the failure.
    • shutdown

      public void shutdown()
      Shuts down the reconnection handler and its scheduler.
    • getLogger

      protected org.slf4j.Logger getLogger()
      Description copied from class: AbstractDuplexHandler
      Returns the logger of the subclass.
      Specified by:
      getLogger in class AbstractDuplexHandler
      Returns:
      the logger