您的位置:首页 > 其它

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 连接
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
相关文章推荐