一起学Netty(十三)之 Netty简单的重连机制
来源:互联网 发布:vue.js select 选中 编辑:程序博客网 时间:2024/05/11 20:05
其实重连机制并不是多么多高深的技术,其实就是一个在客户端做一个简单的判断,如果连接断了,那么就重新调用连接服务端的代码
当然,我们重连的动作肯定是发生在断连之后发生的,我们可以在上篇的心跳机制的基础上,简单地修改一下客户端的启动代码就可以了:
我们在连接断了之后,我们一般会在finally的方法中去释放资源,这边我们应该不去释放资源,我们在finally里面进行重连:
整个客户端的代码如下:
package com.lyncc.netty.heartbeats;import java.util.concurrent.TimeUnit;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelPipeline;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;import io.netty.handler.timeout.IdleStateHandler;public class HeartBeatsClient { public void connect(int port, String host) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); ChannelFuture future = null; try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("ping", new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS)); p.addLast("decoder", new StringDecoder()); p.addLast("encoder", new StringEncoder()); p.addLast(new HeartBeatClientHandler()); } }); future = b.connect(host, port).sync(); future.channel().closeFuture().sync(); } finally {// group.shutdownGracefully(); if (null != future) { if (future.channel() != null && future.channel().isOpen()) { future.channel().close(); } } System.out.println("准备重连"); connect(port, host); System.out.println("重连成功"); } } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException e) { // 采用默认值 } } new HeartBeatsClient().connect(port, "127.0.0.1"); }}我们再看看服务器端和客户端启动之后的控制台打印信息:
服务器控制台:
客户端:
好了,这样就可以重连~这只是一个简单的Demo,真实的生产场景用法可能并不是这样的
3 0
- 一起学Netty(十三)之 Netty简单的重连机制
- 一起学Netty(十三)之 Netty简单的重连机制
- 一起学Netty(十三)之 Netty简单的重连机制
- 一起学Netty(十四)之 Netty生产级的心跳和重连机制
- 一起学Netty(十四)之 Netty生产级的心跳和重连机制
- 一起学Netty(十四)之 Netty生产级的心跳和重连机制
- 一起学Netty(十四)之 Netty生产级的心跳和重连机制
- Netty 之 Netty简单的重连机制
- 一起学Netty(十二)之 Netty心跳简单Demo
- 一起学Netty(十二)之 Netty心跳简单Demo
- 一起学Netty(十二)之 Netty心跳简单Demo
- Netty 之 Netty生产级的心跳和重连机制
- 一起学Netty(十一)之 Netty心跳之IdleStateHandler
- 一起学Netty(十一)之 Netty心跳之IdleStateHandler
- 一起学Netty(十一)之 Netty心跳之IdleStateHandler
- 一起学Netty(一)之 Hello Netty
- 一起学Netty(一)之 Hello Netty
- 一起学Netty(三)之 SimpleChannelInboundHandler
- listview 中item 点击无效
- DP————1003
- Android学习笔记(一)
- 1.C++学习笔记:内存模型
- Jquery ajax不执行success的原因
- 一起学Netty(十三)之 Netty简单的重连机制
- cf653d Delivery Bears【二分+网络流】
- java中的PO/VO/DAO/BO/POJO简单介绍
- 【知识积累】Oracle数据库安装配置
- 2016百度之星热身赛 1004
- hdu5902 GCD is Funny(错) && hdu5904 LCIS(最长公共连续上升子序列)
- Android studio 获取raw 的文件路径,并转为URI
- 把本地的int类型的图片转换成drawable、Bitmap
- Java千百问_06数据结构(014)_java数组如何存储在内存中