您的位置:首页 > 其它

Netty之心跳检测技术(四)

2017-06-13 12:59 405 查看
Netty之心跳检测技术(四)

一.简介

  "心跳"听起来感觉很牛X的样子,其实只是一种检测端到端连接状态的技术。举个简单的"栗子",现有A、B两端已经互相连接,但是他们之间很长时间没有数据交互,那么A与B如何判断这个连接是否可用呢?我们通常的做法就是,让任何一方,例如我们让A端,定时的发送(例如每5秒钟)一句问候"Are you ok?",如果B都到来自A的问候,回了一句"GUN",A收到了来自B的信息,也不在乎B到底给我回了什么,即可以断定与B的连接并没有断开;如果没有收到来自B的任何回复,过段时间在去发送问候,那么我们通常认为连续3次问候,都没有收到来自B的恢复,即认为A与B之间的连接已经断开。那么就得尝试着重新获取一个新的连接。

  那么Netty作为一个网络应用框架,肯定对这种心跳提供了响应的处理。那我们上代码吧。

二.心跳的实现

2.1 服务端启动程序

public class MyServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

try{
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))   //handler()方法主要是针对bossGroup的处理
.childHandler(new ServerInitializer());  //childHandler()主要是针对workerGroup的处理

ChannelFuture channelFuture = bootstrap.bind(8989).sync();
channelFuture.channel().closeFuture().sync();
}finally{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}


2.2服务端通道初始化

public class ServerInitializer extends ChannelInitializer<SocketChannel>{

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/**
* IdleStateHandler: 空闲状态处理器。
* 四个参数:1.读空闲; 2.写空闲;3.读写空闲; 4.时间单位。
* 所谓的空闲是指多长时间没有发生过对应的时间,就触发调用.
*/
pipeline.addLast(new IdleStateHandler(5, 7, 3, TimeUnit.SECONDS));
pipeline.addLast(new ServerHandler());
}
}


2.3服务端Handler

public class ServerHandler extends ChannelInboundHandlerAdapter{

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if(evt instanceof IdleStateEvent){
IdleStateEvent event = (IdleStateEvent)evt;

String idleType = null;
switch(event.state()){
case READER_IDLE:
idleType = "读空闲";
break;
case WRITER_IDLE:
idleType = "写空闲";
break;
case ALL_IDLE:
idleType = "读写空闲";
break;
}

System.out.println(ctx.channel().remoteAddress() + " " + idleType);

ctx.channel().close();
}
}
}


2.4 测试

运行服务端启动程序,然后再启动上一章 《Netty之多用户的聊天室(三)》中的客户端程序,进行测试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: