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 aWriteTimeoutExceptionwhen 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(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()); ...- See Also:
ReadTimeoutHandler,IdleStateHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classWriteTimeoutHandler.WriteTimeoutTask-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedprivate WriteTimeoutHandler.WriteTimeoutTasklastTaskA doubly-linked list to track all WriteTimeoutTasksprivate static longMIN_TIMEOUT_NANOSprivate longtimeoutNanos
-
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 voidaddWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task)voidhandlerRemoved(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.private voidremoveWriteTimeoutTask(WriteTimeoutHandler.WriteTimeoutTask task)private voidscheduleTimeout(ChannelHandlerContext ctx, ChannelPromise promise)voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.protected voidwriteTimedOut(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- theTimeUnitoftimeout
-
-
Method Detail
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto 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:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin 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
-
-