Class ConnectionHealthHandler

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

public final class ConnectionHealthHandler extends AbstractDuplexHandler
A connection handler that monitors the health of a D-Bus connection by performing periodic ping operations using the standard D-Bus Peer.Ping method.

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 Details

    • ConnectionHealthHandler

      public ConnectionHealthHandler(ConnectionConfig config)
      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

      public void addConnectionEventListener(ConnectionEventListener listener)
      Adds a connection event listener.
      Parameters:
      listener - the listener to add, ignored if null
      Since:
      1.0.0
    • removeConnectionEventListener

      public void removeConnectionEventListener(ConnectionEventListener listener)
      Removes a connection event listener.
      Parameters:
      listener - the listener to remove
      Since:
      1.0.0
    • getCurrentState

      public ConnectionState getCurrentState()
      Gets the current connection state.
      Returns:
      the current connection state
      Since:
      1.0.0
    • getLastSuccessfulCheck

      public Instant 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

      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.
    • handleInboundMessage

      public void handleInboundMessage(Context ctx, InboundMessage msg)
      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 interface InboundHandler
      Overrides:
      handleInboundMessage in class AbstractDuplexHandler
      Parameters:
      ctx - the Context this handler is bound to.
      msg - the InboundMessage being processed.
    • 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.
    • triggerHealthCheck

      public CompletableFuture<Void> 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 class AbstractDuplexHandler
      Returns:
      the logger