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:org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizerTests.java

License:Apache License

@SuppressWarnings("unchecked")
private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Integer expected) {
    if (expected == null) {
        verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));
        return;/*  ww  w. ja  va 2 s  . co m*/
    }
    verify(factory, times(1)).addServerCustomizers(this.customizerCaptor.capture());
    NettyServerCustomizer serverCustomizer = this.customizerCaptor.getValue();
    HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
    TcpServer tcpConfiguration = ReflectionTestUtils.invokeMethod(httpServer, "tcpConfiguration");
    ServerBootstrap bootstrap = tcpConfiguration.configure();
    Map<Object, Object> options = (Map<Object, Object>) ReflectionTestUtils.getField(bootstrap, "options");
    assertThat(options).containsEntry(ChannelOption.CONNECT_TIMEOUT_MILLIS, expected);
}

From source file:org.springframework.credhub.configuration.ClientHttpConnectorFactory.java

License:Apache License

/**
 * Create a {@link ClientHttpConnector} for the given {@link ClientOptions}.
 *
 * @param options must not be {@literal null}
 * @return a new {@link ClientHttpConnector}.
 *///from  ww w . java  2  s  .c  o  m
public static ClientHttpConnector create(ClientOptions options) {
    HttpClient httpClient = HttpClient.create();

    if (usingCustomCerts(options)) {
        TrustManagerFactory trustManagerFactory = sslCertificateUtils
                .createTrustManagerFactory(options.getCaCertFiles());

        httpClient = httpClient.secure(sslContextSpec -> sslContextSpec.sslContext(
                SslContextBuilder.forClient().sslProvider(SslProvider.JDK).trustManager(trustManagerFactory)));
    } else {
        httpClient = httpClient.secure(sslContextSpec -> sslContextSpec
                .sslContext(SslContextBuilder.forClient().sslProvider(SslProvider.JDK)));
    }

    if (options.getConnectionTimeout() != null) {
        httpClient = httpClient
                .tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
                        Math.toIntExact(options.getConnectionTimeout().toMillis())));
    }

    return new ReactorClientHttpConnector(httpClient);
}

From source file:org.springframework.vault.config.ClientHttpConnectorFactory.java

License:Apache License

/**
 * Create a {@link ClientHttpConnector} for the given {@link ClientOptions} and
 * {@link SslConfiguration}./*from  w  ww . ja va 2  s .com*/
 *
 * @param options must not be {@literal null}
 * @param sslConfiguration must not be {@literal null}
 * @return a new {@link ClientHttpConnector}.
 */
public static ClientHttpConnector create(ClientOptions options, SslConfiguration sslConfiguration) {

    return new ReactorClientHttpConnector(builder -> {

        if (hasSslConfiguration(sslConfiguration)) {

            builder.sslSupport(sslContextBuilder -> {
                configureSsl(sslConfiguration, sslContextBuilder);
            }).poolResources(PoolResources.elastic("vault-http-" + POOL_COUNTER.incrementAndGet()));
        }

        builder.sslHandshakeTimeout(options.getConnectionTimeout());
        builder.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
                Math.toIntExact(options.getConnectionTimeout().toMillis()));
    });
}

From source file:org.thingsplode.synapse.proxy.EndpointProxy.java

License:Apache License

