欢迎访问昆山宝鼎软件有限公司网站! 设为首页 | 网站地图 | XML | RSS订阅 | 宝鼎邮箱 | 后台管理


新闻资讯

MENU

软件开发知识

对于第二 昆山软件开发 种情况

点击: 次  来源:宝鼎软件 时间:2017-06-12

原文出处: 鸟窝

当我们用Netty实现一个TCP client时,图纸加密,我们虽然但愿当毗连断掉的时候Netty可以或许自动重连。
Netty Client有两种环境下需要重连:

  1. Netty Client启动的时候需要重连
  2. 在措施运行中毗连断掉需要重连。

对付第一种环境,Netty的作者在stackoverflow上给出了办理方案,软件开发,
对付第二种环境,Netty的例子uptime中实现了一种办理方案。

而Thomas在他的文章中提供了这两种方法的实现的例子。

实现ChannelFutureListener 用来启动时监测是否毗连乐成,软件开发,不乐成的话重试:

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 认真重连:

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监测毗连是否断掉,断掉的话也要重连:

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