netty5客户端监测服务端断连后重连
服务端挂了或者主动拒绝客户端的连接后,客户端不死心,每15秒重连试试,3次都不行就算了。修改下之前的客户端引导类(NettyClient,参见netty5心跳与业务消息分发实例),新增两个成员变量,在connect连接方法里的finally加入重连操作:
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
private AtomicInteger reconnetTimes = new AtomicInteger(0); public void connect(int port, String host) throws Exception {
NioEventLoopGroup workGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new NettyMessageDecoder());
channel.pipeline().addLast(new NettyMessageEncoder());
channel.pipeline().addLast(new ControlClientHandler());
channel.pipeline().addLast(new HeartBeatClientHandler());
// channel.pipeline().addLast(new NettyClientHandler());
}
});
ChannelFuture future = bootstrap.connect(host, port).sync();
future.channel().closeFuture().sync();
} finally { group.shutdownGracefully(); // 所有资源释放完成之后,清空资源,再次发起重连操作
executorService.execute(new Runnable() {
@Override
public void run() { // 重连3次均失败,关闭线程池
if (reconnetTimes.get() > 3) {
log.error("reconnect times > 3.");
executorService.shutdown();
} // 每15秒重连一次
try {
TimeUnit.SECONDS.sleep(15);
try {
// 发起重连操作,连接成功后将阻塞
connect(port, "127.0.0.1");
} catch (Exception e) {
reconnetTimes.getAndIncrement();
log.error("client try to reconnect failed, error : {}", e.getMessage());
}
} catch (InterruptedException e) {
reconnetTimes.incrementAndGet();
log.error("client try to reconnect failed, error : {}", e.getMessage());
} }
});
}
}
不起服务端,我们只起客户端,输出如下:
Exception in thread "main" java.net.ConnectException: Connection refused: no further information: /127.0.0.1:9911
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:223)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:276)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:531)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
17:02:02.968 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Connection refused: no further information: /127.0.0.1:9911
17:02:19.013 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Connection refused: no further information: /127.0.0.1:9911
17:02:35.063 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Connection refused: no further information: /127.0.0.1:9911
17:02:51.112 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Connection refused: no further information: /127.0.0.1:9911
17:02:51.112 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - reconnect times > 3.
17:03:07.140 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@16c59706 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@6fa6a4f0[Shutting down, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 4] Process finished with exit code 1
如果3次重连过程中你把服务端起了,那么客户端就会连上去:
Exception in thread "main" java.net.ConnectException: Connection refused: no further information: /127.0.0.1:9911
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:223)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:276)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:531)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
17:06:40.171 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Connection refused: no further information: /127.0.0.1:9911
17:06:56.219 [pool-1-thread-1] ERROR com.wlf.netty.nettyclient.client.NettyClient - client try to reconnect failed, error : Connection refused: no further information: /127.0.0.1:9911
17:07:11.297 [nioEventLoopGroup-4-0] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetectionLevel: simple
17:07:11.304 [nioEventLoopGroup-4-0] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacity: 262144
17:07:11.366 [nioEventLoopGroup-4-0] INFO com.wlf.netty.nettyclient.handler.HeartBeatClientHandler - [client] control response is OK, header : Header{delimiter=-1410399999, length=8, type=0, reserved=0}. sid : 63, interval : 5000
17:07:16.412 [nioEventLoopGroup-4-0] INFO com.wlf.netty.nettyclient.handler.HeartBeatClientHandler - [client] Client send heart beat message to server : ----> NettyMessage{header=Header{delimiter=-1410399999, length=4, type=3, reserved=0}, data=[B@682470ef}
17:07:16.412 [nioEventLoopGroup-4-0] INFO com.wlf.netty.nettyclient.handler.HeartBeatClientHandler - [client] Client send business message to server : ----> NettyMessage{header=Header{delimiter=-1410399999, length=161800, type=1, reserved=0}, data=[B@41bd2db4}
netty5客户端监测服务端断连后重连的更多相关文章
- Android-socket服务端断重启后,android客户端自动重连
今天研究这个问题搞了整整一天啊!终于出来了,不过我没有多大的成就感,为什么呢?因为这不是我的劳动成果.同样的问题,我却没想出来!心塞的很啊…… 不过还是要给大家分享一下,希望给大家带来帮助! 先声明一 ...
- Consul的一个更新:服务端节点故障后重连
研究了一段时间Consul,想写个攻略来着,但太赖了而且表达能力非正常人...今天发现HashiCorp果然接纳大众意见改了点东西.. 场景是: 假如Consul集群内有三个Server Node 时 ...
- .net remoting 客户端与服务端绑定事件,一部电脑当服务器,另一部当客户端,发布后没法接收远程错误信息。
可以是用下面代码抛出远程错误,客户端和服务端都要设置,因为服务端事件回调时角色变成了远程客户端了. RemotingConfiguration.CustomErrorsMode = CustomErr ...
- FastSocket学习笔记~再说客户端与服务端的组成
废话多说 很久之前,我写过几篇FastSocket的文章,基本属于使用的方法,而缺乏对概念的总结讲解,而本讲就是弥补一下上几讲的不足,将核心的模块再说说,再谈谈,再聊聊! 首先FastSocket由C ...
- SignalR 实现web浏览器客户端与服务端的推送功能
SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话. 换句话说,该对话可不受限制地进行单个无状态请求/响应数据交换:它将继 ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(3) 前后台打通
4.2.1.2.4 PipelineDraweeControllerBuilder.obtainController()源码分析 续 上节中我们提到两个核心的步骤 obtainDataSourceSu ...
- Asp.Net MVC 模型验证详解-实现客户端、服务端双重验证
概要 在asp.net webform开发中经常会对用户提交输入的信息进行校验,一般为了安全起见大家都会在客户端进行Javascript(利于交互).服务端双重校验(安全).书写校验代码是一个繁琐的过 ...
- Java实现UDP之Echo客户端和服务端
Java实现UDP之Echo客户端和服务端 代码内容 采用UDP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...
- Java实现TCP之Echo客户端和服务端
Java实现TCP之Echo客户端和服务端 代码内容 采用TCP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...
随机推荐
- markdown锚点
转:https://blog.csdn.net/u012260238/article/details/87815170 markdown 语法文档:https://www.w3cschool.cn/l ...
- 题解 洛谷P5380 【[THUPC2019]鸭棋】
就是一道大模拟. 首先,来解释一下复杂的题意: 给你一些棋子,每个棋都有不同的走法,开局是回归原位. 接下来,题目会给你一个虚拟的走子操作(注意不一定真实),你所需要判断当前操作是否正确.若不正确,输 ...
- oracle+mybatis报错:BindingException("Invalid bound statement (not found): ")
oracle+mybatis报错:BindingException("Invalid bound statement (not found): ") 从mysql转到oracle数 ...
- 在golang中使用json
jsoniter高性能json库 非常快,支持java和go marshal使用的一些坑 package main import ( "encoding/json" "f ...
- Codeforces Global Round 6[A,B,C]
题意:给一个字符串,对它重新排列使得它是60的倍数. 模拟一下,需要能整除60 字符串中需要 能整除2 3 10,所以需要字符串各位数之和能整除3 并且有 一个偶数和一个0 或者两个0也行[没考虑 ...
- PHP流程控制之do...while循环的区别
do...while与while的语法结构基本一样,也是一个布尔型循环,功能也基本一样.大理石平台价格 基本语法规定如下: do { //代码块 } while (判断); do...while ...
- Adobe Acrobat DC
DC: document cloud [云服务] 但是Adobe document cloud包括: Acrobat DC, Adobe sign, 以及Web和移动应用程序. 参考: https:/ ...
- Pytest权威教程06-使用Marks标记测试用例
目录 使用Marks标记测试用例 在未知标记上引发异常: -strict 标记改造和迭代 返回: Pytest权威教程 使用Marks标记测试用例 通过使用pytest.mark你可以轻松地在测试用例 ...
- Hadoop hadoop 之hdfs数据块修复方法
hadoop 之hdfs数据块修复方法: .手动修复 hdfs fsck / #检查集群的健康状态 hdfs debug recoverLease -path 文件位置 -retries 重试次数 # ...
- 浅谈Python-IO多路复用(select、poll、epoll模式)
1. 什么是IO多路复用 在传统socket通信中,存在两种基本的模式, 第一种是同步阻塞IO,其线程在遇到IO操作时会被挂起,直到数据从内核空间复制到用户空间才会停止,因为对CPython来说,很多 ...