netty 5 心跳
netty 5 心跳
package com.server;import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.timeout.IdleStateHandler; public class server { public static void main(String[] args){ // 创建一个 server 服务 ServerBootstrap server = new ServerBootstrap(); EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); // 绑定 try{ server.group(boss, worker); server.channel( NioServerSocketChannel.class); server.childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new IdleStateHandler(5, 5, 10)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new ServerHandler()); } }); //netty3中对应设置如下 //bootstrap.setOption("backlog", 1024); //bootstrap.setOption("tcpNoDelay", true); //bootstrap.setOption("keepAlive", true); //设置参数,TCP参数 server.option(ChannelOption.SO_BACKLOG, 2048);//serverSocketchannel的设置,链接缓冲池的大小 server.childOption(ChannelOption.SO_KEEPALIVE, true);//socketchannel的设置,维持链接的活跃,清除死链接 server.childOption(ChannelOption.TCP_NODELAY, true);//socketchannel的设置,关闭延迟发送 ChannelFuture future = server.bind(10001); System.out.println("start!"); //等待服务端关闭 future.channel().closeFuture().sync(); }catch(Exception e){ e.printStackTrace(); }finally{ worker.shutdownGracefully(); boss.shutdownGracefully(); } } }
ServerHandler 代码
package com.server;import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; public class ServerHandler extends SimpleChannelInboundHandler<String> { @Override public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception { if(evt instanceof IdleStateEvent){ IdleStateEvent event = (IdleStateEvent)evt; if(event.state() == IdleState.ALL_IDLE){ System.out.println("你很久没有联系了"); ChannelFuture future = ctx.writeAndFlush("you will close"); future.addListener( new ChannelFutureListener(){ @Override public void operationComplete(ChannelFuture future) throws Exception { // TODO Auto-generated method stub ctx.channel().close(); } }); } }else{ super.userEventTriggered(ctx, evt); } } @Override protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(msg); ctx.channel().writeAndFlush("hi"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub super.exceptionCaught(ctx, cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub System.out.println("欢迎链接"); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub System.out.println("断开链接"); super.channelInactive(ctx); } }
注意需要引入 netty 5 的jar包
ch.pipeline().addLast(new IdleStateHandler(5, 5, 10)); 给管道加入一个心跳事件, 5 读超时 5写超时 10 读写超时 超过5秒没有读就会发起一个读超时事件Handler 中的 userEventTriggered 这个方法为心跳触发事件,当事件类型为 IdleStateEvent 为超时类型事件 当返回的状态为 IdleState.ALL_IDLE 为读写超时
转载于:https://www.cnblogs.com/qinshuipo/articles/6727022.html
netty 5 心跳相关推荐
- 浅析 Netty 实现心跳机制与断线重连
基础 何为心跳 顾名思义, 所谓 心跳, 即在 TCP 长连接中, 客户端和服务器之间定期发送的一种特殊的数据包, 通知对方自己还在线, 以确保 TCP 连接的有效性. 为什么需要心跳 因为网络的不可 ...
- 四、Netty 实现心跳机制与断线重连
一.概述 何为心跳 顾名思义, 所谓心跳, 即在 TCP 长连接中, 客户端和服务器之间定期发送的一种特殊的数据包, 通知对方自己还在线, 以确保 TCP 连接的有效性. 为什么需要心跳 因为网络的不 ...
- netty之心跳机制
目录 一.前言 二.netty的心跳工具 三.IdleStatehandler 1.构造方法 2.handlerAdded 3.定时任务 4.读事件空闲 5.写事件空闲 一.前言 心跳机制就是定时的给 ...
- 【Netty】利用Netty实现心跳检测和重连机制
一.前言 心跳机制是定时发送一个自定义的结构体(心跳包),让对方知道自己还活着,以确保连接的有效性的机制. 我们用到的很多框架都用到了心跳检测,比如服务注册到 Eureka Server 之后会维 ...
- Netty的心跳机制
文章目录 一.引入 二.工作原理 三.实现 四.源码剖析 五.总结 一.引入 在 TCP 保持长连接的过程中,可能会出现断网等网络异常出现,异常发生的时候, client 与 server 之间如果没 ...
- java 心跳框架_java架构师学习路线-如何使用Netty实现心跳检测
心跳检测是Socket通信经常使用的保证网络连接正常的技术.那么如何实现心跳检测呢?图灵学院今天着重来为大家介绍一下如何使用Netty实现心跳检测. 1.新建java工程,并导入netty使用的jar ...
- Netty实现心跳机制与断线重连
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来源:https://www.jianshu.com/p/ ...
- Netty空闲心跳检测机制
概述 Netty提供了一个读写空闲心跳检测机制的Handler,用于检测读空闲.写空闲.读写空闲. 如果服务端在一定时间内没有执行读请求,就会触发读空闲事件,一定时间内没有执行写请求,就会触发写空闲事 ...
- 开发im即时通讯如何用Netty实现心跳机制、断线重连机制
所谓心跳, 即在 TCP 长连接中, 客户端和服务器之间定期发送的一种特殊的数据包, 通知对方自己还在线, 以确保 TCP 连接的有效性. 注:心跳包还有另一个作用,经常被忽略,即:一个连接如果长时间 ...
- java通过netty实现心跳机制_Netty4服务端心跳机制
Netty4与Netty3.x的心跳机制略有不同,在Netty4中已经去掉了IdleStateAwareChannelHandler这个类,但IdleStateHandler依旧保留,只是心跳超时的触 ...
最新文章
- 拉普拉斯平滑处理 Laplace Smoothing
- apache2.0性能优化
- 实现SELECT的全选,反选,AB选的JAVASCRIPT代码
- Android Studio 全面教程
- bootstrap-table使用 带条件查询翻页及数据更新的问题。
- WPF教程尝试(修正部分格式)
- java检测textarea换行_Textarea和Java 换行符
- Java文件编码格式转换
- 【编程】概念的理解 —— socket
- 谷歌宣布开源 Live Transcribe 语音识别转文字工具
- 2013-07-29 IT 要闻速记快想
- 基于Web的svg编辑器(2)——层次结构设计(DOM结构)
- 宏基4750网卡驱动linux,宏基4750g驱动下载-宏基4750g网卡驱动程序官方版 - 极光下载站...
- elisa数据处理过程图解_(完整版)ELISA原理和分类(附图解)
- 什么是5G承载网?【转载自微信公众号网络技术联盟站】
- 80核处理器_最受欢迎的处理器 酷睿i5-9400F果然霸榜了
- 万物皆可健身环:UP主爆改switch,用健身环玩起《塞尔达传说之旷野「喘」息》...
- 怎么用计算机打出错误,电脑连接打印机怎么一直显示错误怎么办
- windows 建立窗口的程序代码
- [郭德纲]挤兑死人不带脏字
热门文章
- android 资源文件
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](十一)
- 一个简单mvp安卓应用的设计
- c#对PL/SQL查询结果列复制的结果生成指定格式
- HDU-1042 N!
- 悟透LoadRunner - 调用外部DLL的点点滴滴
- 在mininet中测试TCP、UDP带宽并作图
- notion函数_Notion 常见问题一览
- 汉罗塔python_基于Python的汉诺塔算法
- java 隐式构造,java隐式创建的对象