Class AbstractProtocolHandler

java.lang.Object
com.tnsai.protocols.handlers.AbstractProtocolHandler
All Implemented Interfaces:
ProtocolHandler
Direct Known Subclasses:
BroadcastHandler, DebateProtocolHandler, DirectMessageHandler, PubSubHandler, RequestReplyHandler

public abstract class AbstractProtocolHandler extends Object implements ProtocolHandler
Abstract base class for protocol handlers. Provides common lifecycle management (start/stop/isRunning) and transport handling.

Eliminates ~60 lines of duplicate code across 4 concrete handler implementations by centralizing:

  • Transport validation and storage
  • Running state management (volatile boolean)
  • Lifecycle methods with consistent error handling
  • Transport delegation (getTransport, isRunning)

Thread Safety: The running field is volatile to ensure visibility across threads. Subclasses should ensure their own thread safety for additional state.

Template Method Pattern: The start() method calls the protected onStart() hook, allowing subclasses to perform initialization before transport start.

Usage Example:


 public final class MyHandler extends AbstractProtocolHandler {
     public MyHandler(Transport transport) {
         super(transport);
     }

     @Override
     protected String getHandlerName() {
         return "MyHandler";
     }

     @Override
     protected void onStart() throws Exception {
         // Optional: Subclass-specific initialization
         logger.info("Initializing handler-specific resources");
     }

     @Override
     public Message send(Message message) throws HandlerException {
         validateRunning();  // Built-in helper
         return transport.send(message);
     }
 }
 
See Also:
  • Method Details

    • start

      public final void start() throws ProtocolHandler.HandlerException
      Starts the protocol handler and underlying transport.

      Idempotent: If already started, logs warning and returns without error.

      Execution Flow:

      1. Check if already running (idempotent check)
      2. Call onStart() hook for subclass initialization
      3. Start transport
      4. Set running = true
      Specified by:
      start in interface ProtocolHandler
      Throws:
      ProtocolHandler.HandlerException - if start fails (wraps underlying exceptions)
    • stop

      public final void stop()
      Stops the protocol handler and underlying transport.

      Idempotent: Safe to call multiple times, even if not started.

      Execution Flow:

      1. Log stop attempt
      2. Stop transport (even if already stopped)
      3. Set running = false
      4. Call onStop() hook for subclass cleanup

      Exception Handling: Does not throw exceptions, only logs errors.

      Specified by:
      stop in interface ProtocolHandler
    • isRunning

      public final boolean isRunning()
      Checks if handler is running.

      Returns true only if:

      • Handler's running flag is true
      • AND underlying transport is running
      Specified by:
      isRunning in interface ProtocolHandler
      Returns:
      true if handler and transport are both running
    • getTransport

      public final Transport getTransport()
      Gets the underlying transport.
      Specified by:
      getTransport in interface ProtocolHandler
      Returns:
      Transport instance (never null)