Java Code Examples for io.netty.bootstrap.Bootstrap#attr()

The following examples show how to use io.netty.bootstrap.Bootstrap#attr() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyClient.java    From nuls-v2 with MIT License 6 votes vote down vote up
public NettyClient(Node node) {
    this.node = node;
    boot = new Bootstrap();

    AttributeKey<Node> key = null;
    synchronized (NettyClient.class) {
        if (AttributeKey.exists("node")) {
            key = AttributeKey.valueOf("node");
        } else {
            key = AttributeKey.newInstance("node");
        }
    }
    boot.attr(key, node);
    boot.group(worker)
            .channel(NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.SO_SNDBUF, 128 * 1024)
            .option(ChannelOption.SO_RCVBUF, 128 * 1024)
            .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNETCI_TIME_OUT)
            .handler(new NulsChannelInitializer<>(new ClientChannelHandler()));
}
 
Example 2
Source File: NettyClient.java    From nuls with MIT License 6 votes vote down vote up
public NettyClient(Node node) {
        this.node = node;
        boot = new Bootstrap();

        boot.attr(NodeAttributeKey.NODE_KEY, node);
        boot.group(worker)
                .channel(NioSocketChannel.class)
//                .option(ChannelOption.SO_BACKLOG, 1024)
                .option(ChannelOption.TCP_NODELAY, true)            //Send messages immediately
                .option(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.SO_SNDBUF, 128 * 1024)
                .option(ChannelOption.SO_RCVBUF, 128 * 1024)
                .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNETCI_TIME_OUT)
                .handler(new NulsChannelInitializer<>(new ClientChannelHandler()));
    }
 
Example 3
Source File: SimpleChannelPool.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
     * Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.尝试从池中检索健康通道(如果有的话)或创建新的通道。
     * @param promise the promise to provide acquire result.
     * @return future for acquiring a channel.
     */
    private Future<Channel> acquireHealthyFromPoolOrNew(final Promise<Channel> promise) {
        try {
//            从deque中获取一个channel,这里是用双端队列存储的channel
            final Channel ch = pollChannel();
            if (ch == null) {
                // No Channel left in the pool bootstrap a new Channel池中没有剩余通道引导新通道
                Bootstrap bs = bootstrap.clone();
                bs.attr(POOL_KEY, this);
//                如果channel不存在就创建一个
                ChannelFuture f = connectChannel(bs);
                if (f.isDone()) {
//                    promise发布连接成功事件
                    notifyConnect(f, promise);
                } else {
                    f.addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            notifyConnect(future, promise);
                        }
                    });
                }
                return promise;
            }
            EventLoop loop = ch.eventLoop();
            if (loop.inEventLoop()) {
                doHealthCheck(ch, promise);
            } else {
                loop.execute(new Runnable() {
                    @Override
                    public void run() {
                        doHealthCheck(ch, promise);
                    }
                });
            }
        } catch (Throwable cause) {
            promise.tryFailure(cause);
        }
        return promise;
    }