public EndpointProxy initialize() {
    b = new Bootstrap();
    try {// ww w  .j  a v a  2 s.c o m
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslContext != null) {
                    p.addLast(sslContext.newHandler(ch.alloc()));
                }
                //todo: tune the values here
                //p.addLast(new HttpClientCodec());
                //p.addLast(new HttpContentDecompressor());
                p.addLast(HTTP_REQUEST_ENCODER, new HttpRequestEncoder());
                p.addLast(HTTP_RESPONSE_DECODER, new HttpResponseDecoder());
                p.addLast(HTTP_RESPONSE_AGGREGATOR, new HttpObjectAggregator(1048576));
                if (introspection) {
                    p.addLast(httpResponseIntrospector);
                }
                switch (transport.transportType) {
                case HTTP: {
                    p.addLast(REQUEST2HTTP_REQUEST_ENCODER, new Request2HttpRequestEncoder());
                    break;
                }
                case WEBSOCKET: {
                    p.addLast(REQUEST2WS_REQUEST_ENCODER, new Request2WsRequestEncoder());
                    p.addLast(COMMANDRESULT2WS_ENCODER, new CommandResult2WsEncoder(msgIdGeneratorStrategy));
                    break;
                }
                default:
                    logger.warn(
                            "No handler is supporting yet the following transport: " + transport.transportType);
                }

                p.addLast(requestEncoder);
                switch (transport.transportType) {
                case HTTP: {
                    p.addLast(HTTP_RSP2RESPONSE_DECODER, new HttpResponse2ResponseDecoder());
                    break;
                }
                case WEBSOCKET: {
                    p.addLast(WS_MESSAGE_DECODER, new WSMessageDecoder(connectionUri));
                    p.addLast(CMD_AND_NOTIFICATION_HANDLER,
                            new CommandAndNotificationHandler(commandSink, subscriptions));
                    break;
                }
                default:
                    logger.warn("No response handler is supporting yet the following transport: "
                            + transport.transportType);
                }
                p.addLast(responseHandler);
                p.addLast(inboundExceptionHandler);
            }
        });
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    } finally {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            this.logger.info("Activating endpoint proxy (client) shutdown hook...");
            this.stop();
            ForkJoinPool.commonPool().awaitQuiescence(5, TimeUnit.SECONDS);
        }));
    }
    this.lifecycle = ComponentLifecycle.INITIALIZED;
    return this;

}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }//from   ww  w . j av  a 2s.c om
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }
    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.option(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
}

From source file:org.vootoo.client.netty.connect.SimpleConnectionPool.java

License:Apache License

@Override
protected ChannelFuture connectChannel(Bootstrap bs) {
    bs.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    return bs.connect(socketAddress);
}

From source file:org.vootoo.client.netty.NettySolrClientTest.java

License:Apache License

@BeforeClass
public static void start_local_netty() throws InterruptedException {
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(serverGroup).channel(LocalServerChannel.class)
            .handler(new ChannelInitializer<LocalServerChannel>() {
                @Override//from w ww  .  j  a  va 2s. co  m
                public void initChannel(LocalServerChannel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
                }
            }).childHandler(new MockSolrServerChannelInitializer());

    //client
    client = new Bootstrap();
    client.group(clientGroup).channel(LocalChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);

    // Start the server.
    ChannelFuture serverFuture = sb.bind(addr).sync();

    serverChannel = serverFuture.channel();
}

From source file:org.vootoo.server.netty.SolrServerHandlerTest.java

License:Apache License

@BeforeClass
public static void start_local_netty() throws InterruptedException {
    queryExecutor = new RequestExecutor(
            ExecutorConfig.createDefault().setName("query-executor").setThreadNum(5));
    updateExecutor = new RequestExecutor(
            ExecutorConfig.createDefault().setName("update-executor").setThreadNum(2));
    // Note that we can use any event loop to ensure certain local channels
    // are handled by the same event loop thread which drives a certain socket channel
    // to reduce the communication latency between socket channels and local channels.
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(serverGroup).channel(LocalServerChannel.class)
            .handler(new ChannelInitializer<LocalServerChannel>() {
                @Override/*from   w  ww .  j  a va 2  s . c om*/
                public void initChannel(LocalServerChannel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
                }
            }).childHandler(new SolrServerChannelInitializer(h.getCoreContainer(), new ChannelHandlerConfigs(),
                    queryExecutor, updateExecutor));

    client = new Bootstrap();
    client.group(clientGroup).channel(LocalChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);

    // Start the server.
    ChannelFuture serverFuture = sb.bind(addr).sync();

    serverChannel = serverFuture.channel();
}

From source file:org.waarp.common.utility.WaarpNettyUtil.java

License:Open Source License

/**
 * Add default configuration for client bootstrap
 * /*from   w  ww. j  a  v a 2  s.  c  o m*/
 * @param bootstrap
 * @param group
 * @param timeout
 */
public static void setBootstrap(Bootstrap bootstrap, EventLoopGroup group, int timeout) {
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.group(group);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.option(ChannelOption.SO_RCVBUF, 1048576);
    bootstrap.option(ChannelOption.SO_SNDBUF, 1048576);
}

From source file:org.waarp.common.utility.WaarpNettyUtil.java

License:Open Source License

/**
 * Add default configuration for server bootstrap
 * //from  w  w w .j  ava  2s. com
 * @param bootstrap
 * @param groupBoss
 * @param groupWorker
 * @param timeout
 */
public static void setServerBootstrap(ServerBootstrap bootstrap, EventLoopGroup groupBoss,
        EventLoopGroup groupWorker, int timeout) {
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.group(groupBoss, groupWorker);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, 1048576);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, 1048576);
}