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.waarp.openr66.protocol.localhandler.LocalTransaction.java

License:Open Source License

/**
 * Constructor//  w  w  w .ja  v a2 s .c o  m
 */
public LocalTransaction() {
    serverBootstrap.channel(LocalServerChannel.class);
    serverBootstrap.group(Configuration.configuration.getLocalBossGroup(),
            Configuration.configuration.getLocalWorkerGroup());
    serverBootstrap.option(ChannelOption.TCP_NODELAY, true);
    serverBootstrap.option(ChannelOption.SO_REUSEADDR, true);
    serverBootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            (int) Configuration.configuration.getTIMEOUTCON());
    serverBootstrap.childHandler(new LocalServerInitializer());
    try {
        serverChannel = serverBootstrap.bind(socketLocalServerAddress).sync().channel();
    } catch (InterruptedException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
    localChannelGroup.add(serverChannel);

    clientBootstrap.channel(LocalChannel.class);
    // Same Group than Network final handler 
    clientBootstrap.group(Configuration.configuration.getLocalWorkerGroup());
    clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            (int) Configuration.configuration.getTIMEOUTCON());
    clientBootstrap.handler(new LocalClientInitializer());
}

From source file:org.wenxueliu.netty.client.ClientTest.java

License:Apache License

private void connectRetry(String ip, int port, ChannelFutureListener clientConnectionListener) {
    try {//from www.j  a v  a 2 s  . c o  m
        bootstrap = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE)
                .option(ChannelOption.SO_RCVBUF, SEND_BUFFER_SIZE)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);

        pipelineFactory = new DefaultChannelInitializer(timer, this);
        bootstrap.handler(pipelineFactory);
        //.handler(new ChannelInitializer<SocketChannel>() {

        //    @Override
        //    protected void initChannel(SocketChannel channel) throws Exception {
        //        ChannelPipeline p = channel.pipeline();
        //        p.addLast(new MessageDecoder(),
        //                  new StringEncoder(CharsetUtil.UTF_8),
        //                  new IdleStateHandler(IDLE_TIMEOUT_SEC, 0, 0),
        //                  new BootstrapTimeoutHandler(timer, 10),
        //                  new ConnectionHandler());
        //    }
        //});
        bootstrap.remoteAddress(ip, port);
        ChannelFuture future = bootstrap.connect();
        future.awaitUninterruptibly();
        future.addListener(clientConnectionListener);
    } catch (Exception e) {
        log.warn("Connection to the server {}:{} failed", ip, port);
    }
}

From source file:org.wso2.carbon.gateway.internal.transport.sender.channel.ChannelUtils.java

License:Open Source License

/**
 * Provides incomplete Netty channel future.
 *
 * @param targetChannel  Target channel which has channel specific parameters such as handler
 * @param eventLoopGroup Event loop group of inbound IO workers
 * @param eventLoopClass Event loop class if Inbound IO Workers
 * @param httpRoute      Http Route which represents BE connections
 * @return ChannelFuture//from   w  w  w.  ja v  a 2 s .  c  om
 */
@SuppressWarnings("unchecked")
public static ChannelFuture getNewChannelFuture(TargetChannel targetChannel, EventLoopGroup eventLoopGroup,
        Class eventLoopClass, HttpRoute httpRoute) {
    BootstrapConfiguration bootstrapConfiguration = BootstrapConfiguration.getInstance();
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.channel(eventLoopClass);
    clientBootstrap.group(eventLoopGroup);
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, bootstrapConfiguration.isKeepAlive());
    clientBootstrap.option(ChannelOption.TCP_NODELAY, bootstrapConfiguration.isTcpNoDelay());
    clientBootstrap.option(ChannelOption.SO_REUSEADDR, bootstrapConfiguration.isSocketReuse());
    clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, bootstrapConfiguration.getConnectTimeOut());

    // set the pipeline factory, which creates the pipeline for each newly created channels
    TargetInitializer targetInitializer = new TargetInitializer();
    targetChannel.setTargetInitializer(targetInitializer);
    clientBootstrap.handler(targetInitializer);
    if (log.isDebugEnabled()) {
        log.debug("Created new TCP client bootstrap connecting to {}:{} with options: {}", httpRoute.getHost(),
                httpRoute.getPort(), clientBootstrap);
    }

    return clientBootstrap.connect(new InetSocketAddress(httpRoute.getHost(), httpRoute.getPort()));
}

