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之多用戶的聊天室(三)》中的客戶端程序,進行測試。