心跳检测技术
现有A、B两端已经互相连接,但是他们之间很长时间没有数据交互,那么A与B如何判断这个连接是否可用呢?我们通常的做法就是,让任何一方,例如我们让A端,定时的发送(例如每5秒钟)一句问候"Are you ok?",如果B都到来自A的问候,回了一句"GUN",A收到了来自B的信息,也不在乎B到底给我回了什么,即可以断定与B的连接并没有断开;如果没有收到来自B的任何回复,过段时间在去发送问候,那么我们通常认为连续3次问候,都没有收到来自B的恢复,即认为A与B之间的连接已经断开。那么就得尝试着重新获取一个新的连接
Server
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(8899).sync();
channelFuture.channel().closeFuture().sync();
}finally{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
ServerInitializer
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());
}
}
ServerHandler
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;
default:
break;
}
System.out.println(ctx.channel().remoteAddress() + " " + idleType);
ctx.channel().close();
}
}
}