From source file:org.wso2.carbon.transport.http.netty.listener.HTTPTransportListener.java

License:Open Source License

private void startTransport() {
    //Create Bootstrap Configuration from listener parameters
    ServerBootstrapConfiguration.createBootStrapConfiguration(transportProperties);
    ServerBootstrapConfiguration serverBootstrapConfiguration = ServerBootstrapConfiguration.getInstance();
    //boss group is for accepting channels
    EventLoopGroup bossGroup = HTTPTransportContextHolder.getInstance().getBossGroup();
    if (bossGroup == null) {
        bossGroup = new NioEventLoopGroup(
                bossGroupSize != 0 ? bossGroupSize : Runtime.getRuntime().availableProcessors());
        HTTPTransportContextHolder.getInstance().setBossGroup(bossGroup);
    }//from   ww w  . j  ava2s.c o  m
    //worker group is for processing IO
    EventLoopGroup workerGroup = HTTPTransportContextHolder.getInstance().getWorkerGroup();
    if (workerGroup == null) {
        workerGroup = new NioEventLoopGroup(
                workerGroupSize != 0 ? workerGroupSize : Runtime.getRuntime().availableProcessors() * 2);
        HTTPTransportContextHolder.getInstance().setWorkerGroup(workerGroup);
    }
    log.debug("Netty Boss group size " + bossGroup);
    log.debug("Netty Worker group Size" + workerGroup);
    bootstrap = new ServerBootstrap();
    bootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());
    log.debug("Netty Server Socket BACKLOG " + serverBootstrapConfiguration.getSoBackLog());

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);

    addChannelInitializer();

    bootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    log.debug("Netty Server Socket TCP_NODELAY " + serverBootstrapConfiguration.isTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    log.debug("Netty Server Socket SO_KEEPALIVE " + serverBootstrapConfiguration.isKeepAlive());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverBootstrapConfiguration.getConnectTimeOut());
    log.debug(
            " Netty Server Socket CONNECT_TIMEOUT_MILLIS " + serverBootstrapConfiguration.getConnectTimeOut());

    bootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
    bootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());

    if (defaultListenerConfig.isBindOnStartup()) {
        bindInterface(defaultListenerConfig);
    }

    TransportListenerManager transportListenerManager = HTTPTransportContextHolder.getInstance().getManager();
    if (transportListenerManager != null) {
        transportListenerManager.registerTransportListener(this);
    }

}

From source file:org.wso2.carbon.transport.http.netty.listener.NettyListener.java

License:Open Source License

private void startTransport() {
    ServerBootstrapConfiguration.createBootStrapConfiguration(nettyConfig.getParameters());
    ServerBootstrapConfiguration serverBootstrapConfiguration = ServerBootstrapConfiguration.getInstance();
    bossGroup = new NioEventLoopGroup(nettyConfig.getBossThreadPoolSize());
    workerGroup = new NioEventLoopGroup(nettyConfig.getWorkerThreadPoolSize());
    bootstrap = new ServerBootstrap();
    bootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);

    addChannelInitializer();//from  w  w w . j  av  a 2  s. c  o  m
    bootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverBootstrapConfiguration.getConnectTimeOut());

    bootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    bootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());

    setupChannelInitializer();
    try {
        bootstrap.bind(new InetSocketAddress(nettyConfig.getHost(), nettyConfig.getPort())).sync();
        TransportListenerManager artifactDeployer = NettyTransportContextHolder.getInstance().getManager();
        if (artifactDeployer != null) {
            artifactDeployer.registerTransportListener(id, this);
        }
        log.info("Netty Listener starting on port " + nettyConfig.getPort());
    } catch (InterruptedException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.transport.http.netty.listener.ServerConnectorBootstrap.java

License:Open Source License

public void addSocketConfiguration(ServerBootstrapConfiguration serverBootstrapConfiguration) {
    // Set other serverBootstrap parameters
    serverBootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());
    serverBootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    serverBootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            serverBootstrapConfiguration.getConnectTimeOut());
    serverBootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    serverBootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    serverBootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    serverBootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());

    log.debug("Netty Server Socket BACKLOG " + serverBootstrapConfiguration.getSoBackLog());
    log.debug("Netty Server Socket TCP_NODELAY " + serverBootstrapConfiguration.isTcpNoDelay());
    log.debug("Netty Server Socket SO_KEEPALIVE " + serverBootstrapConfiguration.isKeepAlive());
    log.debug("Netty Server Socket CONNECT_TIMEOUT_MILLIS " + serverBootstrapConfiguration.getConnectTimeOut());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
}

