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()); ...
WriteTimeoutHandler
,
IdleStateHandler
ChannelHandler.Sharable
Constructor and Description |
---|
ReadTimeoutHandler(int timeoutSeconds)
Creates a new instance.
|
ReadTimeoutHandler(long timeout,
TimeUnit unit)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
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, 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 secondsprotected final void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception
IdleStateHandler
IdleStateEvent
should be fired. This implementation calls
ChannelHandlerContext.fireUserEventTriggered(Object)
.channelIdle
in class IdleStateHandler
Exception
protected void readTimedOut(ChannelHandlerContext ctx) throws Exception
Exception
Copyright © 2008–2017 The Netty Project. All rights reserved.