Netty4:向不同的服务器发送多个请求

时间:2017-07-14 17:39:05

标签: java sockets netty

我有一个发送多个请求的客户端。每个请求都会转到不同的服务器。因此,200个请求发送到200个不同的服务器 我创建了一个带有不同bootstrap的偶数循环组,用于不同的连接 我应该使用200个频道来处理200个请求还是单个频道。下面是我的代码,现在我正在使用单一频道:

public HttpClientDemo(int serverPort)
    {
        this.serverPort = serverPort;
        this.pipelineFactory = new HTTPClientInitializer();
        this.workerGroup =  new NioEventLoopGroup();
    }

    public void connect(String address, int timeout) {

            connectAsync(address).syncUninterruptibly();
    }   

    private ChannelFuture connectAsync(final String address)
    {
        return new Bootstrap()
                .group(workerGroup)
                .channel(NioSocketChannel.class)
                .handler(pipelineFactory).connect(address,serverPort).addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) {
                        if(future.isSuccess())
                        {
                            log.info("Client is able to connect to: " + address);
                        }
                        else
                        {
                            log.error("Client not able to connect to: " + address + " " +  future.cause());
                        }
                    }
                });
    }

1 个答案:

答案 0 :(得分:0)

每个频道都连接到不同的端点和不同的服务器,这意味着您将需要不同的频道。

相关问题