From source file:org.wso2.carbon.transport.http.netty.listener.ServerConnectorController.java

License:Open Source License

public void start() {

    Set<TransportProperty> transportPropertiesSet = transportsConfiguration.getTransportProperties();

    Map<String, Object> transportProperties = new HashMap<>();

    if (transportPropertiesSet != null && !transportPropertiesSet.isEmpty()) {
        transportProperties = transportPropertiesSet.stream()
                .collect(Collectors.toMap(TransportProperty::getName, TransportProperty::getValue));

    }// w ww  . ja v a 2  s.c o m

    // Create Bootstrap Configuration from listener parameters
    ServerBootstrapConfiguration.createBootStrapConfiguration(transportProperties);
    ServerBootstrapConfiguration serverBootstrapConfiguration = ServerBootstrapConfiguration.getInstance();

    // Create Boss Group - boss group is for accepting channels
    EventLoopGroup bossGroup = HTTPTransportContextHolder.getInstance().getBossGroup();
    if (bossGroup == null) {
        int bossGroupSize = Util.getIntProperty(transportProperties, Constants.SERVER_BOOTSTRAP_BOSS_GROUP_SIZE,
                Runtime.getRuntime().availableProcessors());

        bossGroup = new NioEventLoopGroup(bossGroupSize);
        HTTPTransportContextHolder.getInstance().setBossGroup(bossGroup);
    }

    // Create Worker Group - worker group is for processing IO
    EventLoopGroup workerGroup = HTTPTransportContextHolder.getInstance().getWorkerGroup();
    if (workerGroup == null) {
        int workerGroupSize = Util.getIntProperty(transportProperties,
                Constants.SERVER_BOOTSTRAP_WORKER_GROUP_SIZE, Runtime.getRuntime().availableProcessors() * 2);
        workerGroup = new NioEventLoopGroup(workerGroupSize);
        HTTPTransportContextHolder.getInstance().setWorkerGroup(workerGroup);
    }
    // Set Handler Executor
    HTTPTransportContextHolder.getInstance().setHandlerExecutor(new HandlerExecutor());

    bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);

    // Register Channel initializer
    handler = new HTTPServerChannelInitializer();
    handler.setupConnectionManager(transportProperties);
    bootstrap.childHandler(handler);

    int bufferSize = Util.getIntProperty(transportProperties, Constants.OUTPUT_CONTENT_BUFFER_SIZE, 0);

    if (bufferSize != 0) {
        BufferFactory.createInstance(bufferSize);
    }

    // Set other bootstrap parameters
    bootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());
    log.debug("Netty Server Socket BACKLOG " + serverBootstrapConfiguration.getSoBackLog());
    bootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    log.debug("Netty Server Socket TCP_NODELAY " + serverBootstrapConfiguration.isTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    log.debug("Netty Server Socket SO_KEEPALIVE " + serverBootstrapConfiguration.isKeepAlive());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverBootstrapConfiguration.getConnectTimeOut());
    log.debug(
            " Netty Server Socket CONNECT_TIMEOUT_MILLIS " + serverBootstrapConfiguration.getConnectTimeOut());
    bootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
    bootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    bootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    bootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());

    initialized = true;
}

From source file:org.wso2.carbon.transport.http.netty.sender.channel.ChannelUtils.java

