Netty Client 重连实现
当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连。
Netty Client有两种情况下需要重连:
- Netty Client启动的时候需要重连
- 在程序运行中连接断掉需要重连。
对于第一种情况,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 重连实现的更多相关文章
- Netty Client重连实现
from:http://itindex.net/detail/54161-netty-client 当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连 ...
- Netty断线重连
Netty断线重连 最近使用Netty开发一个中转服务,需要一直保持与Server端的连接,网络中断后需要可以自动重连,查询官网资料,实现方案很简单,核心思想是在channelUnregistered ...
- Android Netty Client
Android netty client Start a netty client on android Download netty Download url :https://netty.io/d ...
- Netty 自动重连
from: http://www.dozer.cc/2015/05/netty-auto-reconnect.html 自动重连 用 Netty 写 Client 和 Server 的时候必须要去处理 ...
- Netty 断线重连解决方案
http://www.spring4all.com/article/889 本篇文章是Netty专题的第七篇,前面六篇文章如下: 高性能NIO框架Netty入门篇 高性能NIO框架Netty-对象传输 ...
- Netty Client和Server端实现
本文基于Nett4.0.26.Final版本浅析Client与Server端通讯,先看服务器端: public class Server { public static void run(int po ...
- 最新Java面试题及答案整理
基础篇 一.基本功 面向对象特征 封装,继承,多态和抽象 1. 封装 封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰 ...
- 2018年最新Java面试题及答案整理
基础篇 基本功 面向对象特征 封装,继承,多态和抽象 封装封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰符: pub ...
- 2018年最新Java面试题及答案整理(持续完善中…)
2018年最新Java面试题及答案整理(持续完善中…) 基础篇 基本功 面向对象特征 封装,继承,多态和抽象 封装封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内 ...
随机推荐
- 用Java编程计算出所有的"水仙花数"
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个 "水仙花数 ",因 ...
- hash路由(哈希路由)
1.https://www.cnblogs.com/huanying2015/p/8047376.html (js 哈希路由原理实现) 2.https://www.cnblogs.com/yeer/a ...
- 【zzulioj-1676】与同学比身高(综合)
题目链接: http://acm.zzuli.edu.cn/problem.php?id=1676 题目描述 新学年开学了,学校又迎来了一批新同学,已知部分同学之间的身高关系,请列出可推断出的同学之间 ...
- //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- 【python】windows下安装xgboost的python库
傻瓜教程 主要参考了https://www.hongweipeng.com/index.php/archives/826/ 和 https://github.com/dmlc/xgboost/iss ...
- MySqlDBHelper数据库连接
这里是本人在工作中用到,希望给大家帮助 public class MySqlDBHelper { //获取一个记录器 private static readonly log4net.ILog log ...
- Markdown速成班
更多内容请参考: http://ibruce.info/2013/11/26/markdown/
- C / C ++中的数组讲解
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- docker中宿主机与容器(container)互相拷贝传递文件的方法
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/71425077 本文出自[我是干勾鱼的博客] 前面讲解过如何进入.退出docker ...
- 机器学习算法实现解析——libFM之libFM的训练过程概述
本节主要介绍的是libFM源码分析的第四部分--libFM的训练. FM模型的训练是FM模型的核心的部分. 4.1.libFM中训练过程的实现 在FM模型的训练过程中,libFM源码中共提供了四种训练 ...