public class WriteTimeoutHandler extends ChannelOutboundHandlerAdapter
WriteTimeoutException
。
// The connection is closed when a write operation cannot finish in 30 seconds.
public class MyChannelInitializer extends ChannelInitializer
<Channel
> {
public void initChannel(Channel
channel) {
channel.pipeline().addLast("writeTimeoutHandler", new WriteTimeoutHandler
(30);
channel.pipeline().addLast("myHandler", new MyHandler());
}
}
// Handler should handle the WriteTimeoutException
.
public class MyHandler extends ChannelDuplexHandler
{
@Override
public void exceptionCaught(ChannelHandlerContext
ctx, Throwable
cause)
throws 异常
{
if (cause instanceof WriteTimeoutException
) {
// do something
} else {
super.exceptionCaught(ctx, cause);
}
}
}
ServerBootstrap
bootstrap = ...;
...
bootstrap.childHandler(new MyChannelInitializer());
...
ReadTimeoutHandler
, IdleStateHandler
ChannelHandler.Sharable
Constructor and Description |
---|
WriteTimeoutHandler(int timeoutSeconds)
创建一个新的实例。
|
WriteTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
创建一个新的实例。
|
Modifier and Type | Method and Description |
---|---|
void |
handlerRemoved(ChannelHandlerContext ctx)
子类可以忽略这个方法。
|
void |
write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
|
protected void |
writeTimedOut(ChannelHandlerContext ctx)
在检测到写入超时时调用
|
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
- 以秒为单位写入超时
public WriteTimeoutHandler(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- 写入超时
unit
-
TimeUnit
timeout
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.write(Object, ChannelPromise)
转发到ChannelPipeline
中的下一个ChannelOutboundHandler
。
子类可以重写此方法来更改行为。
write
,界面
ChannelOutboundHandler
write
在课堂上
ChannelOutboundHandlerAdapter
ctx
- 为其写入操作的ChannelHandlerContext
msg
- 要写入的消息
promise
- 在操作完成后通知ChannelPromise
java.lang.Exception
- 发生错误时抛出
public 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
Copyright © 2008–2018 The Netty Project. All rights reserved.