Package com.tnsai.protocols.handlers
Class AbstractProtocolHandler
java.lang.Object
com.tnsai.protocols.handlers.AbstractProtocolHandler
- All Implemented Interfaces:
ProtocolHandler
- Direct Known Subclasses:
BroadcastHandler,DebateProtocolHandler,DirectMessageHandler,PubSubHandler,RequestReplyHandler
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.protocols.handlers.ProtocolHandler
ProtocolHandler.HandlerCallback, ProtocolHandler.HandlerException -
Method Summary
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.protocols.handlers.ProtocolHandler
send, sendAsync
-
Method Details
-
start
Starts the protocol handler and underlying transport.Idempotent: If already started, logs warning and returns without error.
Execution Flow:
- Check if already running (idempotent check)
- Call
onStart()hook for subclass initialization - Start transport
- Set running = true
- Specified by:
startin interfaceProtocolHandler- 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:
- Log stop attempt
- Stop transport (even if already stopped)
- Set running = false
- Call
onStop()hook for subclass cleanup
Exception Handling: Does not throw exceptions, only logs errors.
- Specified by:
stopin interfaceProtocolHandler
-
isRunning
public final boolean isRunning()Checks if handler is running.Returns true only if:
- Handler's
runningflag is true - AND underlying transport is running
- Specified by:
isRunningin interfaceProtocolHandler- Returns:
- true if handler and transport are both running
- Handler's
-
getTransport
Gets the underlying transport.- Specified by:
getTransportin interfaceProtocolHandler- Returns:
- Transport instance (never null)
-