Package com.lucimber.dbus.connection
Class ConnectionReconnectHandler
java.lang.Object
com.lucimber.dbus.connection.AbstractDuplexHandler
com.lucimber.dbus.connection.ConnectionReconnectHandler
- All Implemented Interfaces:
Handler,InboundHandler,OutboundHandler
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 Summary
ConstructorsConstructorDescriptionCreates a new reconnection handler with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidManually cancels any pending reconnection attempts.intGets the current number of reconnection attempts.Gets the timestamp of the last reconnection attempt.protected org.slf4j.LoggerReturns the logger of the subclass.voidhandleInboundFailure(Context ctx, Throwable cause) Invoked when an error occurs during the processing of an inbound message.booleanGets whether reconnection is currently enabled.voidInvoked when the connection associated with this context becomes active.voidInvoked when the connection associated with this context becomes inactive.voidonHandlerAdded(Context ctx) Invoked when this handler is added to the pipeline.voidonHandlerRemoved(Context ctx) Invoked just before this handler is removed from the pipeline.voidreset()Resets the reconnection state, clearing attempt count and timers.voidshutdown()Shuts down the reconnection handler and its scheduler.Methods inherited from class com.lucimber.dbus.connection.AbstractDuplexHandler
handleInboundMessage, handleOutboundMessage, handleUserEvent
-
Constructor Details
-
ConnectionReconnectHandler
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
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
Description copied from interface:HandlerInvoked when this handler is added to the pipeline.This is typically the first lifecycle method called for a handler.
- Specified by:
onHandlerAddedin interfaceHandler- Overrides:
onHandlerAddedin classAbstractDuplexHandler- Parameters:
ctx- theContextthis handler is bound to.
-
onHandlerRemoved
Description copied from interface:HandlerInvoked just before this handler is removed from the pipeline.This allows the handler to perform any necessary cleanup.
- Specified by:
onHandlerRemovedin interfaceHandler- Overrides:
onHandlerRemovedin classAbstractDuplexHandler- Parameters:
ctx- theContextthis handler is bound to.
-
onConnectionActive
Description copied from interface:HandlerInvoked when the connection associated with this context becomes active.- Specified by:
onConnectionActivein interfaceHandler- Overrides:
onConnectionActivein classAbstractDuplexHandler- Parameters:
ctx- theContextthis handler is bound to.
-
onConnectionInactive
Description copied from interface:HandlerInvoked when the connection associated with this context becomes inactive.- Specified by:
onConnectionInactivein interfaceHandler- Overrides:
onConnectionInactivein classAbstractDuplexHandler- Parameters:
ctx- theContextthis handler is bound to.
-
handleInboundFailure
Description copied from interface:InboundHandlerInvoked 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:
handleInboundFailurein interfaceInboundHandler- Overrides:
handleInboundFailurein classAbstractDuplexHandler- Parameters:
ctx- theContextthis handler is bound to.cause- theThrowabledescribing the failure.
-
shutdown
public void shutdown()Shuts down the reconnection handler and its scheduler. -
getLogger
protected org.slf4j.Logger getLogger()Description copied from class:AbstractDuplexHandlerReturns the logger of the subclass.- Specified by:
getLoggerin classAbstractDuplexHandler- Returns:
- the logger
-