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 TypeMethodDescriptionvoidAdds a connection event listener.Gets the current connection state.Gets the timestamp of the last successful health check.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.voidhandleInboundMessage(Context ctx, InboundMessage msg) Invoked when an inbound message is received and propagated through the pipeline.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.voidRemoves a connection event listener.voidshutdown()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: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.
-
handleInboundMessage
Description copied from interface:InboundHandlerInvoked 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:
handleInboundMessagein interfaceInboundHandler- Overrides:
handleInboundMessagein classAbstractDuplexHandler- Parameters:
ctx- theContextthis handler is bound to.msg- theInboundMessagebeing processed.
-
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.
-
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:AbstractDuplexHandlerReturns the logger of the subclass.- Specified by:
getLoggerin classAbstractDuplexHandler- Returns:
- the logger
-