Example usage for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS

List of usage examples for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS.

Prototype

ChannelOption CONNECT_TIMEOUT_MILLIS

To view the source code for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS.

Click Source Link

Usage

From source file:com.mpush.netty.client.NettyTCPClient.java

License:Apache License

protected void initOptions(Bootstrap b) {
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000);
    b.option(ChannelOption.TCP_NODELAY, true);
}

From source file:com.mpush.netty.http.NettyHttpClient.java

License:Apache License

@Override
protected void doStart(Listener listener) throws Throwable {
    workerGroup = new NioEventLoopGroup(http_work, new DefaultThreadFactory(ThreadNames.T_HTTP_CLIENT));
    b = new Bootstrap();
    b.group(workerGroup);//  w w  w.  ja  v a  2 s  .co  m
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.option(ChannelOption.TCP_NODELAY, true);
    b.option(ChannelOption.SO_REUSEADDR, true);
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("decoder", new HttpResponseDecoder());
            ch.pipeline().addLast("aggregator", new HttpObjectAggregator(maxContentLength));
            ch.pipeline().addLast("encoder", new HttpRequestEncoder());
            ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this));
        }
    });
    timer = new HashedWheelTimer(new NamedThreadFactory(T_HTTP_TIMER), 1, TimeUnit.SECONDS, 64);
    listener.onSuccess();
}

From source file:com.mpush.test.client.ConnClientBoot.java

License:Apache License

@Override
protected void doStart(Listener listener) throws Throwable {
    ServiceDiscoveryFactory.create().syncStart();
    CacheManagerFactory.create().init();

    this.workerGroup = new NioEventLoopGroup();
    this.bootstrap = new Bootstrap();
    bootstrap.group(workerGroup)//
            .option(ChannelOption.TCP_NODELAY, true)//
            .option(ChannelOption.SO_REUSEADDR, true)//
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)//
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60 * 1000)
            .option(ChannelOption.SO_RCVBUF, 5 * 1024 * 1024).channel(NioSocketChannel.class);

    bootstrap.handler(new ChannelInitializer<SocketChannel>() { // (4)
        @Override//from   w w  w .j  a va2  s.c o  m
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("decoder", new PacketDecoder());
            ch.pipeline().addLast("encoder", PacketEncoder.INSTANCE);
            ch.pipeline().addLast("handler", new ConnClientChannelHandler());
        }
    });

    listener.onSuccess();
}

From source file:com.navercorp.nbasearc.gcp.PhysicalConnection.java

License:Apache License

static PhysicalConnection create(String ip, int port, SingleThreadEventLoop eventLoop, Gateway gw,
        int reconnectInterval) {
    final PhysicalConnection pc = new PhysicalConnection(ip, port, eventLoop, gw, reconnectInterval);

    pc.b.group(eventLoop.getEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_LINGER, 0)
            .option(ChannelOption.SO_SNDBUF, SOCKET_BUFFER).option(ChannelOption.SO_RCVBUF, SOCKET_BUFFER)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//from  w  w  w. j  a v  a2s  . c o  m
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(pc.new PhysicalConnectionHandler());
                }
            });

    return pc;
}

From source file:com.navercorp.pinpoint.grpc.client.ChannelFactory.java

License:Apache License

private void setupClientOption(final NettyChannelBuilder channelBuilder) {
    channelBuilder.keepAliveTime(clientOption.getKeepAliveTime(), TimeUnit.MILLISECONDS);
    channelBuilder.keepAliveTimeout(clientOption.getKeepAliveTimeout(), TimeUnit.MILLISECONDS);
    channelBuilder.keepAliveWithoutCalls(clientOption.isKeepAliveWithoutCalls());
    channelBuilder.maxHeaderListSize(clientOption.getMaxHeaderListSize());
    channelBuilder.maxInboundMessageSize(clientOption.getMaxInboundMessageSize());

    // ChannelOption
    channelBuilder.withOption(ChannelOption.TCP_NODELAY, true);
    channelBuilder.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, clientOption.getConnectTimeout());
    final WriteBufferWaterMark writeBufferWaterMark = new WriteBufferWaterMark(
            clientOption.getWriteBufferLowWaterMark(), clientOption.getWriteBufferHighWaterMark());
    channelBuilder.withOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufferWaterMark);
    if (logger.isInfoEnabled()) {
        logger.info("Set clientOption {}. name={}", clientOption, name);
    }/*from ww  w  . ja va 2 s .c o m*/
}

