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
public class WriteTimeoutHandler extends ChannelOutboundHandlerAdapter
Raises aWriteTimeoutException
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 extends
ChannelInitializer
<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:
ReadTimeoutHandler
,IdleStateHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
WriteTimeoutHandler.WriteTimeoutTask
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
closed
private WriteTimeoutHandler.WriteTimeoutTask
lastTask
A doubly-linked list to track all WriteTimeoutTasksprivate static long
MIN_TIMEOUT_NANOS
private long
timeoutNanos
-
Constructor Summary
Constructors Constructor Description WriteTimeoutHandler(int timeoutSeconds)
Creates a new instance.WriteTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.protected void
writeTimedOut(ChannelHandlerContext ctx)
Is called when a write timeout was detected-
Methods 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 Detail
-
MIN_TIMEOUT_NANOS
private static final long MIN_TIMEOUT_NANOS
-
timeoutNanos
private final long timeoutNanos
-
lastTask
private WriteTimeoutHandler.WriteTimeoutTask lastTask
A doubly-linked list to track all WriteTimeoutTasks
-
closed
private boolean closed
-
-
Constructor Detail
-
WriteTimeoutHandler
public WriteTimeoutHandler(int timeoutSeconds)
Creates a new instance.- Parameters:
timeoutSeconds
- write timeout in seconds
-
WriteTimeoutHandler
public WriteTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
Creates a new instance.- Parameters:
timeout
- write timeoutunit
- theTimeUnit
oftimeout
-
-
Method Detail
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
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:
java.lang.Exception
- thrown if an error occurs
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
scheduleTimeout
private void scheduleTimeout(ChannelHandlerContext ctx, ChannelPromise promise)
-
addWriteTimeoutTask
private void addWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task)
-
removeWriteTimeoutTask
private void removeWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task)
-
writeTimedOut
protected void writeTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
Is called when a write timeout was detected- Throws:
java.lang.Exception
-
-