Nếu lớp ReadTimeoutHandler được sử dụng, thời gian chờ có thể được kiểm soát.
Sau đây là báo giá từ Javadoc.
public class MyPipelineFactory implements ChannelPipelineFactory {
private final Timer timer;
public MyPipelineFactory(Timer timer) {
this.timer = timer;
}
public ChannelPipeline getPipeline() {
// An example configuration that implements 30-second read timeout:
return Channels.pipeline(
new ReadTimeoutHandler(timer, 30), // timer must be shared.
new MyHandler());
}
}
ServerBootstrap bootstrap = ...;
Timer timer = new HashedWheelTimer();
...
bootstrap.setPipelineFactory(new MyPipelineFactory(timer));
...
Khi nó sẽ gây ra một time-out, MyHandler.exceptionCaught (ChannelHandlerContext ctx, ExceptionEvent e) được gọi với ReadTimeoutException.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
if (e.getCause() instanceof ReadTimeoutException) {
// NOP
}
ctx.getChannel().close();
}
Nguồn
2011-02-18 10:54:19