From source file:com.navercorp.pinpoint.grpc.server.ServerFactory.java

License:Apache License

private void setupServerOption(final NettyServerBuilder builder) {
    // TODO @see PinpointServerAcceptor
    builder.withChildOption(ChannelOption.TCP_NODELAY, true);
    builder.withChildOption(ChannelOption.SO_REUSEADDR, true);
    builder.withChildOption(ChannelOption.SO_RCVBUF, this.serverOption.getReceiveBufferSize());
    builder.withChildOption(ChannelOption.SO_BACKLOG, this.serverOption.getBacklogQueueSize());
    builder.withChildOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.serverOption.getConnectTimeout());
    final WriteBufferWaterMark writeBufferWaterMark = new WriteBufferWaterMark(
            this.serverOption.getWriteBufferLowWaterMark(), this.serverOption.getWriteBufferHighWaterMark());
    builder.withChildOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufferWaterMark);

    builder.handshakeTimeout(this.serverOption.getHandshakeTimeout(), TimeUnit.MILLISECONDS);
    builder.flowControlWindow(this.serverOption.getFlowControlWindow());

    builder.maxInboundMessageSize(this.serverOption.getMaxInboundMessageSize());
    builder.maxHeaderListSize(this.serverOption.getMaxHeaderListSize());

    builder.keepAliveTime(this.serverOption.getKeepAliveTime(), TimeUnit.MILLISECONDS);
    builder.keepAliveTimeout(this.serverOption.getKeepAliveTimeout(), TimeUnit.MILLISECONDS);
    builder.permitKeepAliveTime(this.serverOption.getPermitKeepAliveTimeout(), TimeUnit.MILLISECONDS);
    builder.permitKeepAliveWithoutCalls(this.serverOption.isPermitKeepAliveWithoutCalls());

    builder.maxConnectionIdle(this.serverOption.getMaxConnectionIdle(), TimeUnit.MILLISECONDS);
    builder.maxConnectionAge(this.serverOption.getMaxConnectionAge(), TimeUnit.MILLISECONDS);
    builder.maxConnectionAgeGrace(this.serverOption.getMaxConnectionAgeGrace(), TimeUnit.MILLISECONDS);
    builder.maxConcurrentCallsPerConnection(this.serverOption.getMaxConcurrentCallsPerConnection());
    if (logger.isInfoEnabled()) {
        logger.info("Set serverOption {}. name={}, hostname={}, port={}", serverOption, name, hostname, port);
    }/*  w ww  .ja  v a  2s  . co  m*/
}

From source file:com.netflix.atlas.client.RxHttp.java

License:Apache License

/**
 * Execute an HTTP request.//from  w  w w  . ja v  a2  s .c  om
 *
 * @param server Server to send the request to.
 * @param req    Request to execute.
 * @return Observable with the response of the request.
 */
private static Observable<HttpClientResponse<ByteBuf>> executeSingle(Server server,
        HttpClientRequest<ByteBuf> req) {
    HttpClient.HttpClientConfig config = new HttpClient.HttpClientConfig.Builder()
            .readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS).userAgent(USER_AGENT).build();

    HttpClientBuilder<ByteBuf, ByteBuf> builder = RxNetty
            .<ByteBuf, ByteBuf>newHttpClientBuilder(server.host(), server.port())
            .pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpClientConfigurator())
            .config(config).channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MS);

    if (server.isSecure()) {
        builder.withSslEngineFactory(DefaultFactories.trustAll());
    }

    final HttpClient<ByteBuf, ByteBuf> client = builder.build();
    return client.submit(req).doOnNext(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override
        public void call(HttpClientResponse<ByteBuf> res) {
            LOGGER.debug("Got response: {}", res.getStatus().code());
        }
    }).doOnError(new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            LOGGER.info("Error sending metrics: {}/{}", throwable.getClass().getSimpleName(),
                    throwable.getMessage());
        }
    }).doOnTerminate(new Action0() {
        @Override
        public void call() {
            client.shutdown();
        }
    });
}

