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(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()); ...
ChannelHandler.Sharable
构造器和说明 |
---|
WriteTimeoutHandler(int timeoutSeconds)
Creates a new instance.
|
WriteTimeoutHandler(long timeout,
java.util.concurrent.TimeUnit unit)
Creates a new instance.
|
限定符和类型 | 方法和说明 |
---|---|
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelHandlerContext.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, read
ensureNotSharable, exceptionCaught, handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerAdded
public WriteTimeoutHandler(int timeoutSeconds)
timeoutSeconds
- write timeout in secondspublic WriteTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- write timeoutunit
- the TimeUnit
of timeout
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapter
ChannelHandlerContext.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
在接口中 ChannelOutboundHandler
write
在类中 ChannelOutboundHandlerAdapter
ctx
- 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
ChannelHandlerAdapter
handlerRemoved
在接口中 ChannelHandler
handlerRemoved
在类中 ChannelHandlerAdapter
java.lang.Exception
protected void writeTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception