public class WriteTimeoutHandler extends ChannelOutboundHandlerAdapter
WriteTimeoutException when a write operation cannot finish in a certain period of time.
// The connection is closed when a write operation cannot finish in 30 seconds. public class MyChannelInitializer extendsChannelInitializer<Channel> { public void initChannel(Channelchannel) { channel.pipeline().addLast("writeTimeoutHandler", newWriteTimeoutHandler(30); channel.pipeline().addLast("myHandler", new MyHandler()); } } // Handler should handle theWriteTimeoutException. public class MyHandler extendsChannelDuplexHandler{@Overridepublic void exceptionCaught(ChannelHandlerContextctx,Throwablecause) throwsException{ if (cause instanceofWriteTimeoutException) { // do something } else { super.exceptionCaught(ctx, cause); } } }ServerBootstrapbootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
ReadTimeoutHandler,
IdleStateHandler| Modifier and Type | Class and Description |
|---|---|
private class |
WriteTimeoutHandler.WriteTimeoutTask |
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private boolean |
closed |
private WriteTimeoutHandler.WriteTimeoutTask |
lastTask
A doubly-linked list to track all WriteTimeoutTasks
|
private static long |
MIN_TIMEOUT_NANOS |
private long |
timeoutNanos |
| Constructor and Description |
|---|
WriteTimeoutHandler(int timeoutSeconds)
Creates a new instance.
|
WriteTimeoutHandler(long timeout,
java.util.concurrent.TimeUnit unit)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
addWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task) |
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
private void |
removeWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task) |
private void |
scheduleTimeout(ChannelHandlerContext ctx,
ChannelPromise promise) |
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
protected void |
writeTimedOut(ChannelHandlerContext ctx)
Is called when a write timeout was detected
|
bind, close, connect, deregister, disconnect, flush, readensureNotSharable, exceptionCaught, handlerAdded, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaught, handlerAddedprivate static final long MIN_TIMEOUT_NANOS
private final long timeoutNanos
private WriteTimeoutHandler.WriteTimeoutTask lastTask
private boolean closed
public WriteTimeoutHandler(int timeoutSeconds)
timeoutSeconds - write timeout in secondspublic WriteTimeoutHandler(long timeout,
java.util.concurrent.TimeUnit unit)
timeout - write timeoutunit - the TimeUnit of timeoutpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapterChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.write in interface ChannelOutboundHandlerwrite in class ChannelOutboundHandlerAdapterctx - the ChannelHandlerContext for which the write operation is mademsg - the message to writepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerRemoved in interface ChannelHandlerhandlerRemoved in class ChannelHandlerAdapterjava.lang.Exceptionprivate void scheduleTimeout(ChannelHandlerContext ctx, ChannelPromise promise)
private void addWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task)
private void removeWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task)
protected void writeTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception