Class ConnectionHealthHandler
- All Implemented Interfaces:
Handler
,InboundHandler
,OutboundHandler
This handler integrates with the connection pipeline and manages connection state transitions, health checks, and event firing to registered listeners.
The health monitoring is performed using the standard D-Bus Peer.Ping method, which is supported by all D-Bus implementations. The handler automatically starts monitoring when the connection becomes active and stops when it becomes inactive.
Connection state transitions are tracked and notified to registered listeners through the
ConnectionEventListener
interface.
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new connection health handler with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a connection event listener.Gets the current connection state.Gets the timestamp of the last successful health check.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.void
handleInboundMessage
(Context ctx, InboundMessage msg) Invoked when an inbound message is received and propagated through the pipeline.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
Removes a connection event listener.void
shutdown()
Shuts down the health monitor and its executors.Manually triggers a health check.Methods inherited from class com.lucimber.dbus.connection.AbstractDuplexHandler
handleOutboundMessage, handleUserEvent
-
Constructor Details
-
ConnectionHealthHandler
Creates a new connection health handler with the specified configuration.- Parameters:
config
- the connection configuration, must not be null- Throws:
NullPointerException
- if config is null- Since:
- 1.0.0
-
-
Method Details
-
addConnectionEventListener
Adds a connection event listener.- Parameters:
listener
- the listener to add, ignored if null- Since:
- 1.0.0
-
removeConnectionEventListener
Removes a connection event listener.- Parameters:
listener
- the listener to remove- Since:
- 1.0.0
-
getCurrentState
Gets the current connection state.- Returns:
- the current connection state
- Since:
- 1.0.0
-
getLastSuccessfulCheck
Gets the timestamp of the last successful health check.- Returns:
- the timestamp of the last successful health check, or null if none
- Since:
- 1.0.0
-
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.
-
handleInboundMessage
Description copied from interface:InboundHandler
Invoked when an inbound message is received and propagated through the pipeline.Handlers may inspect, transform, or act upon the message before forwarding it downstream.
- Specified by:
handleInboundMessage
in interfaceInboundHandler
- Overrides:
handleInboundMessage
in classAbstractDuplexHandler
- Parameters:
ctx
- theContext
this handler is bound to.msg
- theInboundMessage
being processed.
-
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.
-
triggerHealthCheck
Manually triggers a health check.If the handler is not active or no context is available, this method returns a completed future immediately.
- Returns:
- a CompletableFuture that indicates when the health check is triggered
- Since:
- 1.0.0
-
shutdown
public void shutdown()Shuts down the health monitor and its executors.This method stops all health monitoring, cancels pending health checks, and shuts down the internal thread pools. It should be called when the handler is no longer needed to prevent resource leaks.
- Since:
- 1.0.0
-
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
-