標籤:讀寫 socket 端到端 user ann type iter object rac
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之多使用者的聊天室(三)》中的用戶端程式,進行測試。
Netty之心跳檢測技術(四)