public class ReadTimeoutHandler extends IdleStateHandler
ReadTimeoutException
。
// The connection is closed when there is no inbound traffic
// for 30 seconds.
public class MyChannelInitializer extends ChannelInitializer
<Channel
> {
public void initChannel(Channel
channel) {
channel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler
(30);
channel.pipeline().addLast("myHandler", new MyHandler());
}
}
// Handler should handle the ReadTimeoutException
.
public class MyHandler extends ChannelDuplexHandler
{
@Override
public void exceptionCaught(ChannelHandlerContext
ctx, Throwable
cause)
throws 异常
{
if (cause instanceof ReadTimeoutException
) {
// do something
} else {
super.exceptionCaught(ctx, cause);
}
}
}
ServerBootstrap
bootstrap = ...;
...
bootstrap.childHandler(new MyChannelInitializer());
...
WriteTimeoutHandler
, IdleStateHandler
ChannelHandler.Sharable
Constructor and Description |
---|
ReadTimeoutHandler(int timeoutSeconds)
创建一个新的实例。
|
ReadTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
创建一个新的实例。
|
Modifier and Type | Method and Description |
---|---|
protected void |
channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)
IdleStateEvent 应该被解雇时被调用。
|
protected void |
readTimedOut(ChannelHandlerContext ctx)
在检测到读取超时时调用。
|
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
- 以秒为单位读取超时
public ReadTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- 读取超时
unit
-
TimeUnit
timeout
protected final void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws java.lang.Exception
IdleStateHandler
IdleStateEvent
应该被解雇时被调用。
这个实现调用ChannelHandlerContext.fireUserEventTriggered(Object)
。
channelIdle
在课堂
IdleStateHandler
java.lang.Exception
protected void readTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception
Copyright © 2008–2018 The Netty Project. All rights reserved.