当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连。
Netty Client有两种情况下需要重连:

  1. Netty Client启动的时候需要重连
  2. 在程序运行中连接断掉需要重连。

对于第一种情况,Netty的作者在stackoverflow上给出了解决方案
对于第二种情况,Netty的例子uptime中实现了一种解决方案

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

实现ChannelFutureListener 用来启动时监测是否连接成功,不成功的话重试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 负责重连:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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监测连接是否断掉,断掉的话也要重连:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

Netty Client 重连实现的更多相关文章

  1. Netty Client重连实现

    from:http://itindex.net/detail/54161-netty-client 当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连 ...

  2. Netty断线重连

    Netty断线重连 最近使用Netty开发一个中转服务,需要一直保持与Server端的连接,网络中断后需要可以自动重连,查询官网资料,实现方案很简单,核心思想是在channelUnregistered ...

  3. Android Netty Client

    Android netty client Start a netty client on android Download netty Download url :https://netty.io/d ...

  4. Netty 自动重连

    from: http://www.dozer.cc/2015/05/netty-auto-reconnect.html 自动重连 用 Netty 写 Client 和 Server 的时候必须要去处理 ...

  5. Netty 断线重连解决方案

    http://www.spring4all.com/article/889 本篇文章是Netty专题的第七篇,前面六篇文章如下: 高性能NIO框架Netty入门篇 高性能NIO框架Netty-对象传输 ...

  6. Netty Client和Server端实现

    本文基于Nett4.0.26.Final版本浅析Client与Server端通讯,先看服务器端: public class Server { public static void run(int po ...

  7. 最新Java面试题及答案整理

    基础篇 一.基本功 面向对象特征 封装,继承,多态和抽象 1. 封装 封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰 ...

  8. 2018年最新Java面试题及答案整理

    基础篇 基本功 面向对象特征 封装,继承,多态和抽象 封装封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰符: pub ...

  9. 2018年最新Java面试题及答案整理(持续完善中…)

    2018年最新Java面试题及答案整理(持续完善中…) 基础篇 基本功 面向对象特征 封装,继承,多态和抽象 封装封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内 ...

随机推荐

  1. Pandas面板(Panel)

    面板(Panel)是3D容器的数据.面板数据一词来源于计量经济学,部分源于名称:Pandas - pan(el)-da(ta)-s. 3轴(axis)这个名称旨在给出描述涉及面板数据的操作的一些语义. ...

  2. coredata 数据库升级

    在真实开发中,因为需求是不断变化的,说不定什么时候就需要往模型里添加新的字段,添加新的模型,甚至是大规模的重构:所以数据的迁移就显得尤为重要了. CoreData 中,数据迁移本质就是把旧的 SQLi ...

  3. jmeter导入jar包后在beanshell中import失效的问题解决

    最近一直很忙,没有时间来更新了,今天抽空把之前遇到的问题记录下来. 之前在使用jmeter做http请求性能压测时,因为要对所有入参做排序再加密作为一个入参,所以写了一段java代码,用来处理入参,打 ...

  4. layer弹出层 获取index

    function closelayer(){ var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index ...

  5. 图解MySQL 内连接、外连接、左连接、右连接、全连接

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  6. 将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成

    将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成. 代码比较乱,但是方法还是对的,大家又需要的选择看,希望对博友 有帮助,欢迎留言分享! public class s ...

  7. flash播放器插件与flash播放器的区别

    flash插件是一个网页ActiveX控件,而flash播放器是一个exe的可执行程序.前者用于播放网页中的falsh动画,而后者用于播放本地swf格式文件.

  8. switch遇到0的问题

    你是否经常有switch来代替if else?是否因为使用了switch,提高代码的执行效率而庆幸?好吧,你和我一样,但也许你没有遇到下面的问题. 这个小程序,会输出什么呢?会是'00'么?   结果 ...

  9. Arduino UNO的原理图

    Arduino UNO的原理图是开源的,所以可以从arduino网站上下载它: https://www.arduino.cc/en/Main/ArduinoBoardUno 原理图PDF: https ...

  10. 题目一:给出一个n,代表有从1到n的数字[1,2,3,··· n],问可以构成多少种二叉搜索树?

    题目一:给出一个n,代表有从1到n的数字[1,2,3,··· n],问可以构成多少种二叉搜索树? 一开始的想法是直接递归构造,时间复杂度是指数上升:后来想法是找规律:先看例子: n = 1, 有一个元 ...