IdleStateHandler:当连接空闲时间太长时,将会触发一个IdleStateEvent事件。然后你可以通过在你的ChannlInboundHandler中重写userEventTriggered()方法来处理该IdleStateEvent事件。
ReadTimeoutHandler:如果在指定时间间隔内没有收到任何的入站数据,则抛出一个ReadTimeoutException并关闭对应的Channel。可以通过重写你的ChannelHandler中的exceptionCaught()方法来检测该ReadTimeoutException
WriteTimeoutHandler:如果在指定时间间隔内没有收到任何的出站数据,则抛出一个WriteTimeoutException并关闭对应的Channel。可以通过重写你的ChannelHandler中的exceptionCaught()方法来检测该WriteTimeoutException。
基于IdleStateHandler 我们可以实现心态检测:
package netty.pushmsg; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.handler.timeout.IdleStateEvent; import io.netty.util.CharsetUtil; import io.netty.util.concurrent.GenericFutureListener; import java.util.Iterator; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** * 心跳检查 */ @ChannelHandler.Sharable public final class HeartbeatHandler extends ChannelInboundHandlerAdapter { private static final ByteBuf HEARTBEAT_SEQUENCE = Unpooled.unreleasableBuffer( Unpooled.copiedBuffer("HEARTBEAT", CharsetUtil.ISO_8859_1)); private static final Set<Channel> CHANNELS = ConcurrentHashMap.newKeySet(); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { CHANNELS.add(ctx.channel()); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { CHANNELS.remove(ctx.channel()); ctx.channel().close(); super.channelInactive(ctx); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { CHANNELS.remove(ctx.channel()); ctx.channel().close(); //TODO 打印日志 cause.printStackTrace(); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { //链路空闲了发送心跳包 ctx.writeAndFlush(HEARTBEAT_SEQUENCE).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { future.channel().close(); CHANNELS.remove(future.channel()); } } }); } else { super.userEventTriggered(ctx, evt); } } public static Iterator<Channel> getActiveChannels() { return CHANNELS.iterator(); } }