Netty 10000连接同时进行

时间:2015-08-11 13:05:55

标签: java netty

我正在尝试使用Netty同时模拟10000个客户端连接到服务器。当956个客户端连接到服务器时,一切都很好,但957客户端会导致错误异常。

注意:我在同一台机器上运行服务器和客户端(win7 8GB ram,i7-CPU)

错误:

java.lang.IllegalStateException: failed to create a child event loop
io.netty.channel.ChannelException: failed to open a new selector

我的代码:

try {
        con.connect();
    } catch (Exception e) {
        logger.error("Client: error connect to ip {} and port {}, ",id, ip, port,e);
        return;
    }

connect方法的代码是:

public void connect() {
    workerGroup = new NioEventLoopGroup();
    Bootstrap bs = new Bootstrap();
    bs.group(workerGroup).channel(NioSocketChannel.class);
    bs.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 300));
            ch.pipeline().addLast("idleStateActionHandler", new IdleStateEventHandler());
            ch.pipeline().addLast("logger", new LoggingHandler());
            ch.pipeline().addLast("commandDecoder", new CommandDecoder());
            ch.pipeline().addLast("commandEncoder", new CommandEncoder());
        }
    });

1 个答案:

答案 0 :(得分:6)

您应该为每个连接调用使用相同的NioEventLoopGroup实例。否则你会创建很多线程。