public class ReadTimeoutHandler extends IdleStateHandler
ReadTimeoutException
when no data was read within a certain
period of time.
// The connection is closed when there is no inbound traffic // for 30 seconds. public class MyChannelInitializer extendsChannelInitializer
<Channel
> { public void initChannel(Channel
channel) { channel.pipeline().addLast("readTimeoutHandler", newReadTimeoutHandler
(30)); channel.pipeline().addLast("myHandler", new MyHandler()); } } // Handler should handle theReadTimeoutException
. public class MyHandler extendsChannelDuplexHandler
{@Override
public void exceptionCaught(ChannelHandlerContext
ctx,Throwable
cause) throwsException
{ if (cause instanceofReadTimeoutException
) { // do something } else { super.exceptionCaught(ctx, cause); } } }ServerBootstrap
bootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...
ChannelHandler.Sharable
构造器和说明 |
---|
ReadTimeoutHandler(int timeoutSeconds)
Creates a new instance.
|
ReadTimeoutHandler(long timeout,
java.util.concurrent.TimeUnit unit)
Creates a new instance.
|
限定符和类型 | 方法和说明 |
---|---|
protected void |
channelIdle(ChannelHandlerContext ctx,
IdleStateEvent evt)
Is called when an
IdleStateEvent should be fired. |
protected void |
readTimedOut(ChannelHandlerContext ctx)
Is called when a read timeout was detected.
|
channelActive, channelInactive, channelRead, channelReadComplete, channelRegistered, getAllIdleTimeInMillis, getReaderIdleTimeInMillis, getWriterIdleTimeInMillis, handlerAdded, handlerRemoved, newIdleStateEvent, resetReadTimeout, resetWriteTimeout, write
bind, close, connect, deregister, disconnect, flush, read
channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
ensureNotSharable, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught
public ReadTimeoutHandler(int timeoutSeconds)
timeoutSeconds
- read timeout in secondspublic ReadTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- read timeoutunit
- the TimeUnit
of timeout
protected final void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws java.lang.Exception
IdleStateHandler
IdleStateEvent
should be fired. This implementation calls
ChannelHandlerContext.fireUserEventTriggered(Object)
.channelIdle
在类中 IdleStateHandler
java.lang.Exception
protected void readTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception