Netty Client 重連實現

當咱們用Netty實現一個TCP client時,咱們固然但願當鏈接斷掉的時候Netty可以自動重連。
Netty Client有兩種狀況下須要重連:html

  1. Netty Client啓動的時候須要重連
  2. 在程序運行中鏈接斷掉須要重連。

對於第一種狀況,Netty的做者在stackoverflow上給出了解決方案
對於第二種狀況,Netty的例子uptime中實現了一種解決方案java

而Thomas在他的文章中提供了這兩種方式的實現的例子。git

實現ChannelFutureListener 用來啓動時監測是否鏈接成功,不成功的話重試:github

public class Client 

 { 

   private EventLoopGroup loop = new NioEventLoopGroup(); 

   public static void main( String[] args ) 

   { 

     new Client().run(); 

   } 

   public Bootstrap createBootstrap(Bootstrap bootstrap, EventLoopGroup eventLoop) {  

     if (bootstrap != null) { 

       final MyInboundHandler handler = new MyInboundHandler(this); 

       bootstrap.group(eventLoop); 

       bootstrap.channel(NioSocketChannel.class); 

       bootstrap.option(ChannelOption.SO_KEEPALIVE, true); 

       bootstrap.handler(new ChannelInitializer<SocketChannel>() { 

         @Override 

         protected void initChannel(SocketChannel socketChannel) throws Exception {  

           socketChannel.pipeline().addLast(handler); 

         } 

       }); 

       bootstrap.remoteAddress("localhost", 8888);

       bootstrap.connect().addListener(new ConnectionListener(this));

     } 

     return bootstrap; 

   } 

   public void run() { 

     createBootstrap(new Bootstrap(), loop);

   } 

 }

ConnectionListener 負責重連:bootstrap

public class ConnectionListener implements ChannelFutureListener { 

  private Client client; 

  public ConnectionListener(Client client) { 

    this.client = client; 

  } 

  @Override 

  public void operationComplete(ChannelFuture channelFuture) throws Exception {  

    if (!channelFuture.isSuccess()) { 

      System.out.println("Reconnect"); 

      final EventLoop loop = channelFuture.channel().eventLoop(); 

      loop.schedule(new Runnable() { 

        @Override 

        public void run() { 

          client.createBootstrap(new Bootstrap(), loop); 

        } 

      }, 1L, TimeUnit.SECONDS); 

    } 

  } 

}

一樣在ChannelHandler監測鏈接是否斷掉,斷掉的話也要重連:socket

public class MyInboundHandler extends SimpleChannelInboundHandler { 

   private Client client; 

   public MyInboundHandler(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() { 

         client.createBootstrap(new Bootstrap(), eventLoop); 

       } 

     }, 1L, TimeUnit.SECONDS); 

     super.channelInactive(ctx); 

   } 

 }

http://stackoverflow.com/questions/19739054/whats-the-best-way-to-reconnect-after-connection-closed-in-netty參考文檔

  • https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java
  • http://tterm.blogspot.jp/2014/03/netty-tcp-client-with-reconnect-handling.html
  • ctx.close vs ctx.channel().close
  • ctx.write vs ctx.channel().write
相關文章
相關標籤/搜索