天猫网站建设可行性分析,网站建设软件开发工作室整站模板,云南网站建设价格,安徽工程建设信息网实名制查询本文介绍了 Netty 超时机制的原理#xff0c;以及如何在连接闲置时发送一个心跳来维持连接。Netty 超时机制的介绍Netty 的超时类型 IdleState 主要分为#xff1a;ALL_IDLE : 一段时间内没有数据接收或者发送READER_IDLE #xff1a; 一段时间内没有数据接收WRITER_IDLE 以及如何在连接闲置时发送一个心跳来维持连接。Netty 超时机制的介绍Netty 的超时类型 IdleState 主要分为ALL_IDLE : 一段时间内没有数据接收或者发送READER_IDLE 一段时间内没有数据接收WRITER_IDLE 一段时间内没有数据发送在 Netty 的 timeout 包下主要类有IdleStateEvent 超时的事件IdleStateHandler 超时状态处理ReadTimeoutHandler 读超时状态处理WriteTimeoutHandler 写超时状态处理其中 IdleStateHandler 包含了读\写超时状态处理比如private static final int READ_IDEL_TIME_OUT 4; // 读超时private static final int WRITE_IDEL_TIME_OUT 5;// 写超时private static final int ALL_IDEL_TIME_OUT 7; // 所有超时new IdleStateHandler(READ_IDEL_TIME_OUT,WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS));上述例子在 IdleStateHandler 中定义了读超时的时间是 4 秒 写超时的时间是 5 秒其他所有的超时时间是 7 秒。应用 IdleStateHandler既然 IdleStateHandler 包括了读\写超时状态处理那么很多时候 ReadTimeoutHandler 、 WriteTimeoutHandler 都可以不用使用。定义另一个名为 HeartbeatHandlerInitializer 的 ChannelInitializer public class HeartbeatHandlerInitializer extends ChannelInitializer {private static final int READ_IDEL_TIME_OUT 4; // 读超时private static final int WRITE_IDEL_TIME_OUT 5;// 写超时private static final int ALL_IDEL_TIME_OUT 7; // 所有超时Overrideprotected void initChannel(Channel ch) throws Exception {ChannelPipeline pipeline ch.pipeline();pipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT,WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS)); // 1pipeline.addLast(new HeartbeatServerHandler()); // 2}}使用了 IdleStateHandler 分别设置了读、写超时的时间定义了一个 HeartbeatServerHandler 处理器用来处理超时时发送心跳定义了一个心跳处理器public class HeartbeatServerHandler extends ChannelInboundHandlerAdapter {// Return a unreleasable view on the given ByteBuf// which will just ignore release and retain calls.private static final ByteBuf HEARTBEAT_SEQUENCE Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(Heartbeat,CharsetUtil.UTF_8)); // 1Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt)throws Exception {if (evt instanceof IdleStateEvent) { // 2IdleStateEvent event (IdleStateEvent) evt;String type ;if (event.state() IdleState.READER_IDLE) {type read idle;} else if (event.state() IdleState.WRITER_IDLE) {type write idle;} else if (event.state() IdleState.ALL_IDLE) {type all idle;}ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate()).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); // 3System.out.println( ctx.channel().remoteAddress()超时类型 type);} else {super.userEventTriggered(ctx, evt);}}}定义了心跳时要发送的内容判断是否是 IdleStateEvent 事件是则处理将心跳内容发送给客户端服务器服务器代码比较简单启动后侦听 8082 端口public final class HeartbeatServer {static final int PORT 8082;public static void main(String[] args) throws Exception {// Configure the server.EventLoopGroup bossGroup new NioEventLoopGroup(1);EventLoopGroup workerGroup new NioEventLoopGroup();try {ServerBootstrap b new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HeartbeatHandlerInitializer());// Start the server.ChannelFuture f b.bind(PORT).sync();// Wait until the server socket is closed.f.channel().closeFuture().sync();} finally {// Shut down all event loops to terminate all threads.bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}}客户端测试客户端用操作系统自带的 Telnet 程序即可telnet 127.0.0.1 8082效果20151106-heartbeat源码欢迎留言讨论加关注持续更新