From source file:com.netflix.client.netty.http.NettyHttpClient.java

License:Apache License

@Override
protected HttpClient<I, O> cacheLoadRxClient(Server server) {
    HttpClientBuilder<I, O> clientBuilder;
    if (requestIdProvider != null) {
        clientBuilder = RxContexts.<I, O>newHttpClientBuilder(server.getHost(), server.getPort(),
                requestIdProvider, RxContexts.DEFAULT_CORRELATOR, pipelineConfigurator);
    } else {/*from  w w w. j a v  a 2  s. c  om*/
        clientBuilder = RxContexts.<I, O>newHttpClientBuilder(server.getHost(), server.getPort(),
                RxContexts.DEFAULT_CORRELATOR, pipelineConfigurator);
    }
    Integer connectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null,
            DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
    Integer readTimeout = getProperty(IClientConfigKey.Keys.ReadTimeout, null,
            DefaultClientConfigImpl.DEFAULT_READ_TIMEOUT);
    Boolean followRedirect = getProperty(IClientConfigKey.Keys.FollowRedirects, null, null);
    HttpClientConfig.Builder builder = new HttpClientConfig.Builder().readTimeout(readTimeout,
            TimeUnit.MILLISECONDS);
    if (followRedirect != null) {
        builder.setFollowRedirect(followRedirect);
    }
    RxClient.ClientConfig rxClientConfig = builder.build();
    clientBuilder.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout).config(rxClientConfig);
    if (isPoolEnabled()) {
        clientBuilder.withConnectionPoolLimitStrategy(poolStrategy)
                .withIdleConnectionsTimeoutMillis(idleConnectionEvictionMills)
                .withPoolIdleCleanupScheduler(poolCleanerScheduler);
    } else {
        clientBuilder.withNoConnectionPooling();
    }
    if (sslContextFactory != null) {
        try {
            clientBuilder
                    .withSslEngineFactory(DefaultFactories.fromSSLContext(sslContextFactory.getSSLContext()));
        } catch (ClientSslSocketFactoryException e) {
            throw new RuntimeException(e);
        }
    }
    HttpClient<I, O> client = clientBuilder.build();
    return client;
}

From source file:com.netflix.client.netty.http.SSEClient.java

License:Apache License

@Override
protected HttpClient<I, ServerSentEvent> getRxClient(String host, int port) {
    HttpClientBuilder<I, ServerSentEvent> clientBuilder = new HttpClientBuilder<I, ServerSentEvent>(host, port)
            .pipelineConfigurator(pipelineConfigurator);
    int requestConnectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null,
            DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
    RxClient.ClientConfig rxClientConfig = new HttpClientConfig.Builder().build();

    HttpClient<I, ServerSentEvent> client = clientBuilder
            .channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, requestConnectTimeout).config(rxClientConfig)
            .build();/*from  w w  w .  ja v a2s  .co m*/
    return client;
}

From source file:com.netflix.client.netty.tcp.LoadBalancingTcpClient.java

License:Apache License

@Override
protected RxClient<I, O> cacheLoadRxClient(Server server) {
    ClientBuilder<I, O> builder = RxNetty.newTcpClientBuilder(server.getHost(), server.getPort());
    if (pipelineConfigurator != null) {
        builder.pipelineConfigurator(pipelineConfigurator);
    }//w  w w  .j a v  a 2s  .  c  o  m
    Integer connectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null,
            DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
    builder.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    if (isPoolEnabled()) {
        builder.withConnectionPoolLimitStrategy(poolStrategy)
                .withIdleConnectionsTimeoutMillis(idleConnectionEvictionMills)
                .withPoolIdleCleanupScheduler(poolCleanerScheduler);
    } else {
        builder.withNoConnectionPooling();
    }
    RxClient<I, O> client = builder.build();
    return client;
}