Class WriteOperationListener<T extends io.netty.util.concurrent.Future<?>>
- Type Parameters:
T
- the type of future being listened to, must extendFuture
- All Implemented Interfaces:
io.netty.util.concurrent.GenericFutureListener<T>
,EventListener
GenericFutureListener
that provides consistent logging for write operations and
optionally chains custom completion logic.
This listener automatically logs the outcome of write operations using the LoggerUtils.TRANSPORT
marker with appropriate log levels:
- DEBUG - Successful operations
- ERROR - Failed operations with exception details
- WARN - Cancelled operations
After logging, the listener can optionally execute custom completion logic via the provided
Consumer
function.
Usage Examples
Basic logging only:
ctx.writeAndFlush(message)
.addListener(new WriteOperationListener<>(LOGGER));
With custom completion logic:
ctx.writeAndFlush(message)
.addListener(new WriteOperationListener<>(LOGGER, future -> {
if (future.isSuccess()) {
// Handle successful write
processSuccessfulWrite();
} else {
// Handle failed write
handleWriteFailure(future.cause());
}
}));
SASL authentication example:
ctx.writeAndFlush(authMessage)
.addListener(new WriteOperationListener<>(LOGGER, future -> {
if (future.isSuccess()) {
currentState = State.AWAITING_RESPONSE;
startResponseTimeout(ctx);
} else {
failAuthentication(ctx, "Failed to send auth message");
}
}));
This class is thread-safe and can be used across multiple channels and handlers. The logger instance is not required to be static final, making it suitable for use in various contexts where different loggers may be needed.
- Since:
- 1.0
- See Also:
-
GenericFutureListener
LoggerUtils.TRANSPORT
-
Constructor Summary
ConstructorsConstructorDescriptionWriteOperationListener
(org.slf4j.Logger logger) Creates a new WriteOperationListener that only performs logging.WriteOperationListener
(org.slf4j.Logger logger, Consumer<T> futureConsumer) Creates a new WriteOperationListener with custom completion logic. -
Method Summary
Modifier and TypeMethodDescriptionvoid
operationComplete
(T future) Called when the write operation completes, regardless of success or failure.
-
Constructor Details
-
WriteOperationListener
public WriteOperationListener(org.slf4j.Logger logger) Creates a new WriteOperationListener that only performs logging.This constructor creates a listener that will log the outcome of write operations but will not execute any custom completion logic.
- Parameters:
logger
- the logger to use for logging write operation outcomes; must not be null- Throws:
NullPointerException
- if logger is null
-
WriteOperationListener
Creates a new WriteOperationListener with custom completion logic.This constructor creates a listener that will log the outcome of write operations and then execute the provided custom completion logic.
- Parameters:
logger
- the logger to use for logging write operation outcomes; must not be nullfutureConsumer
- optional consumer to execute custom completion logic; may be null- Throws:
NullPointerException
- if logger is null
-
-
Method Details
-
operationComplete
Called when the write operation completes, regardless of success or failure.This method first logs the outcome of the write operation using the
LoggerUtils.TRANSPORT
marker:- If the operation was successful, logs at DEBUG level
- If the operation failed with an exception, logs at ERROR level with the exception
- If the operation was cancelled, logs at WARN level
After logging, if a custom completion consumer was provided during construction, it will be executed with the future as its argument.
This method is thread-safe and can be called from any thread.
- Specified by:
operationComplete
in interfaceio.netty.util.concurrent.GenericFutureListener<T extends io.netty.util.concurrent.Future<?>>
- Parameters:
future
- the completed future representing the write operation result
-