Package io.netty.handler.timeout
Class WriteTimeoutHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelOutboundHandlerAdapter
io.netty.handler.timeout.WriteTimeoutHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelOutboundHandler
Raises a
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(Channel
channel) { channel.pipeline().addLast("writeTimeoutHandler", newWriteTimeoutHandler
(30); channel.pipeline().addLast("myHandler", new MyHandler()); } } // Handler should handle theWriteTimeoutException
. public class MyHandler extendsChannelDuplexHandler
{@Override
public void exceptionCaught(ChannelHandlerContext
ctx,Throwable
cause) throwsException
{ if (cause instanceofWriteTimeoutException
) { // do something } else { super.exceptionCaught(ctx, cause); } } }ServerBootstrap
bootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
A doubly-linked list to track all WriteTimeoutTasksprivate static final long
private final long
-
Constructor Summary
ConstructorsConstructorDescriptionWriteTimeoutHandler
(int timeoutSeconds) Creates a new instance.WriteTimeoutHandler
(long timeout, TimeUnit unit) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
void
Do nothing by default, sub-classes may override this method.private void
private void
scheduleTimeout
(ChannelHandlerContext ctx, ChannelPromise promise) void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.protected void
Is called when a write timeout was detectedMethods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught, handlerAdded
-
Field Details
-
MIN_TIMEOUT_NANOS
private static final long MIN_TIMEOUT_NANOS -
timeoutNanos
private final long timeoutNanos -
lastTask
A doubly-linked list to track all WriteTimeoutTasks -
closed
private boolean closed
-
-
Constructor Details
-
WriteTimeoutHandler
public WriteTimeoutHandler(int timeoutSeconds) Creates a new instance.- Parameters:
timeoutSeconds
- write timeout in seconds
-
WriteTimeoutHandler
Creates a new instance.- Parameters:
timeout
- write timeoutunit
- theTimeUnit
oftimeout
-
-
Method Details
-
write
Description copied from class:ChannelOutboundHandlerAdapter
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelOutboundHandlerAdapter
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
scheduleTimeout
-
addWriteTimeoutTask
-
removeWriteTimeoutTask
-
writeTimedOut
Is called when a write timeout was detected- Throws:
Exception
-