当我们用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. PHPAdmin的安装和配置

    phpadmin是用于管理mysql数据库的一个产品,,毕竟很多数据库服务器不能够公开连接,所以只能够使用http的方式来进行连接管理.     下载phpadmin( http://xj-http. ...

  2. 开发中Dialog多弹窗管理

    随着项目的不断迭代,加上产品经理大法(这里加一个弹窗提示,这里加一个引导….)各种弹窗在应用启动时候需要展示, 然而它们出现的时机还有可能重叠.我勒个擦...有没有一种优(tou)雅(lan)的方式来 ...

  3. spring3: 对JDBC的支持 之 关系数据库操作对象化

    7.3.1  概述 所谓关系数据库对象化其实就是用面向对象方式表示关系数据库操作,从而可以复用. Spring JDBC框架将数据库操作封装为一个RdbmsOperation,该对象是线程安全的.可复 ...

  4. spring3: Aspectj后置返回通知

    Aspectj后置返回通知 接口: package chapter1.server; public interface IHelloService2 { public int sayAfterRetu ...

  5. mysql:字符分割,将字符分割成数组

    1.分割函数:SUBSTRING_INDEX('浙江温州-中国电信','-','1') 2.用例(筛选'-'前至少4个汉字的数据) a.数据分布 b.筛选sql   select t.mobile_n ...

  6. js的onclick字符串参数的解决办法

    <a href='#' onclick='onedit(\""+ name + "\")';>编辑</a>" 一些写法实例~~ ...

  7. jenkins的Master/Slave模式

    一. Master/Slave模式 分担jenkins服务器的压力,任务分配到其它执行机来执行 Master:Jenkins服务器 Slave:执行机(奴隶机).执行Master分配的任务,并返回任务 ...

  8. windows钩​子​

    (转自:http://wenku.baidu.com/view/5d41fdbec77da26925c5b08d.html) Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消 ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. .net 学习路线感想(转)

    从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...