本文先容了 Netty 超机缘制的道理,以及如安在毗连闲置时发送一个心跳来维持毗连。
Netty 超机缘制的先容
Netty 的超时范例 IdleState 主要分为:
在 Netty 的 timeout 包下,主要类有:
个中 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<Channel> { 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; // 所有超时 @Override protected 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)); // 1 pipeline.addLast(new HeartbeatServerHandler()); // 2 } }
界说了一个心跳处理惩罚器
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)); // 1 @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { // 2 IdleStateEvent 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); // 3 System.out.println( ctx.channel().remoteAddress()+"超时范例:" + type); } else { super.userEventTriggered(ctx, evt); } } }
处事器
处事器代码较量简朴,劳务派遣管理系统,启动后侦听 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
结果
源码
见 https://github.com/waylau/netty-4-user-guide-demos 中 heartbeat包
参考