Netty 快速入门系列 - Chapter 3 Netty5.x【第九讲】 - 单客户多Client 重连
2018-03-25 19:32
459 查看
MultipleNetty5Client :学习[b]getFirstChannel 通过递归,获取一个可用Channel[/b]
public Channel nextChannel() throws InterruptedException {
return getFirstChannel(0);
}
private Channel getFirstChannel(int tryCount) throws InterruptedException;
private List<Channel> channels; 保存单个客户,多个Client 连接
MultClientHandler package com.john.netty.learn.ch05;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class MultClientHandler extends SimpleChannelInboundHandler<String> {
public MultClientHandler() {
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("Client Read message " + msg);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive(ChannelHandlerContext " + ctx + ")");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelInactive(ChannelHandlerContext " + ctx + ")");
}
}所有源码下载 :https://download.csdn.net/download/netcobol/10308871
public Channel nextChannel() throws InterruptedException {
return getFirstChannel(0);
}
private Channel getFirstChannel(int tryCount) throws InterruptedException;
private List<Channel> channels; 保存单个客户,多个Client 连接
package com.john.netty.learn.ch05; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoop; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; public class MultipleNetty5Client { private String ip; private int port; private int count; private Bootstrap bootstrap; private List<Channel> channels; private EventLoopGroup workers = new NioEventLoopGroup(); private AtomicLong index = new AtomicLong(); public MultipleNetty5Client(String ip, int port, int count) { this.ip = ip; this.port = port; this.count = count; channels = new ArrayList<>(count); } public void connect() { this.bootstrap = new Bootstrap(); this.bootstrap.group(this.workers); bootstrap.channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new MultClientHandler()); } }); for (int i = 0; i < this.count; i++) { channels.add(bootstrap.connect(this.ip, this.port).channel()); } } public Channel nextChannel() throws InterruptedException { return getFirstChannel(0); } private Channel getFirstChannel(int tryCount) throws InterruptedException { Channel channel = channels.get((int) Math.abs(index.incrementAndGet() % channels.size())); if (!channel.isActive()) { reconnect(channel); // 重连 if (tryCount >= channels.size()) { throw new IllegalAccessError("No Available Connection Channel!"); } return getFirstChannel(++tryCount); } return channel; } private void reconnect(Channel channel) throws InterruptedException { synchronized (channel) { if (channel.isActive()) { return; } int indexOf = this.channels.indexOf(channel); if (indexOf == -1) { return; } channels.set(indexOf, bootstrap.connect(this.ip, this.port).sync().channel()); } } public void console() throws Exception { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, "GBK")); while (true) { try { System.out.println("请输入:"); String line = bufferedReader.readLine(); this.send(line); if ("quit".equalsIgnoreCase(line)) { break; } } catch (Throwable e) { } } } private void send(String message) throws InterruptedException { this.nextChannel().writeAndFlush(message); } public static void main(String[] args) throws Exception { MultipleNetty5Client multipleNetty5Client = new MultipleNetty5Client("127.0.0.1", 23, 3); multipleNetty5Client.connect(); multipleNetty5Client.console(); } }
MultClientHandler package com.john.netty.learn.ch05;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class MultClientHandler extends SimpleChannelInboundHandler<String> {
public MultClientHandler() {
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("Client Read message " + msg);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive(ChannelHandlerContext " + ctx + ")");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelInactive(ChannelHandlerContext " + ctx + ")");
}
}所有源码下载 :https://download.csdn.net/download/netcobol/10308871
相关文章推荐
- Netty 快速入门系列 - Chapter 3 Netty5.x【第八讲】 - Client 重连
- Netty 快速入门系列 - Chapter 3 Netty5.x【第六讲】 -Netty5 案例
- Netty 快速入门系列 - Chapter 3 Netty5.x【第七讲】 - Channel线程安全?
- Netty 快速入门系列 - Chapter 2 Netty3.x 【第五讲】 - 源码讲解
- Netty 快速入门系列 - Chapter 8 数据包协议【第二十二讲】Socket攻击及防护
- Netty 快速入门系列 - Chapter 1 传统OIO与NIO - NIO 【第二讲】
- Netty 快速入门系列 - Chapter 6 自定义数据协议【第十三讲】 通过大端序列方法将4个字节int转成 byte数组
- Netty 快速入门系列 - Chapter 8 数据包协议【第二十讲】解决方案-Pipeline用例代码
- Netty 快速入门系列 - Chapter 6 自定义数据协议【第十四讲】ByteBuffer 和 ChannelBuffers
- Netty 快速入门系列 - Chapter 7 数据包协议【第十八讲】解决方案-经典协议包结构
- Netty 快速入门系列 - Chapter 6 自定义数据协议【第十五讲】自定义方法Serializer
- Netty 快速入门系列 - Chapter 4 Netty心跳【第十讲】 - IdleStateHandler学习
- Netty 快速入门系列 - Chapter 7 数据包协议【第十九讲】解决方案-粘包分包原理
- Netty 快速入门系列 - Chapter 8 数据包协议【第二十一讲】FrameDecoder 讲解
- Netty 快速入门系列 - Chapter 7 数据包协议【第十六讲】数据传输问题
- Netty 快速入门系列 - Chapter 5 Netty之序列化【第十一讲】 Protocol buff
- Netty 快速入门系列 - Chapter 7 数据包协议【第十七讲】解决方案- 分割符 及 长度+数据
- Netty 快速入门系列 - Chapter 1 传统OIO与NIO - 传统OIO 【第一讲】
- Netty 快速入门系列 - Chapter 2 Netty3.x 【第四讲】 - 基本原理
- Netty 快速入门系列 - Chapter 5 Netty之序列化【第十二讲】 Java Serializable