License:Open Source License

/**
 * Provides incomplete Netty channel future.
 *
 * @param targetChannel       Target channel which has channel specific parameters such as handler
 * @param eventLoopGroup      Event loop group of inbound IO workers
 * @param eventLoopClass      Event loop class if Inbound IO Workers
 * @param httpRoute           Http Route which represents BE connections
 * @param senderConfiguration sender configuration
 * @return ChannelFuture/*from   w ww . ja  va2  s . c o  m*/
 */
@SuppressWarnings("unchecked")
public static ChannelFuture getNewChannelFuture(TargetChannel targetChannel, EventLoopGroup eventLoopGroup,
        Class eventLoopClass, HttpRoute httpRoute, SenderConfiguration senderConfiguration) {
    BootstrapConfiguration bootstrapConfiguration = BootstrapConfiguration.getInstance();
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.channel(eventLoopClass);
    clientBootstrap.group(eventLoopGroup);
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, bootstrapConfiguration.isKeepAlive());
    clientBootstrap.option(ChannelOption.TCP_NODELAY, bootstrapConfiguration.isTcpNoDelay());
    clientBootstrap.option(ChannelOption.SO_REUSEADDR, bootstrapConfiguration.isSocketReuse());
    clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, bootstrapConfiguration.getConnectTimeOut());

    // set the pipeline factory, which creates the pipeline for each newly created channels
    NettyClientInitializer nettyClientInitializer = new NettyClientInitializer(senderConfiguration);
    targetChannel.setNettyClientInitializer(nettyClientInitializer);
    clientBootstrap.handler(nettyClientInitializer);
    if (log.isDebugEnabled()) {
        log.debug("Created new TCP client bootstrap connecting to {}:{} with options: {}", httpRoute.getHost(),
                httpRoute.getPort(), clientBootstrap);
    }

    return clientBootstrap.connect(new InetSocketAddress(httpRoute.getHost(), httpRoute.getPort()));
}

From source file:org.wso2.carbon.transport.http.netty.sender.channel.pool.PoolableTargetChannelFactory.java

License:Open Source License

private Bootstrap instantiateAndConfigBootStrap(EventLoopGroup eventLoopGroup, Class eventLoopClass,
        BootstrapConfiguration bootstrapConfiguration) {
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.channel(eventLoopClass);
    clientBootstrap.group(eventLoopGroup);
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, bootstrapConfiguration.isKeepAlive());
    clientBootstrap.option(ChannelOption.TCP_NODELAY, bootstrapConfiguration.isTcpNoDelay());
    clientBootstrap.option(ChannelOption.SO_REUSEADDR, bootstrapConfiguration.isSocketReuse());
    clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, bootstrapConfiguration.getConnectTimeOut());
    return clientBootstrap;
}

From source file:org.wso2.carbon.transport.http.netty.sender.RedirectHandler.java

License:Open Source License

/**
 * Bootstrap a netty client to send the redirect request.
 *
 * @param channelHandlerContext Channel handler context
 * @param redirectUrl           Redirect URL
 * @return ChannelFuture/*from  w  ww . j  a v a  2s  .  co  m*/
 */
private void bootstrapClient(ChannelHandlerContext channelHandlerContext, URL redirectUrl,
        HTTPCarbonMessage httpCarbonRequest, HttpRequest httpRequest) {
    EventLoopGroup group = channelHandlerContext.channel().eventLoop();
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.group(group).channel(NioSocketChannel.class)
            .remoteAddress(new InetSocketAddress(redirectUrl.getHost(),
                    redirectUrl.getPort() != -1 ? redirectUrl.getPort()
                            : getDefaultPort(redirectUrl.getProtocol())))
            .handler(new RedirectChannelInitializer(sslEngine, httpTraceLogEnabled, maxRedirectCount,
                    chunkDisabled, originalChannelContext, isIdleHandlerOfTargetChannelRemoved));
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            15000);
    ChannelFuture channelFuture = clientBootstrap.connect();
    registerListener(channelHandlerContext, channelFuture, httpCarbonRequest, httpRequest);
}