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 TypeMethodDescriptionvoid
Manually cancels any pending reconnection attempts.int
Gets the current number of reconnection attempts.Gets the timestamp of the last reconnection attempt.protected org.slf4j.Logger
Returns the logger of the subclass.void
handleInboundFailure
(Context ctx, Throwable cause) Invoked when an error occurs during the processing of an inbound message.boolean
Gets whether reconnection is currently enabled.void
Invoked when the connection associated with this context becomes active.void
Invoked when the connection associated with this context becomes inactive.void
onHandlerAdded
(Context ctx) Invoked when this handler is added to the pipeline.void
onHandlerRemoved
(Context ctx) Invoked just before this handler is removed from the pipeline.void
reset()
Resets the reconnection state, clearing attempt count and timers.void
shutdown()
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: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 interfaceHandler
- Overrides:
onHandlerAdded
in classAbstractDuplexHandler
- Parameters:
ctx
- theContext
this handler is bound to.
-
onHandlerRemoved
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 interfaceHandler
- Overrides:
onHandlerRemoved
in classAbstractDuplexHandler
- Parameters:
ctx
- theContext
this handler is bound to.
-
onConnectionActive
Description copied from interface:Handler
Invoked when the connection associated with this context becomes active.- Specified by:
onConnectionActive
in interfaceHandler
- Overrides:
onConnectionActive
in classAbstractDuplexHandler
- Parameters:
ctx
- theContext
this handler is bound to.
-
onConnectionInactive
Description copied from interface:Handler
Invoked when the connection associated with this context becomes inactive.- Specified by:
onConnectionInactive
in interfaceHandler
- Overrides:
onConnectionInactive
in classAbstractDuplexHandler
- Parameters:
ctx
- theContext
this handler is bound to.
-
handleInboundFailure
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 interfaceInboundHandler
- Overrides:
handleInboundFailure
in classAbstractDuplexHandler
- Parameters:
ctx
- theContext
this handler is bound to.cause
- theThrowable
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 classAbstractDuplexHandler
- Returns:
- the logger
-