四netty學習之重連, ReaderTimeoutHandler, IdleStateHander

關於重連

何時須要重連呢? 1. 啓動的時候,沒有成功鏈接 2. 運行過程當中,鏈接斷掉bootstrap

對第一種狀況的解決方法: 實現ChannelFutureListener用來監測是否鏈接成功,不成功的話重試ide

public class ReConnectionListener implements ChannelFutureListener {
    private Client client;
    private int port;
    private String host;
    public ReConnectionListener(){
        this.client = client;
    }

    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
        if(!future.isSuccess()){
            final EventLoop loop = future.channel().eventLoop();
            loop.schedule(new Runnable() {
                @Override
                public void run() {
                    try {
                        client.run(port, host); // 這裏大意就是從新調用那個鏈接的過程
                    } catch (Exception e) {
                    }
                }
            }, 1, TimeUnit.SECONDS);

        }
    }

對第二種狀況的解決辦法是: 在一個handler裏,oop

public class ReConnectionHandler extends ChannelHandlerAdapter{
    private Client client;
    public ReConnectionHandler(Client client){
        this.client = client;
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        final EventLoop eventLoop = ctx.channel().eventLoop();
        eventLoop.schedule(new Runnable() {
            @Override
            public void run() {
                try {
                    client.run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 1, TimeUnit.SECONDS);
        super.channelInactive(ctx);
    }

}

提供的Handler.

ReadTimeoutHandlerthis

WriteTimeoutHandlercode

IdleStateHandler( int readerIdleTimeSeconds,int writerIdleTimeSeconds, int allIdleTimeSeconds)事件

ReadTimeoutHandler 和 WriteTimeoutHandler 是讀寫超時Handler,只有一個參數的構造方法能夠穿進去一個秒數,意味着,若是讀寫事件超過 指定的秒數,就會拋出異常.傳到下一個handler的exceptionCaught方法裏ip

IdleStateHandler 是什麼意思呢 是空閒事件Handler 能夠指定一個寫空閒,讀空閒, 若是超過指定的時間沒有讀寫就會拋出異常 用這個Handler能夠實現心跳檢測,it

/ An example that sends a ping message when there is no outbound traffic
   // for 30 seconds.  The connection is closed when there is no inbound traffic
   // for 60 seconds.
  
   public class MyChannelInitializer extends ChannelInitializer<Channel> {
        @Override
       public void initChannel(Channel channel) {
           channel.pipeline().addLast("idleStateHandler", new IdleStateHandler(60, 30, 0));
           channel.pipeline().addLast("myHandler", new MyHandler());
       }
   }
  
   // Handler should handle the IdleStateEvent triggered by IdleStateHandler.
   public class MyHandler extends ChannelHandlerAdapter {
        @Override
       public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
           if (evt instanceof IdleStateEvent) {
               IdleStateEvent e = (IdleStateEvent) evt;
               if (e.state() == IdleState.READER_IDLE) {
                   ctx.close();
               } else if (e.state() == IdleState.WRITER_IDLE) {
                   ctx.writeAndFlush(new PingMessage());
               }
           }
       }
   }
  
   ServerBootstrap bootstrap = ...;
   ...
   bootstrap.childHandler(new MyChannelInitializer());
相關文章
相關標籤/搜索