在啓動客戶端的時候,咱們通常會ide
channelFuture.channel().closeFuture().sync();
這是一段阻塞的代碼,除非鏈路斷了,不然是不會終止的,咱們能夠在handler中手動關閉,達到關閉客戶端的效果spa
package org.zln.netty.five.part05.client; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.zln.netty.five.part05.dto.SubscribeReq; import org.zln.netty.five.part05.dto.SubscribeResp; /** * Created by sherry on 16/11/5. */ public class SubReqClientHandler extends ChannelHandlerAdapter { /** * 日誌 */ private Logger logger = LoggerFactory.getLogger(SubReqClientHandler.class); private static int count = 0; 。。。。。。 @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { SubscribeResp subscribeResp = (SubscribeResp) msg; logger.debug("這是收到的第 " + (++count) + " 筆響應 -- " + subscribeResp); //關閉鏈路 ctx.close(); } 。。。。。。 }