Netty3 - 多连接的客户端示例

Netty 4/5 说明: 目前  http://netty.io/ 发布的最新版本号4.1.30.Final,但是并没有netty5相关的版本发布了

Netty3 模拟多连接的客户端

package xss.netty.netty3.client;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import xss.netty.netty3.HelloClientHandler;

import java.net.InetSocketAddress;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *  多个连接的客户端
 */
public class ClientMutliChannel {
    //客户端连接服务类
    ClientBootstrap clientBootstrap=new ClientBootstrap();

    //会话列表
//    List<Channel> channelList=new ArrayList<Channel>();
    Vector<Channel> channels=new Vector<Channel>();


    //计数器
    private final AtomicInteger index =new AtomicInteger();

    /**
     * 初始化channel 的个数
     * @param channelCount
     */
    public void init(int channelCount){
        ExecutorService worker = Executors.newCachedThreadPool();
        ExecutorService boss = Executors.newCachedThreadPool();
        //设置Thread Factory
        clientBootstrap.setFactory(new NioClientSocketChannelFactory(boss,worker));
        //set pipe line factory
        clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline channelPipeline= Channels.pipeline();
                channelPipeline.addLast("decoder",new StringDecoder());
                channelPipeline.addLast("encoder",new StringEncoder());
                channelPipeline.addLast("hello",new HelloClientHandler());
                return channelPipeline;
            }
        });
        //connet to server
        for(int count=1;count<channelCount;count++) {
            ChannelFuture channelFuture=clientBootstrap.connect(new InetSocketAddress("127.0.0.1",9999));
            channels.add(channelFuture.getChannel());
        }
        System.out.println("Init 完成");
    }

    public Channel getChannel(){
        return getActiveChannel(0);
    }

    /**
     * 返回可用的channel
     * @param count
     * @return
     */
    private Channel getActiveChannel(int count){
        Channel channel=channels.get(Math.abs(index.getAndIncrement() % channels.size()));
        if(!channel.isConnected()){
            reconnect(channel);
            if(count >= channels.size()){// 超出连接连接数的大小
                throw new RuntimeException("没有可用的连接");
            }
            return getActiveChannel(count+1);//尝试下一个
        }
        return channel;
    }

    /**
     * 重连
     * @param channel
     */
    private void reconnect(Channel channel){
        if(channels.indexOf(channel) == -1){
            return;
        }
        Channel reconnetChannel=clientBootstrap.connect(new InetSocketAddress("127.0.0.1",9999)).getChannel();
        channels.set(channels.indexOf(channel),reconnetChannel);
    }

}

相关问题: 是不是线程安全的? 粘包/拆包的问题?

后续验证粘包与拆包的问题

猜你喜欢

转载自blog.csdn.net/seanme/article/details/83239866
今日推荐